c# - 如何使用 open xml c# 将段落居中对齐

标签 c# ms-word openxml openxml-sdk

我正在尝试对齐以居中段落,但对该段落没有任何影响。我正在使用 OpenXml。下面是代码:

//paragraph properties 
ParagraphProperties User_heading_pPr = new ParagraphProperties();

//trying to align center a paragraph
Justification justification1 = new Justification() { Val = JustificationValues.Center };

// build paragraph piece by piece
Text text = new Text(DateTime.Now.ToString() + " , ");
Text text1 = new Text(gjenerimi + " , ");
Text text2 = new Text(merreshifren());
var run = new Run();
run.Append(text,text1,text2);
Paragraph newParagraph = new Paragraph(run);
User_heading_pPr.Append(justification1);
newParagraph.Append(User_heading_pPr);

以下是我尝试使段落居中对齐的方式。

最佳答案

颠倒分配文本和段落属性的顺序:

User_heading_pPr.Append(justification1);
Paragraph newParagraph = new Paragraph(User_heading_pPr);
newParagraph.Append(run);

在有效且格式正确的 Word Open XML 中,段落属性必须在运行之前。因此,您必须以相同的方式构建 Open XML 文档。

这与对象模型有点不同,我们通常习惯于处理它们的方式 - 顺序很重要!

关于c# - 如何使用 open xml c# 将段落居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49570412/

相关文章:

c# - 如何从 .DOCX 文件中获取带有图像的标题文本

.NET OpenXML SDK 2 RunProperties 为 Null

c# - 自定义布局,Xamarin 表单。最佳方法

c# - 选择 HTML.RadioButtonFor 后如何为模型属性赋值

c# - Android/Java 对应 C#/C++ 方法 GetTickCount() 的方法是什么?

java - 避免 Word 在 Apache POI 生成的 .doc 开头显示空白页

c# - 能够在用户尝试关闭 Windows 窗体而不保存时通知用户请求 'Save Changes'

php - 在 PHP 中编辑 word 文件时出现问题

创建编号列表时 VBA Access Word 错误

c# - 使用 OpenXML 和 XElement 将多个 word 文档合并为一个文档