کار با ستون ها و ردیف ها

برای کنترل بیشتر بر نحوه کار جداول، یاد بگیرید که چگونه ستون ها و ردیف ها را دستکاری کنید.

فهرست عناصر جدول را پیدا کنید

ستون ها، ردیف ها و سلول ها با دسترسی به گره سند انتخاب شده توسط شاخص آن مدیریت می شوند. پیدا کردن شاخص هر گره شامل جمع آوری تمام گره های کودک از نوع عنصر از گره اصلی است و سپس با استفاده از روش 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 (DOM)، گره Table از گره های 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();

ردیف ها را به عنوان ردیف های هدر مشخص کنید

شما می توانید ردیف اول را در جدول به عنوان ردیف سرصفحه فقط در صفحه اول یا در هر صفحه تکرار کنید اگر جدول به چند قسمت تقسیم شود. در Aspose.Words می توانید ردیف هدر را در هر صفحه با استفاده از ویژگی HeadingFormat تکرار کنید.

همچنین می توانید چندین ردیف هدر را علامت بزنید اگر چنین ردیف هایی یکی پس از دیگری در ابتدای جدول قرار داشته باشند. برای انجام این کار، باید ویژگی های HeadingFormat را به این ردیف ها اعمال کنید.

مثال کد زیر نشان می دهد که چگونه یک جدول بسازیم که شامل ردیف های هدر است که در صفحات بعدی تکرار می شوند:

// 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");

جداول و ردیف ها را از شکستن صفحات نگه دارید

گاهی اوقات محتویات یک جدول نباید در صفحات تقسیم شود. به عنوان مثال، اگر یک عنوان بالای یک جدول باشد، عنوان و جدول همیشه باید در یک صفحه نگه داشته شوند تا ظاهر مناسب حفظ شود.

دو تکنیک جداگانه وجود دارد که برای دستیابی به این قابلیت مفید است:

  • Allow row break across pages که به ردیف های جدول اعمال می شود
  • Keep with next که به پاراگراف های سلول های جدول اعمال می شود

به طور پیش فرض، ویژگی های بالا غیرفعال هستند.

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

از شکستن یک ردیف در صفحات جلوگیری کنید

این شامل محدود کردن محتوای داخل سلول های یک ردیف از تقسیم شدن در یک صفحه است. در Microsoft Word، این می تواند در زیر ویژگی های جدول به عنوان گزینه “اجازه دهید ردیف در صفحات شکسته شود"یافت شود. در 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");

نگه داشتن یک جدول از شکستن در صفحات

برای جلوگیری از تقسیم جدول در صفحات، باید مشخص کنیم که می خواهیم محتوای موجود در جدول با هم باقی بماند.

برای انجام این کار، 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");