If somebody like the post and its helpful in your work then, add comments.

Thursday, August 6, 2009

WordprocessingML : Insert page number in the word document in center bottom of MS Word Document

Their are 3 things required for the inserting page number;

1. Footer.xml
2. Footnotes.xml
3. EndNotes.xml

And Section Properties to append in the document.

Sample code for that

                Footer footer = new Footer(new Paragraph(new ParagraphProperties(new ParagraphStyleId() { Val = "Footer" })));
                var footerPart1 = mainPart.AddNewPart<FooterPart>("rid110");
                Footer1().Save(footerPart1);
                body.Append(footer);

                Footnotes footnotes = new Footnotes(new Paragraph(new ParagraphProperties(new ParagraphStyleId() { Val = "Footnotes" })));
                var footnodeinpage = mainPart.AddNewPart<FootnotesPart>("rid111");
                PageFootNote().Save(footnodeinpage);
                body.Append(footnotes);

                Endnotes endnotes = new Endnotes(new Paragraph(new ParagraphProperties(new ParagraphStyleId() { Val = "Endnotes" })));
                var endnodeinpage = mainPart.AddNewPart<EndnotesPart>("rid112");
                PageEndNote().Save(endnodeinpage);
                body.Append(endnotes);

                body.Append(PageSectionProperties());


Methods

#region Page Number
private static Endnotes PageEndNote()
{
var element =
new Endnotes(
new Endnote(
new Paragraph(
new ParagraphProperties(
new SpacingBetweenLines() { After = (UInt64Value)0UL, Line = 240, LineRule = LineSpacingRuleValues.Auto }),
new Run(
new SeparatorMark())
) { RsidParagraphAddition = "00670250", RsidParagraphProperties = "00EA1D8B", RsidRunAdditionDefault = "00670250" }
) { Type = FootnoteEndnoteValues.Separator, Id = 0 },
new Endnote(
new Paragraph(
new ParagraphProperties(
new SpacingBetweenLines() { After = (UInt64Value)0UL, Line = 240, LineRule = LineSpacingRuleValues.Auto }),
new Run(
new ContinuationSeparatorMark())
) { RsidParagraphAddition = "00670250", RsidParagraphProperties = "00EA1D8B", RsidRunAdditionDefault = "00670250" }
) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 1 });
return element;
}

private static Footer Footer1()
{
var element =
new Footer(
new SdtBlock(
new SdtProperties(
new SdtId() { Val = 538536024 },
new DocPartObjectSdt(
new DocPartGallery() { Val = "Page Numbers (Bottom of Page)" },
new DocPartUnique())),
new SdtContentBlock(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Footer" },
new Justification() { Val = JustificationValues.Center }),
new SimpleField(
new Run(
new RunProperties(
new NoProof()),
new Text("6") ->Total Page Number in document
) { RsidRunAddition = "00E906EE" }
) { Instruction = " PAGE \\* MERGEFORMAT " }
) { RsidParagraphAddition = "00EA1D8B", RsidRunAdditionDefault = "002E7045" })),
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Footer" })
) { RsidParagraphAddition = "00EA1D8B", RsidRunAdditionDefault = "00EA1D8B" });
return element;
}

private static Footnotes PageFootNote()
{
var element =
new Footnotes(
new Footnote(
new Paragraph(
new ParagraphProperties(
new SpacingBetweenLines() { After = (UInt64Value)0UL, Line = 240, LineRule = LineSpacingRuleValues.Auto }),
new Run(
new SeparatorMark())
) { RsidParagraphAddition = "00670250", RsidParagraphProperties = "00EA1D8B", RsidRunAdditionDefault = "00670250" }
) { Type = FootnoteEndnoteValues.Separator, Id = 0 },
new Footnote(
new Paragraph(
new ParagraphProperties(
new SpacingBetweenLines() { After = (UInt64Value)0UL, Line = 240, LineRule = LineSpacingRuleValues.Auto }),
new Run(
new ContinuationSeparatorMark())
) { RsidParagraphAddition = "00670250", RsidParagraphProperties = "00EA1D8B", RsidRunAdditionDefault = "00670250" }
) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 1 });
return element;
}

//Adding the section properties in the document
public static SectionProperties PageSectionProperties()
{
var element =
new SectionProperties(
new FooterReference() { Type = HeaderFooterValues.Default, Id = "rid110" },
new PageSize() { Width = (UInt64Value)12240UL, Height = (UInt64Value)15840UL },
new PageMargin() { Top = 1440, Right = (UInt64Value)1440UL, Bottom = 1440, Left = (UInt64Value)1440UL, Header = (UInt64Value)720UL, Footer = (UInt64Value)720UL, Gutter = (UInt64Value)0UL },
new Columns() { Space = (UInt64Value)720UL },
new DocGrid() { LinePitch = 360 }
) { RsidRPr = "007024C7", RsidR = "000B6998", RsidSect = "006040EE" };
return element;
}

#endregion

Also find on openxmldeveloper.org Link

1 comment: