PageFieldOpts.cs
- //
- // This code is part of Document Solutions for Word demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Collections.Generic;
- using System.Linq;
- using System.Globalization;
- using GrapeCity.Documents.Word;
- using GrapeCity.Documents.Word.Fields;
-
- namespace DsWordWeb.Demos
- {
- // This sample shows how to use the PAGE and NUMPAGES field options
- // to insert the current and total page numbers into the document
- // and customize the fields' appearance.
- public class PageFieldOpts
- {
- public GcWordDocument CreateDocx()
- {
- var doc = new GcWordDocument();
-
- // The PageFieldOptions and NumPagesFieldOptions classes provide
- // convenient strong-typed access to options specific to the
- // 'PAGE' and 'NUMPAGES' MS Word fields respectively.
-
- // PAGE field options for the page header:
- var pgOptHeader = new PageFieldOptions(doc);
- pgOptHeader.NumberStyle = NumberStyle.Decimal;
-
- // PAGE field options for page numbers in the body:
- var pgOptBody = new PageFieldOptions(doc);
- pgOptBody.NumberStyle = NumberStyle.UpperRoman;
-
- // NUMPAGES field options:
- var numpagesOpt = new NumPagesFieldOptions(doc);
- numpagesOpt.NumberStyle = NumberStyle.Decimal;
-
- // Add a page header that includes the current and total page numbers:
- var para = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph("Page header - page ");
- para.AddComplexField(pgOptHeader);
- para.AddRun(" of ");
- para.AddComplexField(numpagesOpt);
-
- // Pick a style that will be used for page numbers in the document body:
- var pageStyle = doc.Styles[BuiltInStyleId.IndexHeading];
-
- // Add some pages, with periodic inclusions of the PAGE field:
- var rnd = Util.NewRandom();
- for (int i = 0; i < rnd.Next(6, 12); i++)
- {
- var p = doc.Body.AddParagraph("This text is on page ", pageStyle);
- p.AddComplexField(pgOptBody);
- p.AddRun(" of ");
- p.AddComplexField(numpagesOpt);
- p.GetRange().Runs.Add(".");
- var par = Util.LoremIpsumPar();
- doc.Body.AddParagraph(par);
- }
-
- // Update fields using a specific culture:
- doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
-
- // Done:
- return doc;
- }
- }
- }
-