无法加入的Open XML Word文档两段 [英] Unable to add two paragraphs in open xml word document
问题描述
我是新来的Open XML。这是我已经能够迄今为止实现的:
I am new to Open XML. This is what I have been able to achieve so far:
- 创建一个Word文档
- 添加段落与一些文本
- 通过更改段落的对齐属性对齐文本
- 更改字体大小和放大器;大胆的(开/关)
我想添加两款具有不同的字体大小和理由。
这是我的code:
I am trying to add two paragraphs with different font size and justifications. This is my code:
Dim FontHeading As New DocumentFormat.OpenXml.Wordprocessing.FontSize
FontHeading.Val = New StringValue("28")
Dim FontSubHeading As New DocumentFormat.OpenXml.Wordprocessing.FontSize
FontSubHeading.Val = New StringValue("24")
Dim wordDocument As WordprocessingDocument = WordprocessingDocument.Create(Server.MapPath("/test.docx"), WordprocessingDocumentType.Document)
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
mainPart.Document = New Document()
Dim dbody As New Body
dbody.AppendChild(AddParagraph("PREM CORPORATE", FontHeading, FontBold, CenterJustification))
dbody.AppendChild(AddParagraph("Company Incorporation Documents", FontSubHeading, FontBold, CenterJustification))
mainPart.Document.AppendChild(dbody)
mainPart.Document.Save()
wordDocument.Close()
功能添加段落:
Private Function AddParagraph(ByVal txt As String, ByVal fsize As DocumentFormat.OpenXml.Wordprocessing.FontSize, ByVal fbold As Bold, ByVal pjustification As Justification) As Paragraph
Dim runProp As New RunProperties
runProp.Append(fsize)
runProp.Append(fbold)
Dim run As New Run
run.Append(runProp)
run.Append(New Text(txt))
Dim pp As New ParagraphProperties
pp.Justification = pjustification
Dim p As Paragraph = New Paragraph
p.Append(pp)
p.Append(run)
Return p
End Function
一个空文档中的上述结果。
如果我删除第二行dbody.AppendChild,那么它成功地增加了第一段。
The above results in an empty document. If I remove the second dbody.AppendChild line, then it successfully adds the first paragraph.
请帮我需要做什么来改变/添加。
Please help what do I need to change/add.
推荐答案
您正在尝试添加的相同的实例的的粗体
和理由
对象不同的段落
。这是不允许的,应该导致错误:
You're trying to add the same instance of the Bold
and Justification
objects to different Paragraphs
. This isn't allowed and should result in the error:
System.InvalidOperationException - 无法插入OpenXmlElementnewChild对象,因为它是树的一部分
System.InvalidOperationException - Cannot insert the OpenXmlElement "newChild" because it is part of a tree.
要克服这个你应该创建一个新的粗体
和新的理由
每次需要的时间。
To get round this you should create a new Bold
and a new Justification
each time you need one.
在你的 AddParagraph
方法,你可能只是需要一个布尔
来表示文字是否应该大胆一个 JustificationValues
来表示使用则创建的每个新的实例为理由要求:
In your AddParagraph
method you could just take a Boolean
to denote whether or not the text should be bold and a JustificationValues
to denote the justification to use then create new instances of each as required:
Private Function AddParagraph(txt As String, fsize As DocumentFormat.OpenXml.Wordprocessing.FontSize, bold As Boolean, pjustification As JustificationValues) As Paragraph
Dim runProp As New RunProperties()
runProp.Append(fsize)
If bold Then
runProp.Append(New Bold())
End If
Dim run As New Run()
run.Append(runProp)
run.Append(New Text(txt))
Dim pp As New ParagraphProperties()
pp.Justification = New Justification() With { _
Key .Val = pjustification _
}
Dim p As New Paragraph()
p.Append(pp)
p.Append(run)
Return p
End Function
您调用添加段落
则是这样的:
Your calls to add the Paragraphs
would then be something like this:
dbody.AppendChild(AddParagraph("PREM CORPORATE", FontHeading, True, JustificationValues.Center))
dbody.AppendChild(AddParagraph("Company Incorporation Documents", FontSubHeading, True, JustificationValues.Center))
这篇关于无法加入的Open XML Word文档两段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!