質問

How to paginate a word document for example

  • if there are 10 pages:

    page 1 of 10

    Page 2 of 10 ...

  • if there are 15 pages

    1 of 15 Page

    2 of 15 ...

and so on to generate a dynamic number of pages

役に立ちましたか?

解決

The Open Xml SDK does NOT provide application behaviors such as layout (ex. pagination of WordprocessingML documents) or recalculation functionality. You can read more @ http://blogs.msdn.com/b/brian_jones/archive/2008/10/06/open-xml-format-sdk-2-0.aspx

他のヒント

It has been three years since you asked your question but maybe I can help other people that are facing this problem.

Here is the code that can paginate a word document.

string documentPath = @"C:\Temp\FooterPOC.docx";

using (WordprocessingDocument package =
    WordprocessingDocument.Create(
    documentPath, WordprocessingDocumentType.Document))
{
    {
        MainDocumentPart objMainDocumentPart = package.AddMainDocumentPart();
        Document objDocument = new Document();
        objMainDocumentPart.Document = objDocument;
        Body objBody = new Body();
        SectionProperties objSectionProperties = new SectionProperties();
        FooterPart objFootPart = objMainDocumentPart.AddNewPart<FooterPart>();
        Footer objFooter = new Footer();
        objFootPart.Footer = objFooter;

        Paragraph objParagraph_1 = new Paragraph();
        ParagraphProperties objParagraphProperties = new ParagraphProperties();
        ParagraphStyleId objParagraphStyleId = new ParagraphStyleId() { Val = "Footer" };

        objParagraphProperties.Append(objParagraphStyleId);
        Justification objJustification = new Justification() { Val = JustificationValues.Right };
        objParagraphProperties.Append(objJustification);
        objParagraph_1.Append(objParagraphProperties);

        Run objRun_1 = new Run();
        Text objText_1 = new Text() { Space = SpaceProcessingModeValues.Preserve };
        objText_1.Text = "Página ";
        objRun_1.Append(objText_1);
        objParagraph_1.Append(objRun_1);

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

        Run objRun_3 = new Run();
        FieldCode objFieldCode_1 = new FieldCode();
        objFieldCode_1.Text = "PAGE";
        objRun_3.Append(objFieldCode_1);
        objParagraph_1.Append(objRun_3);

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

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

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

        Run objRun_9 = new Run();
        FieldCode objFieldCode_2 = new FieldCode();
        objFieldCode_2.Text = "NUMPAGES";
        objRun_9.Append(objFieldCode_2);
        objParagraph_1.Append(objRun_9);

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

        objFooter.Append(objParagraph_1);

        string strFootrID = objMainDocumentPart.GetIdOfPart(objFootPart);
        FooterReference objFooterReference = new FooterReference()
        {
            Type = HeaderFooterValues.Default,
            Id = strFootrID
        };

        objSectionProperties.Append(objFooterReference);
        objBody.Append(objSectionProperties);
        objMainDocumentPart.Document.Append(objBody);
    }
}

I found this code here. It was a little bit buggy but it helped me a lot!

There were a few lines that i thought it was unnecessary so i removed it and worked great, but i'm only a newbie in openXML so maybe they were in fact necessary.

Cheers!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top