我使用 Word 2013 .docx 作为在 openxml 代码中打开的模板,填充内容控件,然后将其保存,下载给用户,这部分工作正常。
在一个内容控件中,目标是添加项目符号列表。问题来自尝试附加或替换从方法生成的新段落。此方法工作正常,我将变量发送给它,它返回一个带有项目符号的段落。它插入,但它破坏了doc这个词;
下图是template.docx(上)和resulting.docx(下)只有下段不同,multiline=1后空白区域;其余部分相同。如果我删除运行元素,保存,尝试,我可以打开单词 doc 并在那里查看格式化的项目符号列表,以便该部分“有效”,但错误是什么?
public static Paragraph GenerateParagraph(string bulletText)
{
var element =
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "ListParagraph" },
new NumberingProperties(
new NumberingLevelReference() { Val = 1 },
new NumberingId() { Val = 2 })),
new Run(
new Text(bulletText)) //Text you want to insert with bullet
) { RsidParagraphAddition = "00031711", RsidParagraphProperties = "00031711", RsidRunAdditionDefault = "00031711" };
return element;
}`
文档一词:
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tag w:val="custMilestones"/>
<w:id w:val="-1610118874"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder>
<w:multiLine w:val="1"/>
* in the file compare this original file was different, in this area with missing lines
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:r w:rsidRPr="002C1BAC">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
<w:r w:rsidR="00DD453F">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
</w:sdtContent>
和生成的文件:
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tag w:val="custMilestones"/>
<w:id w:val="-1610118874"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder>
<w:multiLine w:val="1"/>
<w:p>
<w:p w:rsidR="00031711" w:rsidP="00031711" w:rsidRDefault="00031711">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="1"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:t>
Summary of outlines including the details of customer products and part numbers.
</w:t>
</w:r>
</w:p>
</w:p>
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:r w:rsidRPr="002C1BAC">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
<w:r w:rsidR="00DD453F">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
</w:sdtContent>
运行的代码,word doc 有一个名为 custMilestones 作为标记名的富文本内容控件*(它是使用富文本 cc 设置的),并且内容控件中有一个空格,否则代码将引发对象引用未设置异常。
byte[] byteArray = System.IO.File.ReadAllBytes(fileName);
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(mem, true))
{
MainDocumentPart mainDoc = wordDoc.MainDocumentPart;
Body bodyDoc = mainDoc.Document.Body;
foreach (var cc in wordDoc.ContentControls())
{
SdtProperties props = cc.Elements<SdtProperties>().FirstOrDefault();
Tag tag = props.Elements<Tag>().FirstOrDefault();
dat1 = tag.Val.ToString();
switch (dat1)
{
case "custMilestones":
props.RemoveAllChildren<Paragraph>();
props.AppendChild<Paragraph>(new Paragraph(GenerateParagraph(custmilestone1)));
break;
default:
break;
}
编辑 2015 年 7 月 30 日,生成的 xml 产生了奇怪的样式结果:
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:rStyle w:val="Style1"/>
<w:b w:val="0"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tag w:val="custmiles"/>
<w:id w:val="568603642"/>
<w:placeholder>
<w:docPart w:val="D9E50C82EA8C4C45A237822288EB36B5"/>
</w:placeholder>
<w:showingPlcHdr/>
<w15:appearance w15:val="hidden"/>
</w:sdtPr>
<w:sdtEndPr>
<w:rPr>
<w:rStyle w:val="DefaultParagraphFont"/>
<w:sz w:val="36"/>
</w:rPr>
</w:sdtEndPr>
<w:sdtContent>
<w:r w:rsidRPr="00B87AB0" w:rsidR="00E73294">
<w:rPr>
<w:b w:val="0"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve"/>
</w:r>
</w:sdtContent>
</w:sdt>
<w:r w:rsidRPr="00B87AB0" w:rsidR="00C06C61">
<w:rPr>
<w:b w:val="0"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tab/>
<w:sdt>
<w:sdtPr>
<w:id w:val="-206335861"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder>
</w:sdtPr>
<w:sdtContent>
<w:p w:rsidRDefault="00816E2A" w:rsidR="00816E2A" w:rsidP="00816E2A">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:ind w:leftChars="0"/>
</w:pPr>
<w:r>
<w:t>
this is test sentence1 for the software to make lists with numbers
</w:t>
</w:r>
</w:p>
<w:p w:rsidRDefault="00816E2A" w:rsidR="00816E2A" w:rsidP="00816E2A">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:ind w:leftChars="0"/>
</w:pPr>
<w:r>
<w:t>
this is test sentence2 for the software to make lists with numbers
</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:p w:rsidRDefault="00816E2A" w:rsidR="00546790" w:rsidP="00816E2A">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:ind w:leftChars="0"/>
</w:pPr>
<w:r>
<w:t>
this is test sentence3 for the software to make lists with numbers </w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
</w:r>