我想在文档末尾添加分节符并添加一些文本。
我的代码如下:
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();
}
}
}
我该怎么办?
您需要将分节符添加到节属性中。然后,您需要将节属性附加到段落属性。接下来将段落属性附加到段落中。
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 元素努力实现。
首先你需要创建一个中断段落
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);
编辑:
这会添加分页符而不是分节符。
这个会在页面末尾添加分节符
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();
}
}
如果我将SectionMarkValues.NextPage 替换为SectionMarkValues.NextPage.Continously,它仍然会产生分页符
段落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);