[]
GcWord 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. GcWord 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, GcWord 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:
Access the page setup properties using PageSetup property of the Section class.
Set the page size properties. For example, set the orientation of the page using the Orientation property and paper size using the PaperSize property.
Set the page borders of the section using the Borders property and apply border to the pages using the Border class properties.
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:
Access the page setup properties using PageSetup property of the Section class.
Set the page numbering properties. For example, set the starting page number using the StartingNumber property and the page number format using NumberStyle property of the PageNumbering class.This displays the page numbering when you click on the scroll handle, which is the default behavior. However, if you want to display the page number at the bottom of the page, you can insert a footer in the page and use a simple field in it.
Append a footer using the Footers property and add a paragraph to it using the Add method.
Add a simple field in the paragraph using Add method of the SimpleFieldCollection class.
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:
Access a paragraph in a section. For example, access first paragraph of the first section.
Add a page break on the desired location in the paragraph using AddBreak method of the TextCollection class. For example, add the page break after first run of the first paragraph.
Set the break type to Page using the BreakType enum.
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:
Get a list of all the breaks of type Page from the document.
Remove a page break using the Delete method. For example, remove the first page break.
//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();
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:
Access the options to print the multiple page document using MultiPagePrinting property of the PageSetup class.
Set the type for document printing using Type property of the MultiPagePrinting class.
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 GcWord, see GcWord sample browser.