עבודה עם טורים ו Rows

לקבלת יותר שליטה על איך שולחנות לעבוד, ללמוד כיצד לתמרן עמודות ושורה.

עקבו אחרי Table Element Index

טורים, שורות ותאים מנוהלים על ידי גישה למסמך שנבחר על ידי המדד שלו. מציאת האינדקס של כל צומת כרוך באיסוף כל אבני הילד של הסוג האלמנט מן הצומת ההורה, ולאחר מכן באמצעות שימוש IndexOf שיטה למצוא את המדד של הצומת הרצוי באוסף.

מצא את מדד השולחן במסמך

לפעמים אתה יכול לעשות שינויים בטבלה מסוימת במסמך. כדי לעשות זאת, אתה יכול להתייחס לשולחן על ידי המדד שלו.

לדוגמה הקוד הבא מראה כיצד לשחזר את מדד השולחן במסמך:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true);
int tableIndex = allTables.IndexOf(table);

מצא את מדד השורה בטבלה

כמו כן, ייתכן שיהיה עליך לבצע שינויים בשורה מסוימת בטבלה שנבחרה. כדי לעשות זאת, אתה יכול גם להתייחס לשורה על ידי המדד שלה.

דוגמא הקוד הבא מראה כיצד לשחזר את מדד השורה בטבלה:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
int rowIndex = table.IndexOf(table.LastRow);

מצא את מדד התא בקו

לבסוף, ייתכן שיהיה עליך לבצע שינויים בתא מסוים, ואתה יכול לעשות זאת גם על ידי מדד התא.

הדוגמה הבאה של הקוד מראה כיצד לשחזר את מדד התא בשורה:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
int cellIndex = row.IndexOf(row.Cells[4]);

עבודה עם טורים

בתוך Aspose.Words Document Object Model ()DOM) Table Node מורכב Row ואז Cell צומת כך, ב Document מודל אובייקטים Aspose.Wordsכמו מסמכי Word, אין מושג של עמודה.

על ידי עיצוב, טבלאות Microsoft Word ו Aspose.Words הם עצמאיים לחלוטין, והנכסים והפעולות הבסיסיות כלולים רק בשורות ובתאים של השולחן. זה נותן טבלאות את היכולת להיות כמה תכונות מעניינות:

  • לכל שורה של שולחן יש מספר שונה לחלוטין של תאים
  • באופן רוטטי, התאים של כל שורה יכולים להיות רוחביים שונים
    • ניתן להצטרף לטבלאות עם פורמטים שונים של שורות ומספר תאים

כל פעולות המבוצעות על עמודות הן למעשה “מקצרות” שמבצעות את הניתוח על ידי שינוי קולקטיבי של תאי שורות באופן שנראה כאילו הן מוחלות על עמודות. כלומר, אתה יכול לבצע פעולות על עמודות פשוט על ידי זהה מעל אותו טבלת תאים אינדקס.

הדוגמה הקודית הבאה מפשטת פעולות כאלה על ידי הוכחת מעמד חזיתי שאוסף את התאים המרכיבים את ה"קומן" של שולחן:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Represents a facade object for a column of a table in a Microsoft Word document.
/// </summary>
internal class Column
{
private Column(Table table, int columnIndex)
{
mTable = table ?? throw new ArgumentException("table");
mColumnIndex = columnIndex;
}
/// <summary>
/// Returns a new column facade from the table and supplied zero-based index.
/// </summary>
public static Column FromIndex(Table table, int columnIndex)
{
return new Column(table, columnIndex);
}
/// <summary>
/// Returns the cells which make up the column.
/// </summary>
public Cell[] Cells => GetColumnCells().ToArray();
/// <summary>
/// Returns the index of the given cell in the column.
/// </summary>
public int IndexOf(Cell cell)
{
return GetColumnCells().IndexOf(cell);
}
/// <summary>
/// Inserts a brand new column before this column into the table.
/// </summary>
public Column InsertColumnBefore()
{
Cell[] columnCells = Cells;
if (columnCells.Length == 0)
throw new ArgumentException("Column must not be empty");
// Create a clone of this column.
foreach (Cell cell in columnCells)
cell.ParentRow.InsertBefore(cell.Clone(false), cell);
// This is the new column.
Column column = new Column(columnCells[0].ParentRow.ParentTable, mColumnIndex);
// We want to make sure that the cells are all valid to work with (have at least one paragraph).
foreach (Cell cell in column.Cells)
cell.EnsureMinimum();
// Increase the index which this column represents since there is now one extra column in front.
mColumnIndex++;
return column;
}
/// <summary>
/// Removes the column from the table.
/// </summary>
public void Remove()
{
foreach (Cell cell in Cells)
cell.Remove();
}
/// <summary>
/// Returns the text of the column.
/// </summary>
public string ToTxt()
{
StringBuilder builder = new StringBuilder();
foreach (Cell cell in Cells)
builder.Append(cell.ToString(SaveFormat.Text));
return builder.ToString();
}
/// <summary>
/// Provides an up-to-date collection of cells which make up the column represented by this facade.
/// </summary>
private List<Cell> GetColumnCells()
{
List<Cell> columnCells = new List<Cell>();
foreach (Row row in mTable.Rows)
{
Cell cell = row.Cells[mColumnIndex];
if (cell != null)
columnCells.Add(cell);
}
return columnCells;
}
private int mColumnIndex;
private readonly Table mTable;
}
view raw column-class.cs hosted with ❤ by GitHub

