با ستون ها و ردیف ها کار کنید
برای کنترل بیشتر بر نحوه کار جداول، نحوه دستکاری ستون ها و ردیف ها را بیاموزید.
Table Element Index {#find-the-index-of-table-elements} را پیدا کنید
ستونها، ردیفها و سلولها با دسترسی به گره سند انتخاب شده توسط فهرست آن مدیریت میشوند. یافتن شاخص هر گره شامل جمع آوری تمام گره های فرزند از نوع عنصر از گره والد، و سپس استفاده از روش IndexOf برای یافتن شاخص گره مورد نظر در مجموعه است.
نمایه یک جدول را در یک سند {#find-the-index-of-table-in-a-document} پیدا کنید
گاهی اوقات ممکن است لازم باشد تغییراتی در یک جدول خاص در یک سند ایجاد کنید. برای این کار می توانید با شاخص آن به یک جدول مراجعه کنید.
مثال کد زیر نحوه بازیابی نمایه جدول در یک سند را نشان می دهد:
// 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); |
نمایه یک ردیف را در جدول {#find-the-index-of-a-row-in-a-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); |
نمایه یک سلول در یک ردیف {#find-the-index-of-a-cell-in-a-row} را پیدا کنید
در نهایت، ممکن است لازم باشد تغییراتی در یک سلول خاص ایجاد کنید، و میتوانید این کار را با فهرست سلولی نیز انجام دهید.
مثال کد زیر نحوه بازیابی نمایه یک سلول در یک ردیف را نشان می دهد:
// 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]); |
با ستون {#work-with-columns} کار کنید
در Aspose.Words Document Object Model (DOM)، گره Table از گره های Row و سپس گره های Cell تشکیل شده است. بنابراین، در Document
Object Model 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; | |
} |
مثال کد زیر نحوه درج یک ستون خالی در جدول را نشان می دهد:
// 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(); |
ردیف ها را به عنوان ردیف های سرصفحه مشخص کنید
شما می توانید انتخاب کنید که ردیف اول جدول به عنوان ردیف سرصفحه فقط در صفحه اول یا در هر صفحه تکرار شود، اگر جدول به چندین تقسیم شود. در Aspose.Words، میتوانید با استفاده از ویژگی HeadingFormat، ردیف سرصفحه را در هر صفحه تکرار کنید.
همچنین اگر این سطرها یکی پس از دیگری در ابتدای جدول قرار دارند، می توانید چندین ردیف سرصفحه را علامت گذاری کنید. برای این کار باید ویژگی های HeadingFormat را روی این ردیف ها اعمال کنید.
مثال کد زیر نحوه ساخت جدولی را نشان می دهد که شامل ردیف های سرصفحه است که در صفحات بعدی تکرار می شود:
// 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"); |
جداول و سطرها را از شکستن در صفحات {#keep-tables-and-rows-from-breaking-across-pages} حفظ کنید
مواقعی وجود دارد که محتویات یک جدول نباید در صفحات تقسیم شود. به عنوان مثال، اگر عنوانی در بالای جدول باشد، عنوان و جدول باید همیشه با هم در یک صفحه نگه داشته شوند تا ظاهر مناسب حفظ شود.
دو تکنیک جداگانه برای دستیابی به این عملکرد مفید هستند:
Allow row break across pages
که برای ردیف های جدول اعمال می شودKeep with next
که روی پاراگراف های سلول های جدول اعمال می شود
به طور پیش فرض، ویژگی های فوق غیرفعال هستند.
یک ردیف از شکستن در سراسر صفحات {#keep-a-row-from-breaking-across-pages} حفظ کنید
این شامل محدود کردن محتوای داخل سلولهای یک ردیف از تقسیم شدن در یک صفحه است. در Microsoft Word، این مورد را می توان در زیر ویژگی های جدول به عنوان گزینه “Allow row to break into pages” یافت. در 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"); |
یک جدول را از شکستن در سراسر صفحات {#keep-a-table-from-breaking-across-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"); |