العمل مع الأعمدة والصفوف

لمزيد من التحكم في كيفية عمل الجداول، تعرف على كيفية التعامل مع الأعمدة والصفوف.

ابحث عن فهرس عناصر الجدول

تتم إدارة الأعمدة والصفوف والخلايا عن طريق الوصول إلى عقدة المستند المحددة من خلال فهرسها. يتضمن العثور على فهرس أي عقدة جمع كل العقد الفرعية لنوع العنصر من العقدة الأصلية، ثم استخدام طريقة 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 من عقد 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();

تحديد الصفوف كصفوف رأس

يمكنك اختيار تكرار الصف الأول في الجدول كصف الرأس فقط في الصفحة الأولى أو في كل صفحة إذا تم تقسيم الجدول إلى عدة صفحات. في 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");

منع الجداول والصفوف من الاختراق عبر صفحات

هناك أوقات لا ينبغي فيها تقسيم محتويات الجدول عبر الصفحات. على سبيل المثال، إذا كان العنوان أعلى الجدول، فيجب دائمًا الاحتفاظ بالعنوان والجدول معًا في نفس الصفحة للحفاظ على المظهر المناسب.

هناك طريقتان منفصلتان مفيدتان لتحقيق هذه الوظيفة:

  • Allow row break across pages، والذي يتم تطبيقه على صفوف الجدول
  • Keep with next، والذي يتم تطبيقه على الفقرات في خلايا الجدول

بشكل افتراضي، يتم تعطيل الخصائص المذكورة أعلاه.

منع الصف من الاختراق عبر صفحات

يتضمن ذلك تقييد المحتوى الموجود داخل خلايا الصف من الانقسام عبر الصفحة. في Microsoft Word، يمكن العثور على هذا ضمن خصائص الجدول كخيار “السماح للصف بالتقاطع بين الصفحات”. في 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");

منع الجدول من الاختراق عبر صفحات

لمنع تقسيم الجدول عبر الصفحات، نحتاج إلى تحديد أننا نريد أن يظل المحتوى الموجود داخل الجدول معًا.

للقيام بذلك، يستخدم 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");