DsWord stores all the page setup attributes, such as page borders, size, margins, etc., as properties in the PageSetup class. These properties control the structure and layout of pages in a word document. DsWord also allows you to insert a page break which is especially required in case of a long document using Type property of the Break class. The Type property takes Page as a value from BreakType enumeration for specifying a page break. Moreover, DsWord lets you specify how a document is printed using Type property of the MultiPagePrinting class. The Type property takes the value from MultiPagePrintingType enumeration, so that the printed document can be bound as a booklet.
To set the page properties:
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); doc.Load("SampleDoc.docx"); Section first = doc.Body.Sections.First; first.PageSetup.Size.Orientation = PageOrientation.Landscape; first.PageSetup.Size.PaperSize = PaperSize.PaperLetter; first.PageSetup.Borders.AppliesTo = PageBorderAppliesTo.AllPages; first.PageSetup.Borders.Left.LineStyle = LineStyle.BabyPacifier; first.PageSetup.Borders.Right.LineStyle = LineStyle.BabyPacifier; first.PageSetup.Borders.AlwaysInFront = true; doc.Save("SetPageProperties.docx"); |
To set the page numbering:
C# |
Copy Code |
---|---|
doc.Load("SampleDoc.docx"); Section first = doc.Body.Sections.First; //Set page numbering first.PageSetup.PageNumbering.StartingNumber = 3; first.PageSetup.PageNumbering.NumberStyle = NumberStyle.NumberInDash; first.Footers[HeaderFooterType.Primary].Body.Paragraphs.Add(""); first.Footers[HeaderFooterType.Primary].Body.Paragraphs.First.GetRange().SimpleFields.Add("PAGE"); //Save the document doc.Save("PageNumbering.docx"); |
To insert page break in a Word document:
C# |
Copy Code |
---|---|
doc.Load("SampleDoc.docx"); //Access the first paragraph of the first section Section first = doc.Body.Sections.First; Paragraph p1 = first.GetRange().Paragraphs.First; //Insert page break Break br1; br1 = p1.GetRange().Runs.First.GetRange().Texts.AddBreak(BreakType.Page); //Save the document doc.Save("SampleDoc_PageBreak.docx"); |
To remove page break:
C# |
Copy Code |
---|---|
//Load the document doc.Load("SampleDoc_PageBreak.docx"); //Get a list of all the breaks of type "Page" that exist in the document var breaks = new List<Break>(); foreach (var text in doc.Body.Texts) { Break x = text as Break; if (x != null && x.Type == BreakType.Page) breaks.Add(x); } //Remove the first page break breaks[0].Delete(); //Save the document doc.Save("SampleDoc_NoPageBreak.docx"); |
To print a multiple page document:
C# |
Copy Code |
---|---|
doc.Load("SampleDoc.docx"); //Two out of the three sheets in the document will be printed on one page doc.Body.Sections.First.PageSetup.MultiPagePrinting.Type = MultiPagePrintingType.TwoPagesPerSheet; doc.Save("MultiPagePrinting.docx"); |
For more information on how to work with pages and page breaks using DsWord, see DsWord sample browser.