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

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

עקבו אחרי Table Element Index

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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-Java.git.
int rowIndex = table.indexOf(table.getLastRow());

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
int cellIndex = row.indexOf(row.getCells().get(4));

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

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

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

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

work-with-columns-aspose-words-java

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
/// <summary>
/// Represents a facade object for a column of a table in a Microsoft Word document.
/// </summary>
static class Column
{
private Column(Table table, int columnIndex) {
if (table != null) {
mTable = table;
} else {
throw new IllegalArgumentException("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);
}
private ArrayList<Cell> getCells() {
return getColumnCells();
}
/// <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()
{
ArrayList<Cell> columnCells = getCells();
if (columnCells.size() == 0)
throw new IllegalArgumentException("Column must not be empty");
// Create a clone of this column.
for (Cell cell : columnCells)
cell.getParentRow().insertBefore(cell.deepClone(false), cell);
// This is the new column.
Column column = new Column(columnCells.get(0).getParentRow().getParentTable(), mColumnIndex);
// We want to make sure that the cells are all valid to work with (have at least one paragraph).
for (Cell cell : column.getCells())
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()
{
for (Cell cell : getCells())
cell.remove();
}
/// <summary>
/// Returns the text of the column.
/// </summary>
public String toTxt() throws Exception
{
StringBuilder builder = new StringBuilder();
for (Cell cell : getCells())
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 ArrayList<Cell> getColumnCells()
{
ArrayList<Cell> columnCells = new ArrayList<Cell>();
for (Row row : mTable.getRows())
{
Cell cell = row.getCells().get(mColumnIndex);
if (cell != null)
columnCells.add(cell);
}
return columnCells;
}
private int mColumnIndex;
private Table mTable;
}

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "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.
System.out.println(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();
for (Cell cell : newColumn.getColumnCells())
cell.getFirstParagraph().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-Java.git.
Document doc = new Document(getMyDir() + "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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
builder.getRowFormat().setHeadingFormat(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getCellFormat().setWidth(100.0);
builder.insertCell();
builder.writeln("Heading row 1");
builder.endRow();
builder.insertCell();
builder.writeln("Heading row 2");
builder.endRow();
builder.getCellFormat().setWidth(50.0);
builder.getParagraphFormat().clearFormatting();
for (int i = 0; i < 50; i++)
{
builder.insertCell();
builder.getRowFormat().setHeadingFormat(false);
builder.write("Column 1 Text");
builder.insertCell();
builder.write("Column 2 Text");
builder.endRow();
}
doc.save(getArtifactsDir() + "WorkingWithTables.RepeatRowsOnSubsequentPages.docx");

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

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

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

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

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

keep-tables-and-rows-from-breaking-across-pages-aspose-words-java

עקבו אחרי Breaking Overs Pages

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

keep-rows-from-breaking-across-pages-aspose-words-java

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Table spanning two pages.docx");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// Disable breaking across pages for all rows in the table.
for (Row row : (Iterable<Row>) table.getRows())
row.getRowFormat().setAllowBreakAcrossPages(false);
doc.save(getArtifactsDir() + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx");

עקבו אחרי Breaking Overs Pages

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

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

keep-tables-from-breaking-across-pages-aspose-words-java

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "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.
for (Cell cell : (Iterable<Cell>) table.getChildNodes(NodeType.CELL, true))
{
cell.ensureMinimum();
for (Paragraph para : (Iterable<Paragraph>) cell.getParagraphs())
if (!(cell.getParentRow().isLastRow() && para.isEndOfCell()))
para.getParagraphFormat().setKeepWithNext(true);
}
doc.save(getArtifactsDir() + "WorkingWithTables.KeepTableTogether.docx");