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

c# – 如何使用openxml添加分节符下一页?,第1张

概述我想在文档的末尾添加一个分节符并添加一些文本. 我的代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessin 我想在文档的末尾添加一个分节符并添加一些文本.

我的代码如下:

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();        }    }}

我该怎么办?

解决方法 您需要将分节符添加到节属性中.然后,您需要将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元素.

总结

以上是内存溢出为你收集整理的c# – 如何使用openxml添加分节符下一页?全部内容,希望文章能够帮你解决c# – 如何使用openxml添加分节符下一页?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1250467.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存