如何使用openxml添加分节符下一页?

时间:2013-11-18 05:19:34

标签: c# ms-word openxml

我想在文档的末尾添加一个分节符并添加一些文本。

我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace WordDocManipulation
{
    class Program
    {
        static void Main(string[] args)
        {

            string path = @"C:\sample.docx";
            string strtxt = "Hello This is done by programmatically";      

           OpenAndAddTextToWordDocument(path,strtxt);
        }
        public static void OpenAndAddTextToWordDocument(string filepath, string txt)
        {
            /* I want to the below text to be added in the new section */ 

            // Open a WordprocessingDocument for editing using the filepath.
            WordprocessingDocument wordprocessingDocument =
                WordprocessingDocument.Open(filepath, true);

            // Assign a reference to the existing document body.
            Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

            // Add new text.
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text(txt));

            // Close the handle explicitly.
            wordprocessingDocument.Close();
        }
    }
}

我该怎么办?

3 个答案:

答案 0 :(得分:9)

您需要将分节符添加到节属性中。然后,您需要将section属性附加到段落属性。然后将段落属性附加到段落。

        Paragraph paragraph232 = new Paragraph();

        ParagraphProperties paragraphProperties220 = new ParagraphProperties();

        SectionProperties sectionProperties1 = new SectionProperties();
        SectionType sectionType1 = new SectionType(){ Val = SectionMarkValues.NextPage };

        sectionProperties1.Append(sectionType1);

        paragraphProperties220.Append(sectionProperties1);

        paragraph232.Append(paragraphProperties220);

生成的Open XML是:

  <w:p>
    <w:pPr>
      <w:sectPr>
        <w:type w:val="nextPage" />
      </w:sectPr>
    </w:pPr>
  </w:p>

如果您创建一个看起来像希望看到结果的Word文档,然后在Open XML Productivity Tool中打开它,您可以反映代码并查看C#代码将生成各种Open XML元素正在努力实现。

答案 1 :(得分:1)

首先,您需要创建一个中断段落

Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page }));

然后你需要附上段落

wordprocessingDocument.MainDocumentPart.Document.Body.Append(PageBreakParagraph)

如果您不想使用InsertAfter和InsertBefore方法将其追加到最后,您还可以指定插入位置

wordprocessingDocument.MainDocumentPart.Document.Body.InsertAfter(PageBreakParagraph, ReferenceElement);
wordprocessingDocument.MainDocumentPart.Document.Body.InsertBefore(PageBreakParagraph, ReferenceElement);

编辑:

这会增加分页符而不是分节符。

答案 2 :(得分:0)

这将在页面末尾添加分节符

private static void AddSectionBreakToTheDocument(string fileName)
{
    using (WordprocessingDocument mydoc = WordprocessingDocument.Open(fileName, true))
    {
        MainDocumentPart myMainPart = mydoc.MainDocumentPart;
        Paragraph paragraphSectionBreak = new Paragraph();
        ParagraphProperties paragraphSectionBreakProperties = new ParagraphProperties();
        SectionProperties SectionBreakProperties = new SectionProperties();
        SectionType SectionBreakType = new SectionType() { Val = SectionMarkValues.NextPage };
        SectionBreakProperties.Append(SectionBreakType);
        paragraphSectionBreakProperties.Append(SectionBreakProperties);
        paragraphSectionBreak.Append(paragraphSectionBreakProperties);
        myMainPart.Document.Body.InsertAfter(paragraphSectionBreak, myMainPart.Document.Body.LastChild);
        myMainPart.Document.Save();
    }
}