Werk Met Afdelings
Soms wil jy’n dokument hê wat nie dieselfde formatering oor alle bladsye het nie. Byvoorbeeld, jy sal dalk bladsynommerformate moet verander, verskillende bladsygrootte en oriëntasie moet hê, of die eerste dokumentbladsy as voorblad moet hê sonder enige nommering. Jy kan dit met afdelings bereik.
Afdelings is vlakknope wat koptekste en voetskrifte, oriëntasie, kolomme, rande, bladsy nommer opmaak, en ander beheer.
Aspose.Words laat jou toe om afdelings te bestuur, ‘n dokument in afdelings te verdeel en formateringsveranderinge aan te bring wat slegs op’n spesifieke afdeling van toepassing is. Aspose.Words stoor inligting oor afdelingformatering soos koptekste en voetskrifte, bladsyopstelling en kolominstellings in die afdelingbreek.
Hierdie artikel verduidelik hoe om met afdelings en afdelingsonderbrekings te werk.
Watter Afdeling En Afdeling Breek Is
Dokument afdelings word verteenwoordig deur die Section en SectionCollection klasse. Seksie voorwerpe is onmiddellike kinders van die Document node en kan verkry word via die Sections eiendom. Jy kan die nodes te bestuur deur die gebruik van’n paar metodes soos Remove, Add, IndexOf, en ander.
Afdeling breek is’n opsie wat dokument bladsye verdeel in afdelings met aanpasbare uitlegte.
Tipes Van’n Afdeling Breek
Aspose.Words laat jou toe om dokumente te verdeel en te formateer deur verskillende afdelingsbreuke van die BreakType opsomming te gebruik:
- SectionBreakContinuous
- SectionBreakNewColumn
- SectionBreakNewPage
- SectionBreakEvenPage
- SectionBreakOddPage
Jy kan ook die SectionStart opsomming gebruik om’n breek tipe wat slegs van toepassing is vir die eerste afdeling soos kies NewColumn, NewPage, EvenPage, en OddPage.
Bestuur’n Afdeling
Aangesien’n gedeelte’n normale saamgestelde knoop is, kan die hele knoopmanipulasie API gebruik word om afdelings te manipuleer: om afdelings by te voeg, te verwyder en ander bewerkings op afdelings te doen. Jy kan meer lees oor nodes in die artikel Aspose.Words Dokument Voorwerp Model (DOM).
Aan die ander kant kan jy ook die DocumentBuilder
API gebruik om met afdelings te werk. In hierdie artikel sal ons fokus op hierdie spesifieke manier van werk met afdelings.
Voeg Of Verwyder’n Gedeelte Breek
Aspose.Words laat jou toe om’n afdeling in te voeg breek in teks met behulp van die InsertBreak metode.
Die volgende kode voorbeeld toon hoe om’n afdeling breek in’n dokument in te voeg:
// 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(); | |
} | |
} | |
} |
Gebruik die Remove metode om’n afdeling breek uit te vee. As u nie’n spesifieke afdelingsonderbreking hoef te verwyder nie en die inhoud van die afdeling uitvee, kan u die ClearContent - metode gebruik.
Die volgende kode voorbeeld toon hoe om afdelings breek te verwyder:
// 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(); | |
} | |
} |
Skuif’n Afdeling
As jy’n afdeling van een posisie na’n ander in jou dokument wil skuif, moet jy die indeks van daardie afdeling kry. Aspose.Words laat jou toe om’n afdeling posisie van a SectionCollection te kry deur die Item eienskap te gebruik. U kan die Sections eienskap gebruik om alle afdelings in u dokument te kry. Maar as jy net die eerste afdeling wil kry, kan jy die FirstSection eienskap gebruik.
Die volgende kode voorbeeld toon hoe om toegang tot die eerste afdeling en iterate deur die kinders van’n saamgestelde knoop:
// 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."); | |
} | |
} | |
} |
Spesifiseer’n Afdeling Uitleg
Soms wil jy hê jou dokument moet beter lyk deur kreatiewe uitlegte vir verskillende dokumentafdelings te maak. As u die tipe van die huidige afdelingrooster wil spesifiseer, kan u’n afdelinguitlegmodus kies deur die SectionLayoutMode - opsomming te gebruik:
- Verstek
- Rooster
- LineGrid
- SnapToChars
Die volgende kode voorbeeld toon hoe om die aantal reëls wat elke bladsy kan hê te beperk:
// 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"); |
Redigeer’n Afdeling
As u’n nuwe afdeling by u dokument voeg, is daar geen liggaam of paragraaf wat u kan wysig nie. Aspose.Words laat jou toe om te waarborg dat’n afdeling’n liggaam met ten minste een paragraaf bevat met behulp van die EnsureMinimum metode – dit sal outomaties’n Liggaam (of HeaderFooter) knoop by die dokument voeg en dan’n Paragraaf daarby voeg.
Die volgende kode voorbeeld toon hoe om’n nuwe afdeling node voor te berei met behulp van 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!")); |
Voeg By Of Prepend Inhoud
As jy’n vorm wil teken of teks of prent aan die begin/einde van’n afdeling wil byvoeg, kan jy die AppendContent en PrependContent metodes van die Section klas gebruik.
Die volgende kode voorbeeld toon hoe om inhoud van’n bestaande afdeling te voeg:
// 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); |
Kloon’n Afdeling
Aspose.Words laat jou toe om’n gedeelte te dupliseer deur’n volledige kopie daarvan te skep met behulp van die Clone metode.
Die volgende kode voorbeeld toon hoe om die eerste afdeling in jou dokument te kloon:
// 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(); |
Kopieer Afdelings tussen Dokumente
In sommige gevalle kan jy groot dokumente met baie afdelings hê en jy wil die inhoud van’n afdeling van een dokument na’n ander kopieer.
Aspose.Words laat jou toe om afdelings tussen dokumente te kopieer deur die ImportNode metode te gebruik.
Die volgende kode voorbeeld toon hoe om afdelings tussen dokumente te kopieer:
// 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"); |
Werk met Afdeling Kop En Voetskrif
Die basiese reëls vir die vertoon van’n koptekst of voetskrif vir elke afdeling is redelik eenvoudig:
- As die afdeling nie sy eie koptekste/voetskrifte van’n sekere tipe het nie, word dit uit die vorige afdeling geneem.
- Die tipe kop/voetskrif wat op die bladsy vertoon word, word beheer deur die “Different First Page” en “Different Odd & Even pages” afdeling instellings as hulle gedeaktiveer is, dan word die afdeling se eie titels geïgnoreer.
Die volgende kode voorbeeld toon hoe om 2 afdelings met verskillende koptekste te skep:
As u die teks van koptekste en voetskrifte wil verwyder sonder om HeaderFooter voorwerpe in u dokument te verwyder, kan u die ClearHeadersFooters - metode gebruik. Daarbenewens kan u die DeleteHeaderFooterShapes - metode gebruik om alle vorms van koptekste en voetskrifte in u dokument te verwyder.
Die volgende kode voorbeeld toon hoe om inhoud van alle koptekste en voetskrifte in’n afdeling skoon te maak:
Die volgende kode voorbeeld hoe om alle vorms van alle koptekste voetstukke in’n afdeling te verwyder:
Pasmaak Bladsy Eienskappe in’n Afdeling
Voordat jy’n bladsy of’n dokument druk, wil jy dalk die grootte en uitleg van’n enkele bladsy of die hele dokument aanpas en verander. Met bladsyopstelling kan u die instellings van dokumentbladsye soos kantlyne, oriëntasie en grootte verander vir die druk van verskillende eerste bladsye of onewe bladsye.
Aspose.Words laat jou toe om bladsy-en afdelingeienskappe aan te pas deur die PageSetup klas te gebruik.
Die volgende kode voorbeeld toon hoe om sulke eienskappe soos bladsy grootte en oriëntasie vir die huidige afdeling stel:
// 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"); |
Die volgende kode voorbeeld toon hoe om die bladsy eienskappe in alle afdelings te verander:
// 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"); |