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

Moh*_*d K 9 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

emb*_*yle 10

您需要将分节符添加到节属性中.然后,您需要将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);
Run Code Online (Sandbox Code Playgroud)

生成的Open XML是:

  <w:p>
    <w:pPr>
      <w:sectPr>
        <w:type w:val="nextPage" />
      </w:sectPr>
    </w:pPr>
  </w:p>
Run Code Online (Sandbox Code Playgroud)

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

  • 虽然这确实插入了分节符,但最终文档页面属性存在问题。整个文档(在插入分节符之前)可能在末尾有一个特定的“sectionProperties”条目。添加中断时,页面会丢失这些属性。知道如何确保页面属性不会丢失吗? (2认同)

Moh*_*ief 5

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

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

然后你需要附加该段落

wordprocessingDocument.MainDocumentPart.Document.Body.Append(PageBreakParagraph)
Run Code Online (Sandbox Code Playgroud)

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

wordprocessingDocument.MainDocumentPart.Document.Body.InsertAfter(PageBreakParagraph, ReferenceElement);
wordprocessingDocument.MainDocumentPart.Document.Body.InsertBefore(PageBreakParagraph, ReferenceElement);
Run Code Online (Sandbox Code Playgroud)

编辑:

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

  • 这将插入一个分页符。不是分节符。两者非常不同。 (4认同)