החל סגנון טבלה
סגנון טבלה מגדיר קבוצה של עיצוב שניתן להחיל בקלות על טבלה. ניתן להגדיר עיצוב כגון גבולות, הצללה, יישור וגופן בסגנון טבלה ולהחיל אותו על טבלאות רבות לקבלת מראה עקבי.
Aspose.Words תומך החלת שולחן סגנון לשולחן וגם קריאת מאפיינים של כל שולחן סגנון. סגנונות טבלה נשמרים במהלך טעינה ושמירה בדרכים הבאות:
- סגנונות טבלה בפורמטים DOCX ו - WordML נשמרים בעת טעינה ושמירה בפורמטים אלה
- סגנונות טבלה נשמרים בעת טעינה ושמירה בפורמט DOC (אך לא בשום פורמט אחר)
- בעת ייצוא לפורמטים אחרים, עיבוד או הדפסה, סגנונות טבלה מורחבים לעיצוב ישיר בטבלה, כך שכל העיצוב נשמר
צור סגנון טבלה
המשתמש יכול ליצור סגנון חדש ולהוסיף אותו לאוסף סגנון. שיטת Add משמשת ליצירת סגנון טבלה חדש.
דוגמת הקוד הבאה מראה כיצד ליצור סגנון טבלה חדש המוגדר על ידי המשתמש:
// 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); | |
SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
builder->Write(u"Name"); | |
builder->InsertCell(); | |
builder->Write(u"Value"); | |
builder->EndRow(); | |
builder->InsertCell(); | |
builder->InsertCell(); | |
builder->EndTable(); | |
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1")); | |
tableStyle->get_Borders()->set_LineStyle(LineStyle::Double); | |
tableStyle->get_Borders()->set_LineWidth(1); | |
tableStyle->set_LeftPadding(18); | |
tableStyle->set_RightPadding(18); | |
tableStyle->set_TopPadding(12); | |
tableStyle->set_BottomPadding(12); | |
table->set_Style(tableStyle); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.CreateTableStyle.docx"); |
העתק סגנון טבלה קיים
במידת הצורך, תוכל להעתיק סגנון טבלה שכבר קיים במסמך מסוים לאוסף הסגנונות שלך בשיטת AddCopy
.
חשוב לדעת שעם העתקה זו מועתקים גם הסגנונות המקושרים.
דוגמת הקוד הבאה מראה כיצד לייבא סגנון ממסמך אחד למסמך אחר:
EXAMPLE
החל סגנון טבלה קיים
Aspose.Words מספק TableStyle שירש מהכיתה Style. TableStyle מאפשר למשתמש להחיל אפשרויות סגנון שונות כמו הצללה, ריפוד, כניסה, CellSpacing ו Font וכו'.
בנוסף, Aspose.Words מספק את הכיתה StyleCollection וכמה מאפיינים של הכיתה Table
כדי לציין עם איזה סגנון טבלה נעבוד: Style, StyleIdentifier, StyleName, ו - StyleOptions.
Aspose.Words מספק גם ConditionalStyle מחלקה המייצגת עיצוב מיוחד המיושם על אזור כלשהו בטבלה עם סגנון טבלה שהוקצה, ו ConditionalStyleCollection המייצג אוסף של ConditionalStyle אובייקטים. אוסף זה מכיל קבוצה קבועה של פריטים המייצגים פריט אחד עבור כל ערך של סוג הספירה ConditionalStyleType. הספירה ConditionalStyleType מגדירה את כל אזורי הטבלה האפשריים שאליהם ניתן להגדיר עיצוב מותנה בסגנון טבלה.
במקרה זה, ניתן להגדיר עיצוב מותנה עבור כל אזור הטבלה האפשרי המוגדר תחת סוג הספירה ConditionalStyleType.
דוגמת הקוד הבאה מראה כיצד להגדיר עיצוב מותנה עבור שורת הכותרת של הטבלה:
// 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); | |
SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
builder->Write(u"Name"); | |
builder->InsertCell(); | |
builder->Write(u"Value"); | |
builder->EndRow(); | |
builder->InsertCell(); | |
builder->InsertCell(); | |
builder->EndTable(); | |
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1")); | |
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_GreenYellow()); | |
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_Texture(TextureIndex::TextureNone); | |
table->set_Style(tableStyle); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx"); |
אתה יכול גם לבחור לאילו חלקי טבלה להחיל סגנונות, כגון עמודה ראשונה, עמודה אחרונה, שורות פסים. הם מופיעים בספירה TableStyleOptions והם מיושמים דרך המאפיין StyleOptions. הספירה TableStyleOptions מאפשרת שילוב ביטי של תכונות אלה.
דוגמת הקוד הבאה מראה כיצד ליצור טבלה חדשה עם סגנון טבלה מיושם:
// 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); | |
SharedPtr<Table> table = builder->StartTable(); | |
// We must insert at least one row first before setting any table formatting. | |
builder->InsertCell(); | |
// Set the table style used based on the unique style identifier. | |
table->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1); | |
// Apply which features should be formatted by the style. | |
table->set_StyleOptions(TableStyleOptions::FirstColumn | TableStyleOptions::RowBands | TableStyleOptions::FirstRow); | |
table->AutoFit(AutoFitBehavior::AutoFitToContents); | |
builder->Writeln(u"Item"); | |
builder->get_CellFormat()->set_RightPadding(40); | |
builder->InsertCell(); | |
builder->Writeln(u"Quantity (kg)"); | |
builder->EndRow(); | |
builder->InsertCell(); | |
builder->Writeln(u"Apples"); | |
builder->InsertCell(); | |
builder->Writeln(u"20"); | |
builder->EndRow(); | |
builder->InsertCell(); | |
builder->Writeln(u"Bananas"); | |
builder->InsertCell(); | |
builder->Writeln(u"40"); | |
builder->EndRow(); | |
builder->InsertCell(); | |
builder->Writeln(u"Carrots"); | |
builder->InsertCell(); | |
builder->Writeln(u"50"); | |
builder->EndRow(); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx"); |
התמונות למטה מראות ייצוג של Table Styles ב Microsoft Word והמאפיינים המתאימים שלהם ב Aspose.Words.
קח עיצוב מסגנון הטבלה והחל אותו כעיצוב ישיר
Aspose.Words מספק גם את שיטת ExpandTableStylesToDirectFormatting לקחת עיצוב שנמצא על סגנון טבלה ומרחיב אותו על השורות והתאים של הטבלה כעיצוב ישיר. נסה לשלב עיצוב עם סגנון טבלה וסגנון תא.
דוגמת הקוד הבאה מראה כיצד להרחיב את העיצוב מסגנונות לשורות ותאים בטבלה כעיצוב ישיר:
// 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"); | |
// Get the first cell of the first table in the document. | |
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell(); | |
// First print the color of the cell shading. | |
// This should be empty as the current shading is stored in the table style. | |
System::Drawing::Color cellShadingBefore = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor(); | |
std::cout << (String(u"Cell shading before style expansion: ") + cellShadingBefore) << std::endl; | |
doc->ExpandTableStylesToDirectFormatting(); | |
// Now print the cell shading after expanding table styles. | |
// A blue background pattern color should have been applied from the table style. | |
System::Drawing::Color cellShadingAfter = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor(); | |
std::cout << (String(u"Cell shading after style expansion: ") + cellShadingAfter) << std::endl; |