Blogs

Blogs

Open XML Usage - Part Three

3rd November 2014

Part Three

Add a header and footer.

The following code adds a header and footer parts.

When these are added then text and images can appear on each page.

On the Word document output, the line has been added as an image.

It is also possible to set up these headers and footers on differing pages

     i.e. odd or even pages. The style of the page numbering can also be changed

     i.e. instead of a slash the wording “of” could be used.

Please refer to Part One of this series to see the Word document output and calling code.

private void CreateHeader(WordprocessingDocument doc, HeaderPart newHeaderPart)
{
    //// Create a placeholder paragraph - or add any content required
    Header header = new Header();
    ////Run run = new Run();
    ////Text text = new Text();
    ////text.Text = string.Empty;

    //// Add a line image
    string lineFileName1 = HttpContext.Current.Server.MapPath(@"/Images/thinline4.png");
    ImagePart imagePart1 = newHeaderPart.AddImagePart(ImagePartType.Png);
    using (FileStream stream = new FileStream(lineFileName1, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        imagePart1.FeedData(stream);
        stream.Dispose();
    }

    Paragraph paraFooter = new Paragraph();
    WordDocument.AddImageToParagraph(paraFooter, newHeaderPart.GetIdOfPart(imagePart1), lineFileName1, JustificationValues.Right);

    ////run.Append(text);
    ////paraFooter.Append(run);
    header.Append(paraFooter);
    newHeaderPart.Header = header;
}

private static void CreateFooter(MainDocumentPart objMainDocumentPart, string footerText, JustificationValues justifyPageNumbers, FooterPart newFooterPart)
{
    Footer objFooter = new Footer();
    newFooterPart.Footer = objFooter;

    SdtBlock objSdtBlock_1 = new SdtBlock();
    SdtContentBlock objSdtContentBlock_1 = new SdtContentBlock();
    SdtBlock objSdtBlock_2 = new SdtBlock();
    SdtContentBlock objSdtContentBlock_2 = new SdtContentBlock();

    Paragraph objParagraph_1 = new Paragraph();
    ParagraphProperties objParagraphProperties = new ParagraphProperties();
    ParagraphStyleId objParagraphStyleId = new ParagraphStyleId() { Val = "Footer" };
    objParagraphProperties.Append(objParagraphStyleId);
    Justification objJustification = new Justification() { Val = justifyPageNumbers };
    objParagraphProperties.Append(objJustification);
    objParagraph_1.Append(objParagraphProperties);

    Run objRun_1 = new Run(new Text() { Text = "Page ", Space = SpaceProcessingModeValues.Preserve });
    objParagraph_1.Append(objRun_1);

    Run objRun_2 = new Run(new FieldChar() { FieldCharType = FieldCharValues.Begin });
    objParagraph_1.Append(objRun_2);

    Run objRun_3 = new Run(new FieldCode() { Space = SpaceProcessingModeValues.Preserve, Text = "PAGE " });
    objParagraph_1.Append(objRun_3);

    Run objRun_4 = new Run(new FieldChar() { FieldCharType = FieldCharValues.Separate });
    objParagraph_1.Append(objRun_4);
    Run objRun_5 = new Run(new Text() { Text = "2 " });
    objParagraph_1.Append(objRun_5);

    Run objRun_6 = new Run(new FieldChar() { FieldCharType = FieldCharValues.End });
    objParagraph_1.Append(objRun_6);

    Run objRun_7 = new Run(new Text() { Text = "/", Space = SpaceProcessingModeValues.Preserve });
    objParagraph_1.Append(objRun_7);

    Run objRun_8 = new Run(new FieldChar() { FieldCharType = FieldCharValues.Begin });
    objParagraph_1.Append(objRun_8);

    Run objRun_9 = new Run(new FieldCode() { Space = SpaceProcessingModeValues.Preserve, Text = "NUMPAGES  " });
    objParagraph_1.Append(objRun_9);

    Run objRun_10 = new Run(new FieldChar() { FieldCharType = FieldCharValues.Separate });
    objParagraph_1.Append(objRun_10);

    Run objRun_11 = new Run(new Text() { Text = "2 " });
    objParagraph_1.Append(objRun_11);

    Run objRun_12 = new Run(new FieldChar() { FieldCharType = FieldCharValues.End });
    objParagraph_1.Append(objRun_12);

    //// Add a line image
    string lineFileName1 = HttpContext.Current.Server.MapPath(@"/Images/thinline4.png");
    ImagePart imagePart1 = newFooterPart.AddImagePart(ImagePartType.Png);
    using (FileStream stream = new FileStream(lineFileName1, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        imagePart1.FeedData(stream);
        stream.Dispose();
    }

    Paragraph objParagraph_2 = new Paragraph();
    WordDocument.AddImageToParagraph(objParagraph_2, newFooterPart.GetIdOfPart(imagePart1), lineFileName1, JustificationValues.Right);

    //// Add the footer text above below line and style it
    Run objRun_13 = new Run(new Text() { Text = footerText });
    objRun_13.RunProperties = new RunProperties() { RunStyle = new RunStyle() { Val = "FooterText" } };
    objParagraph_2.Append(objRun_13);

    objSdtContentBlock_2.Append(objParagraph_1);
    objSdtContentBlock_2.Append(objParagraph_2);

    objSdtBlock_2.Append(objSdtContentBlock_2);
    objSdtContentBlock_1.Append(objSdtBlock_2);
    objSdtBlock_1.Append(objSdtContentBlock_1);
    objFooter.Append(objSdtBlock_1);

    //// Create the footer reference
    string strFootrID = objMainDocumentPart.GetIdOfPart(newFooterPart);
    FooterReference objFooterReference = new FooterReference()
    {
        Type = HeaderFooterValues.Default,
        Id = strFootrID
    };

    //// Add the footer reference in this way otherwise the footer does not show
    SectionProperties sectionProps = objMainDocumentPart.Document.Body.Elements<SectionProperties>().LastOrDefault();
    if (sectionProps == null)
    {
        sectionProps = new SectionProperties();
        objMainDocumentPart.Document.Body.Append(sectionProps);
    }

    sectionProps.RemoveAllChildren<FooterReference>();
    sectionProps.Append(objFooterReference);

    DocumentSettingsPart objDocumentSettingPart = objMainDocumentPart.AddNewPart<DocumentSettingsPart>();
    objDocumentSettingPart.Settings = new Settings();
    Compatibility objCompatibility = new Compatibility();
    CompatibilitySetting objCompatibilitySetting =
        new CompatibilitySetting()
        {
            Name = CompatSettingNameValues.CompatibilityMode,
            Uri = "http://schemas.microsoft.com/office/word",
            Val = "14"
        };
    objCompatibility.Append(objCompatibilitySetting);
    objDocumentSettingPart.Settings.Append(objCompatibility);
}
This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.