【发布时间】:2018-02-13 22:54:29
【问题描述】:
我正在尝试将字符串附加到新页面中 docx 文档的末尾。
这是我现在使用的代码:
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(path/fname, true))
var body = wordDoc.MainDocumentPart.Document.Body;
var para = body.AppendChild(new Paragraph());
var run = para.AppendChild(new Run());
var txt = "Document Signed by User" + Environment.NewLine;
run.AppendChild(new Text(txt));
但它会将文本附加到文档末尾而不是新页面中。
编辑
使用了 Daniel A. White 提出的解决方案:
var para = body.AppendChild(new Paragraph());
var run = para.AppendChild(new Run());
var plc = run.AppendChild(new Break() { Type = BreakValues.Page });
var txt = "Document Signed by User: " + user.User" + Environment.NewLine;
plc.AppendChild(new Text(txt));
但我收到了这个错误:
非复合元素没有子元素
【问题讨论】:
-
您的示例似乎在
using语句周围缺少{和}。 -
不要追加到休息时间,继续追加到你的跑步中。
-
@DanielA.White 谢谢你,成功了!
标签: c# openxml docx openxml-sdk