עבודה עם עמודות ושורות
לקבלת שליטה רבה יותר על אופן הפעולה של טבלאות, למד כיצד לתפעל עמודות ושורות.
מצא את אינדקס רכיבי הטבלה
עמודות, שורות ותאים מנוהלים על ידי גישה לצומת המסמך שנבחר על ידי האינדקס שלו. מציאת האינדקס של כל צומת כרוכה באיסוף כל הצמתים של סוג האלמנט מהצומת האם, ולאחר מכן שימוש בשיטת 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 (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; | |
} | |
}; |
דוגמת הקוד הבאה מראה כיצד להכניס עמודה ריקה לטבלה:
// 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(); |
ציין שורות כשורות כותרת
אתה יכול לבחור לחזור על השורה הראשונה בטבלה כשורת הכותרת רק בעמוד הראשון או בכל עמוד אם הטבלה מחולקת למספר. ב 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 להיות נכון עבור כל פסקה בתאי הטבלה. היוצא מן הכלל הוא הפסקה האחרונה בטבלה, אשר צריך להיות מוגדר שקר.
דוגמת הקוד הבאה מראה כיצד להגדיר טבלה כדי להישאר יחד באותו דף:
// 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"); |