עבודה עם סעיפים
לפעמים אתה רוצה מסמך שאין לו אותו פורמט בכל הדפים. לדוגמה, ייתכן שיהיה עליך לשנות את פורמטי מספר העמוד, יש גודל וכיוון שונים, או שיש לך את דף המסמך הראשון כעמוד כיסוי ללא מספר. אתה יכול להשיג את זה עם חלקים.
סעיפים הם בלוטות רמות השולטות ראשים והולכי רגל, אוריינטציה, עמודות, שוליים, שוליים, מספר העמודים פורמט, ואחרים.
Aspose.Words מאפשר לך לנהל חלקים, לחלק מסמך לחלקים, ולבצע שינויים פורמט החל רק על סעיף מסוים. Aspose.Words מאחסנת מידע על פורמט סעיף כגון Headers and Footers, דף ההתקנה והגדרות עמודה בפסקה.
מאמר זה מסביר כיצד לעבוד עם חלקים וקטעים.
איזה סעיף ופסקה הם
סעיפים של מסמכים מיוצגים על ידי Section ו SectionCollection שיעורים. חפצים הם ילדים מיידיים של Document צומת וניתן לגשת אליו באמצעות Sections רכוש. אתה יכול לנהל את הנקודות האלה באמצעות כמה שיטות כגון Remove, Add, IndexOf, ואחרים.
שבר סעיף הוא אופציה המחלקת דפים מסמך לסעיפים עם פריסות מותאמות אישית.
סוגים של הפסקה
Aspose.Words מאפשר להפיץ ולעצב מסמכים באמצעות הפסקות חלקים שונות של BreakType המונחים:
- סעיףBreakContinense
- תגית: Breaking NewColumn
- תגית:BreakNewPage
- תגית:Break EvenPage
- פרק:BreakOdPage
אתה יכול גם להשתמש SectionStart בהנחה לבחור סוג הפסקה שחל רק על החלק הראשון כגון NewColumn, NewPage, EvenPage, ו- OddPage.
נהל סעיף
מכיוון שסעיף הוא צומת מורכב רגיל, כל מניפולציה הצומת API ניתן להשתמש כדי לתמרן חלקים: להוסיף, להסיר ופעולות אחרות על חלקים. ניתן לקרוא עוד על צמתים במאמר Aspose.Words Document Object Model (DOM).
מצד שני, אתה יכול גם להשתמש DocumentBuilder
API לעבוד עם חלקים במאמר זה נתמקד בדרך מסוימת זו של עבודה עם חלקים.
הוסף או להסיר פסקה
Aspose.Words מאפשר להוסיף קטע לפרוץ לטקסט באמצעות InsertBreak שיטה.
הדוגמה הבאה של הקוד מראה כיצד להכניס חלק לתוך מסמך:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
/// <summary> | |
/// Insert section breaks before the specified paragraphs. | |
/// </summary> | |
private void InsertSectionBreaks(List<Paragraph> topicStartParas) | |
{ | |
DocumentBuilder builder = new DocumentBuilder(mDoc); | |
foreach (Paragraph para in topicStartParas) | |
{ | |
Section section = para.ParentSection; | |
// Insert section break if the paragraph is not at the beginning of a section already. | |
if (para != section.Body.FirstParagraph) | |
{ | |
builder.MoveTo(para.FirstChild); | |
builder.InsertBreak(BreakType.SectionBreakNewPage); | |
// This is the paragraph that was inserted at the end of the now old section. | |
// We don't really need the extra paragraph, we just needed the section. | |
section.Body.LastParagraph.Remove(); | |
} | |
} | |
} |
השתמש Remove שיטה למחוק הפסקה. אם אתה לא צריך להסיר הפסקה מסוימת ובמקום זאת למחוק את התוכן של סעיף זה, אתה יכול להשתמש ClearContent שיטה.
דוגמה לקוד הבא מראה כיצד להסיר הפסקות:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
private void RemoveSectionBreaks(Document doc) | |
{ | |
// Loop through all sections starting from the section that precedes the last one and moving to the first section. | |
for (int i = doc.Sections.Count - 2; i >= 0; i--) | |
{ | |
// Copy the content of the current section to the beginning of the last section. | |
doc.LastSection.PrependContent(doc.Sections[i]); | |
// Remove the copied section. | |
doc.Sections[i].Remove(); | |
} | |
} |
העברת סעיף
אם אתה רוצה להעביר קטע ממקום אחד למשנהו במסמך שלך, אתה צריך לקבל את המדד של סעיף זה. Aspose.Words מאפשר לך לקבל מיקום חלק מ SectionCollection באמצעות שימוש Item רכוש. אתה יכול להשתמש Sections לרכוש את כל החלקים במסמך שלך. אם אתה רוצה לקבל רק את החלק הראשון, אתה יכול להשתמש FirstSection רכוש.
הדוגמה הבאה של הקוד מראה כיצד לגשת לחלק הראשון ולהתפתל דרך הילדים של צומת מורכב:
// 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.Write("Section 1"); | |
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); | |
builder.Write("Primary header"); | |
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); | |
builder.Write("Primary footer"); | |
Section section = doc.FirstSection; | |
// A Section is a composite node and can contain child nodes, | |
// but only if those child nodes are of a "Body" or "HeaderFooter" node type. | |
foreach (Node node in section) | |
{ | |
switch (node.NodeType) | |
{ | |
case NodeType.Body: | |
{ | |
Body body = (Body)node; | |
Console.WriteLine("Body:"); | |
Console.WriteLine($"\t\"{body.GetText().Trim()}\""); | |
break; | |
} | |
case NodeType.HeaderFooter: | |
{ | |
HeaderFooter headerFooter = (HeaderFooter)node; | |
Console.WriteLine($"HeaderFooter type: {headerFooter.HeaderFooterType}:"); | |
Console.WriteLine($"\t\"{headerFooter.GetText().Trim()}\""); | |
break; | |
} | |
default: | |
{ | |
throw new Exception("Unexpected node type in a section."); | |
} | |
} | |
} |
המונחים: a part Layout
לפעמים אתה רוצה שהמסמך שלך ייראה טוב יותר על ידי ביצוע פריסות יצירתיות עבור קטעי מסמך שונים. אם ברצונך לציין את סוג רשת הסעיף הנוכחית, באפשרותך לבחור מצב פריסת חלק באמצעות טופס SectionLayoutMode המונחים:
- Default
- גרי
- LineGrid
- SnapToCharles
הדוגמה הבאה של הקוד מראה כיצד להגביל את מספר השורות שיש לכל דף:
// 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); | |
// Enable pitching, and then use it to set the number of lines per page in this section. | |
// A large enough font size will push some lines down onto the next page to avoid overlapping characters. | |
builder.PageSetup.LayoutMode = SectionLayoutMode.LineGrid; | |
builder.PageSetup.LinesPerPage = 15; | |
builder.ParagraphFormat.SnapToGrid = true; | |
for (int i = 0; i < 30; i++) | |
builder.Write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "); | |
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.LinesPerPage.docx"); |
לערוך סעיף
כאשר אתה מוסיף סעיף חדש במסמך שלך, לא יהיה גוף או סעיף שניתן לערוך. Aspose.Words מאפשר לך להבטיח כי סעיף מכיל גוף עם לפחות פסקה אחת. EnsureMinimum שיטה – היא תוסיף באופן אוטומטי גוף (או HeaderFooter) צומת למסמך ולאחר מכן תוסיף סעיף לכך.
הדוגמה הבאה של הקוד מראה כיצד להכין פסקה חדשה באמצעות שימוש EnsureMinimum:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
// If we add a new section like this, it will not have a body, or any other child nodes. | |
doc.Sections.Add(new Section(doc)); | |
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it. | |
doc.LastSection.EnsureMinimum(); | |
doc.Sections[0].Body.FirstParagraph.AppendChild(new Run(doc, "Hello world!")); |
תוכן מראש או Prepend
אם אתה רוצה לצייר צורה או להוסיף טקסט או תמונה בתחילת / סיום של סעיף, אתה יכול להשתמש AppendContent ו PrependContent שיטות של Section מעמד.
הדוגמה הבאה של הקוד מראה כיצד לתקן תוכן של סעיף קיים:
// 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.Write("Section 1"); | |
builder.InsertBreak(BreakType.SectionBreakNewPage); | |
builder.Write("Section 2"); | |
builder.InsertBreak(BreakType.SectionBreakNewPage); | |
builder.Write("Section 3"); | |
Section section = doc.Sections[2]; | |
// Insert the contents of the first section to the beginning of the third section. | |
Section sectionToPrepend = doc.Sections[0]; | |
section.PrependContent(sectionToPrepend); | |
// Insert the contents of the second section to the end of the third section. | |
Section sectionToAppend = doc.Sections[1]; | |
section.AppendContent(sectionToAppend); |
סימן לסעיף
Aspose.Words מאפשר לשכפל סעיף על ידי יצירת עותק מלא של זה באמצעות Clone שיטה.
הדוגמה הבאה של הקוד מראה כיצד לשחזר את הקטע הראשון במסמך שלך:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Document.docx"); | |
Section cloneSection = doc.Sections[0].Clone(); |
העתק בין מסמכים
במקרים מסוימים, ייתכן שיש לך מסמכים גדולים עם חלקים רבים ואתה רוצה להעתיק את התוכן של סעיף מ מסמך אחד למשנהו.
Aspose.Words מאפשר לך להעתיק חלקים בין מסמכים באמצעות ImportNode שיטה.
דוגמה לקוד הבא מראה כיצד להעתיק קטעים בין מסמכים:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document srcDoc = new Document(MyDir + "Document.docx"); | |
Document dstDoc = new Document(); | |
Section sourceSection = srcDoc.Sections[0]; | |
Section newSection = (Section)dstDoc.ImportNode(sourceSection, true); | |
dstDoc.Sections.Add(newSection); | |
dstDoc.Save(ArtifactsDir + "WorkingWithSection.CopySection.docx"); |
עבודה עם מדורה Header and Footer
הכללים הבסיסיים להצגת ראש או רגל לכל חלק הם פשוטים למדי:
1.1 1. אם לסעיף אין ראשים/מרגלים משלו מסוג מסוים, הוא נלקח מהחלק הקודם. 2. הסוג של Header/footer המוצג בעמוד נשלט על ידי “העמוד הראשון” ו-“Different Odd & Even Pages” הגדרות - אם הן מוגבלות, אז הכותרות של הקטע תעלמו.
לדוגמה הקוד הבא מראה כיצד ליצור 2 חלקים עם ראשים שונים:
אם אתה רוצה להסיר את הטקסט של ראשים והולכי רגל מבלי להסיר HeaderFooter אובייקטים במסמך שלך, אתה יכול להשתמש ClearHeadersFooters שיטה. בנוסף, אתה יכול להשתמש DeleteHeaderFooterShapes שיטה להסיר את כל הצורות של Headers ו Footers במסמך שלך.
הדוגמה הבאה של הקוד מראה כיצד לנקות את התוכן של כל הראשים וההולכי הרגל בסעיף:
הדוגמה הבאה של הקוד כיצד להסיר את כל הצורות מכל ההולכי רגל בראש בסעיף:
Customize Page Properties in a Part
לפני הדפסה דף או מסמך ייתכן שתרצה להתאים אישית ולשנות את הגודל ואת הפריסה של דף אחד או את המסמך כולו. עם הגדרת דף, באפשרותך לשנות את ההגדרות של דפי מסמך כגון שולי, אוריינטציה וגודל להדפסה של דפים ראשונים שונים או דפים מוזרים.
Aspose.Words מאפשר לך להתאים אישית דף ותכונות סעיף באמצעות PageSetup מעמד.
דוגמה הקוד הבא מראה כיצד להגדיר תכונות כגון גודל דף וכיוון לסעיף הנוכחי:
// 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.PageSetup.Orientation = Orientation.Landscape; | |
builder.PageSetup.LeftMargin = 50; | |
builder.PageSetup.PaperSize = PaperSize.Paper10x14; | |
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.PageSetupAndSectionFormatting.docx"); |
הדוגמה הבאה של הקוד מראה כיצד לשנות את תכונות העמוד בכל הסעיפים:
// 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.Writeln("Section 1"); | |
doc.AppendChild(new Section(doc)); | |
builder.Writeln("Section 2"); | |
doc.AppendChild(new Section(doc)); | |
builder.Writeln("Section 3"); | |
doc.AppendChild(new Section(doc)); | |
builder.Writeln("Section 4"); | |
// It is important to understand that a document can contain many sections, | |
// and each section has its page setup. In this case, we want to modify them all. | |
foreach (Section section in doc) | |
section.PageSetup.PaperSize = PaperSize.Letter; | |
doc.Save(ArtifactsDir + "WorkingWithSection.ModifyPageSetupInAllSections.doc"); |