הדוגמה הבאה של הקוד מראה כיצד להכניס עמודה ריקה לשולחן:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
Column column = Column.FromIndex(table, 0);
// Print the plain text of the column to the screen.
Console.WriteLine(column.ToTxt());
// Create a new column to the left of this column.
// This is the same as using the "Insert Column Before" command in Microsoft Word.
Column newColumn = column.InsertColumnBefore();
foreach (Cell cell in newColumn.Cells)
cell.FirstParagraph.AppendChild(new Run(doc, "Column Text " + newColumn.IndexOf(cell)));

לדוגמה הקוד הבא מראה כיצד להסיר עמודה משולחן במסמך:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 1, true);
Column column = Column.FromIndex(table, 2);
column.Remove();

תגית: Header Rows

אתה יכול לבחור לחזור על השורה הראשונה בטבלה כמו Header Row רק בעמוד הראשון או על כל דף אם השולחן מחולק למספר. In In In Aspose.Words, אתה יכול לחזור על Header Row על כל דף באמצעות HeadingFormat רכוש.

אתה יכול גם לסמן שורות ראש מרובות אם שורות כאלה ממוקמות אחד לאחר השני בתחילת השולחן. כדי לעשות זאת, עליך ליישם את HeadingFormat תכונות שורות אלה.

הדוגמה הקודית הבאה מראה כיצד לבנות שולחן הכולל את Header Rows שחוזר על העמודים הבאים:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();
builder.RowFormat.HeadingFormat = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CellFormat.Width = 100;
builder.InsertCell();
builder.Writeln("Heading row 1");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Heading row 2");
builder.EndRow();
builder.CellFormat.Width = 50;
builder.ParagraphFormat.ClearFormatting();
for (int i = 0; i < 50; i++)
{
builder.InsertCell();
builder.RowFormat.HeadingFormat = false;
builder.Write("Column 1 Text");
builder.InsertCell();
builder.Write("Column 2 Text");
builder.EndRow();
}
doc.Save(ArtifactsDir + "WorkingWithTables.RepeatRowsOnSubsequentPages.docx");

שמור שולחנות ו Rows from Breaking Overs דפים

ישנם זמנים שבהם התוכן של שולחן לא צריך להיות מחולק על פני דפים. לדוגמה, אם כותרת היא מעל שולחן, הכותרת והשולחן צריכים תמיד להיות יחד באותו דף כדי לשמור על המראה הנכון.

ישנן שתי טכניקות נפרדות שימושיות להשגת פונקציונליות זו:

    • Allow row break across pages, המונחים: Tables
    • Keep with next, אשר חל על פסקאות בתאי שולחן

כברירת מחדל, התכונות לעיל מוגבלות.

עקבו אחרי Breaking Overs Pages

זה כרוך הגבלת תוכן בתוך התאים של שורה מלהיות מפוצל על פני דף. In In In Microsoft Word, זה יכול למצוא תחת טבלה Properties כאפשרות “לעמוד שורות כדי לפרוץ דפים”. In In In Aspose.Words זה נמצא תחת RowFormat אובייקט של Row כמו הנכס RowFormat.AllowBreakAcrossPages.

הדוגמה הבאה של הקוד מראה כיצד להשבית שורות על פני דפים לכל שורה בטבלה:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Table spanning two pages.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Disable breaking across pages for all rows in the table.
foreach (Row row in table.Rows)
row.RowFormat.AllowBreakAcrossPages = false;
doc.Save(ArtifactsDir + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx");

עקבו אחרי Breaking Overs Pages

כדי לעצור את השולחן מפיצול בדפים, עלינו לציין שאנו רוצים שהתוכן הכלול בתוך השולחן כדי להישאר יחד.

לעשות את זה, Aspose.Words משתמש בשיטה, המאפשרת למשתמשים לבחור שולחן ולאפשר את KeepWithNext פרמטר true לכל פסקה בתוך תאי השולחן. היוצא מן הכלל הוא הסעיף האחרון בטבלה, שאמור להיות מוגדר. false.

דוגמה לקוד הבא מראה כיצד להגדיר שולחן כדי להישאר יחד באותו דף:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Table spanning two pages.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// We need to enable KeepWithNext for every paragraph in the table to keep it from breaking across a page,
// except for the last paragraphs in the last row of the table.
foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true))
{
cell.EnsureMinimum();
foreach (Paragraph para in cell.Paragraphs)
if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell))
para.ParagraphFormat.KeepWithNext = true;
}
doc.Save(ArtifactsDir + "WorkingWithTables.KeepTableTogether.docx");