平台:.NET 4.5,C#,Open XML 2.0 SDK
What I'm trying to do:
从模板创建一组重复的幻灯片,创建'n'个新幻灯片(每个幻灯片复制模板幻灯片),每张幻灯片中包含一些特定文本 . 文本是多行,有些可能是粗体,因此必须不是作为单个文本插入,而是作为多个文本段落插入 .
Problem
看来我正在做我需要做的所有事情,但我遇到了一个损坏的文件错误 .
What I have successfully managed to do so far
我能够复制模板幻灯片,创建一组新幻灯片,并且我还能够将新文本插入占位符(通过简单替换)并保存 . 所以这些操作不是问题 . 问题是当我以编程方式创建新的段落元素时,设置属性并将节点插入到textBody中 .
以下是我创建新幻灯片的方法的基本步骤
我们在创建一个新的Slide,newSlide和下面的代码的方法中循环创建一组随机文本元素 .
注意:'绘图'是指
using Drawing = DocumentFormat.OpenXml.Drawing;
码
for (int mxx = 0; mxx < 4; mxx++)
{
Drawing.RunProperties rp = new Drawing.RunProperties();
if (mxx % 2 == 0)
rp.Bold = true;
rp.Dirty = false;
Drawing.Text txt = new Drawing.Text();
txt.Text = "another line " + i;
Drawing.Run rn = new Drawing.Run();
Drawing.Paragraph pg = new Drawing.Paragraph();
rn.InsertAt(rp, 0);
rn.InsertAt(txt, 0);
pg.InsertAt(rn, 0);
if (bc > 2 && i > 2) //some conditions - ignore for now
{
DocumentFormat.OpenXml.Presentation.TextBody body = newSlidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.TextBody>().ElementAt(1);
body.InsertAt(pg, 0);
}
}
鉴于我对openXML的了解很少(昨天开始!),我不清楚我还需要做什么 . 我通过提取存档进行逆向工程检查pptx,我可以看到以下主要区别,但是腐败的原因是什么?
好的powerpoint的块
<p:txBody>
<a:bodyPr anchor="ctr" anchorCtr="1"/>
<a:lstStyle/>
<a:p>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0"/>
<a:t>topic</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0"/>
</a:p>
</p:txBody>
来自我损坏的powerpoint XML的
块
<p:txBody>
<a:p>
<a:r>
<a:rPr lang="en-US" b="0" dirty="0" smtClean="0" />
<a:t>andother line 3</a:t>
</a:r>
</a:p>
<a:p>
<a:r>
<a:rPr lang="en-US" b="1" dirty="0" smtClean="0" />
<a:t>andother line 3</a:t>
</a:r>
</a:p>
<a:bodyPr anchor="ctr" anchorCtr="1" />
<a:lstStyle />
<a:p>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0" />
<a:t>2 topic line strategy venu 10:00 AM - 10:30 AM</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0" />
</a:p>
</p:txBody>
那有什么不同?如您所见,我的块中的a:bodyPtr和lstStyle出现在段落元素之间 . 这是问题 - 元素的顺序?还是我错过了别的什么?
很高兴知道是否有一种更简单,更正确的方法来做到这一点 - 例如如何在txtBody的当前p的末尾插入para(a:p)...
谢谢!