[英]Edit a docx with open xml
我正在尝试使用打开的xml编辑word文档,但结果不令人满意:
这是原始文件:
在这里,我刚读完一句话就开始了这份文件。 在:
之后,我必须添加几行。
我想要的结果是这样的(出于某些原因,我不得不擦除屏幕上的某些数据,但这不会影响问题):
但是我只有这样的结果:
因此,在第三个图像中,文本不是居中并且也没有任何选项卡。 加号,不是粗体(如果不是下划线的话,那不是问题)。
但是,真正的问题是它是左对齐的,没有制表符,而应该居中对齐制表符。
这是我的代码:
Text text = sdtFils.Descendants<Text>().FirstOrDefault();
string[] lst = variableCourrier.Tables[0].Rows[0][v_ligne["NOM_CHAMP"].ToString()].ToString().Split("$".ToCharArray());
Paragraph existPar = sdtFils.Descendants<Paragraph>().FirstOrDefault();
if (existPar == null)
{
existPar = sdtFils.Ancestors<Paragraph>().FirstOrDefault();
}
Run existRun = sdtFils.Descendants<Run>().FirstOrDefault();
foreach (string str in lst)
{
Paragraph p = new Paragraph();
ParagraphProperties pPr = new ParagraphProperties(new Tabs(new TabStop() { Val = TabStopValues.Center, Position = 7372 }),
new Justification() { Val = JustificationValues.Left },
new ParagraphStyleId() { Val = "Paragraphedeliste" },
new Indentation() { Start = (int.Parse(existPar.ParagraphProperties.Indentation.Left.Value) + int.Parse(existPar.ParagraphProperties.Indentation.FirstLine.Value)).ToString() });
string[] lstDetails = str.Split(";".ToCharArray());
int j = 0;
foreach (string strDetail in lstDetails)
{
if (!string.IsNullOrEmpty(strDetail))
{
TabChar tab = new TabChar();
Text t = new Text();
Run r = new Run();
RunProperties runPr = new RunProperties(new FontSize() { Val = existRun.RunProperties.FontSize.Val });
r.AppendChild(runPr);
t.Text = j == 0 ? "" + strDetail : " " + strDetail;
r.AppendChild(t);
if (j != lstDetails.Length - 1)
{
Break br = new Break();
r.AppendChild(br);
}
p.AppendChild(r);
j++;
}
}
text.Parent.Parent.Parent.Parent.InsertAfterSelf(p);
}
使用此代码,我拆分了字符串的每个部分,以便可以将它们放入它们的行中。
我认为不正确的部分是在定义Paragraph
元素时,但我看不到如何对其进行纠正,以获取看起来像第二个图像的重用。
有人可以帮助我吗?
如果不清楚,请告诉我,我将编辑问题。
谢谢。
您可以使用Open XML SDK 2.5生产力工具 (记住.docx文件是压缩成单个文件的XML文档的集合),打开* .docx并单击Reflect code以查看正确设置文档样式所需的代码。
您还可以使用Visual Studio外接程序的Open XML Package Editor ,它提供了一种简单的方法来解析和编辑Open Packaging Conventions文件,包括Word,Excel,PowerPoint和Visio文档。
一切都是开源的 ,可以在Github上找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.