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

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

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

ستون ها، ردیف ها و سلول ها با دسترسی به گره سند انتخاب شده توسط شاخص آن مدیریت می شوند. پیدا کردن شاخص هر گره شامل جمع آوری تمام گره های کودک از نوع عنصر از گره اصلی است و سپس با استفاده از روش IndexOf برای پیدا کردن شاخص گره مورد نظر در مجموعه.

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<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-C.git.
int rowIndex = table->IndexOf(table->get_LastRow());

شاخص یک سلول را در یک ردیف پیدا کنید

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

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

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

کار با ستون ها

در مدل Aspose.Words Document Object (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-C.git.
/// <summary>
/// Represents a facade object for a column of a table in a Microsoft Word document.
/// </summary>
class Column : public System::Object
{
public:
/// <summary>
/// Returns the cells which make up the column.
/// </summary>
ArrayPtr<SharedPtr<Cell>> get_Cells()
{
return GetColumnCells()->ToArray();
}
/// <summary>
/// Returns a new column facade from the table and supplied zero-based index.
/// </summary>
static SharedPtr<WorkingWithTables::Column> FromIndex(SharedPtr<Table> table, int columnIndex)
{
return WorkingWithTables::Column::MakeObject(table, columnIndex);
}
/// <summary>
/// Returns the index of the given cell in the column.
/// </summary>
int IndexOf(SharedPtr<Cell> cell)
{
return GetColumnCells()->IndexOf(cell);
}
/// <summary>
/// Inserts a brand new column before this column into the table.
/// </summary>
SharedPtr<WorkingWithTables::Column> InsertColumnBefore()
{
ArrayPtr<SharedPtr<Cell>> columnCells = get_Cells();
if (columnCells->get_Length() == 0)
{
throw System::ArgumentException(u"Column must not be empty");
}
// Create a clone of this column.
for (SharedPtr<Cell> cell : columnCells)
{
cell->get_ParentRow()->InsertBefore(cell->Clone(false), cell);
}
// This is the new column.
auto column = WorkingWithTables::Column::MakeObject(columnCells[0]->get_ParentRow()->get_ParentTable(), mColumnIndex);
// We want to make sure that the cells are all valid to work with (have at least one paragraph).
for (SharedPtr<Cell> cell : column->get_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>
void Remove()
{
for (SharedPtr<Cell> cell : get_Cells())
{
cell->Remove();
}
}
/// <summary>
/// Returns the text of the column.
/// </summary>
String ToTxt()
{
auto builder = System::MakeObject<System::Text::StringBuilder>();
for (SharedPtr<Cell> cell : get_Cells())
{
builder->Append(cell->ToString(SaveFormat::Text));
}
return builder->ToString();
}
private:
int mColumnIndex;
SharedPtr<Table> mTable;
Column(SharedPtr<Table> table, int columnIndex) : mColumnIndex(0)
{
if (table == nullptr)
{
throw System::ArgumentException(u"table");
}
mTable = table;
mColumnIndex = columnIndex;
}
MEMBER_FUNCTION_MAKE_OBJECT(Column, CODEPORTING_ARGS(SharedPtr<Table> table, int columnIndex), CODEPORTING_ARGS(table, columnIndex));
/// <summary>
/// Provides an up-to-date collection of cells which make up the column represented by this facade.
/// </summary>
SharedPtr<System::Collections::Generic::List<SharedPtr<Cell>>> GetColumnCells()
{
SharedPtr<System::Collections::Generic::List<SharedPtr<Cell>>> columnCells =
System::MakeObject<System::Collections::Generic::List<SharedPtr<Cell>>>();
for (const auto& row : System::IterateOver<Row>(mTable->get_Rows()))
{
SharedPtr<Cell> cell = row->get_Cells()->idx_get(mColumnIndex);
if (cell != nullptr)
{
columnCells->Add(cell);
}
}
return columnCells;
}
};
view raw column-class.h hosted with ❤ by GitHub

مثال کد زیر نشان می دهد که چگونه یک ستون خالی را در یک جدول قرار دهید:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<WorkingWithTables::Column> column = WorkingWithTables::Column::FromIndex(table, 0);
// Print the plain text of the column to the screen.
std::cout << column->ToTxt() << std::endl;
// Create a new column to the left of this column.
// This is the same as using the "Insert Column Before" command in Microsoft Word.
SharedPtr<WorkingWithTables::Column> newColumn = column->InsertColumnBefore();
for (SharedPtr<Cell> cell : newColumn->get_Cells())
{
cell->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, String(u"Column Text ") + newColumn->IndexOf(cell)));
}

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 1, true));
SharedPtr<WorkingWithTables::Column> column = WorkingWithTables::Column::FromIndex(table, 2);
column->Remove();
view raw remove-column.h hosted with ❤ by GitHub

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->StartTable();
builder->get_RowFormat()->set_HeadingFormat(true);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_CellFormat()->set_Width(100);
builder->InsertCell();
builder->Writeln(u"Heading row 1");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Heading row 2");
builder->EndRow();
builder->get_CellFormat()->set_Width(50);
builder->get_ParagraphFormat()->ClearFormatting();
for (int i = 0; i < 50; i++)
{
builder->InsertCell();
builder->get_RowFormat()->set_HeadingFormat(false);
builder->Write(u"Column 1 Text");
builder->InsertCell();
builder->Write(u"Column 2 Text");
builder->EndRow();
}
doc->Save(ArtifactsDir + u"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-C.git.
auto doc = MakeObject<Document>(MyDir + u"Table spanning two pages.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Disable breaking across pages for all rows in the table.
for (const auto& row : System::IterateOver<Row>(table->get_Rows()))
{
row->get_RowFormat()->set_AllowBreakAcrossPages(false);
}
doc->Save(ArtifactsDir + u"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-C.git.
auto doc = MakeObject<Document>(MyDir + u"Table spanning two pages.docx");
auto table = System::ExplicitCast<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 (const auto& cell : System::IterateOver<Cell>(table->GetChildNodes(NodeType::Cell, true)))
{
cell->EnsureMinimum();
for (const auto& para : System::IterateOver<Paragraph>(cell->get_Paragraphs()))
{
if (!(cell->get_ParentRow()->get_IsLastRow() && para->get_IsEndOfCell()))
{
para->get_ParagraphFormat()->set_KeepWithNext(true);
}
}
}
doc->Save(ArtifactsDir + u"WorkingWithTables.KeepTableTogether.docx");