簡體   English   中英

如何將頁碼添加到 WordProccesingDocument OpenXml C#

[英]How to add page number to WordProccesingDocument OpenXml C#

公共 static 無效 AddNumeration(參考 WordprocessingDocument finalDoc){

        IEnumerable<FooterPart> foo = finalDoc.MainDocumentPart.FooterParts;

        int count = 1;
        foreach (FooterPart fp in foo){

        Footer f = new Footer();

        ParagraphProperties paragraphProperties1 = new ParagraphProperties(new SectionProperties(new PageNumberType { Start = 1 }));
        paragraphProperties1.NumberingProperties = NumberingProperties
        paragraphProperties1.Justification = new Justification() { Val = JustificationValues.Right };
        Run run1 = new Run();
        Text text1 = new Text();

        text1.Text = count.ToString();
        count++;
        run1.Append(text1);
        Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };

        paragraph1.Append(paragraphProperties1);
        paragraph1.Append(run1);

        f.Append(paragraph1);

        fp.Footer = f;
     }


    }

此代碼始終在每頁的頁腳部分顯示數字 1。

您不需要手動增加頁碼。 嘗試這樣的事情。

FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();
string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);

Footer footer = new Footer(new Paragraph(
                new ParagraphProperties(
                    new ParagraphStyleId() { Val = "Footer" },
                    new Run(
                        new SimpleField(){ Instruction = "PAGE"}))));
footerPart.Footer = footer;
IEnumerable<SectionProperties> sectionProperties =
                        mainDocumentPart.Document.Body.Elements<SectionProperties>();

foreach (var sectionProperty in sectionProperties)
{
     sectionProperty.RemoveAllChildren<FooterReference>();
     sectionProperty.PrependChild<FooterReference>(new FooterReference()
     {
         Id = footerPartId
     });
}

這將添加頁腳並逐頁動態地增加它。

在Google上進行一些搜索: https//janewdaisy.wordpress.com/2012/03/01/insert-footer-in-word-document-cvb-net/

它應該做的工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM