用程序更改docx文件中每个段落的页边距



我正在构建的应用程序为我工作的公司生成提案文档。这是一个复制、替换和删除DOCX文件和XLSX文件特定部分的复杂系统。在生成建议书的过程中,我使用XML强大的工具"DocumentBuilder"来组装文档。我遇到的问题如下。由于模板文件的年龄和选项的数量,我必须处理文档生成器最终忽略了整个文档长度中的一些边距设置。为了解决格式问题,我想做的是遍历XML文件中的每个段落并查找属性,然后深入该元素并检查是否需要修改现有的边距条目或添加新的边距条目。我的目标是让每个段落元素都有一个包含文档默认边距参数的节条目。这应该可以缓解格式问题。不过,我似乎无法让它发挥作用。我尝试了几种不同的方法来更改XML文件,但都没有成功。。。

我正在使用的当前代码块如下,并且正在进行中,因此一些代码可能无法正常工作:

Dim DocOutput As New WmlDocument(WP_OUTPUT)
Using docStream As New OpenXmlMemoryStreamDocument(DocOutput)
Using wpSource As WordprocessingDocument = docStream.GetWordprocessingDocument()
Dim flag As Boolean
Dim Section As New SectionProperties
Dim Margin As New PageMargin
Margin.Top = 720
Margin.Right = 360
Margin.Bottom = 2347
Margin.Left = 1526
Margin.Header = 432
Margin.Footer = 432
RevisionAccepter.AcceptRevisions(wpSource)
Dim root As XElement = wpSource.MainDocumentPart.GetXDocument.Root
Dim body As XElement = root.LogicalChildrenContent.First
For Each blockLevelContentElement As XElement In body.LogicalChildrenContent()
If blockLevelContentElement.Name = W.p Then
For Each Paragraph As XElement In blockLevelContentElement.Elements()
If Paragraph.Name = W.pPr Then
For Each ParagraphProp As XElement In Paragraph.Elements()
flag = False
If ParagraphProp.Name = W.sectPr Then
For Each SectionProp As XElement In ParagraphProp.Elements()
If SectionProp.Name = W.pgMar Then
SectionProp.SetAttributeValue(W.top, Margin.Top)
SectionProp.SetAttributeValue(W.right, Margin.Right)
SectionProp.SetAttributeValue(W.bottom, Margin.Bottom)
SectionProp.SetAttributeValue(W.left, Margin.Left)
SectionProp.SetAttributeValue(W.header, Margin.Header)
SectionProp.SetAttributeValue(W.footer, Margin.Footer)
flag = True
End If
Next
If flag = False Then
ParagraphProp.SetElementValue(W.sectPr, Margin)
flag = True
End If
End If
Next
If Not flag Then
Paragraph.Add(Section.AppendChild(Margin.CloneNode(False)))
End If
End If
Next
End If
Next
End Using
Dim sources As New List(Of Source)
Dim WmlHolder As New WmlDocument(docStream.GetModifiedWmlDocument)
sources.Add(New Source(WmlHolder, True))
DocumentBuilder.BuildDocument(sources, WP_TEMP)
End Using

当我试图将正确的值写入DOCX文件时,我是否做错了什么?我已经挣扎了大约4天了。是否有其他类型的对象需要我处理?

更新:我想明白了。。。我使用了错误的对象模型

此代码适用于完美的

Try
Dim DocOutput As New WmlDocument(WP_OUTPUT)
Using docStream As New OpenXmlMemoryStreamDocument(DocOutput)
Using wpSource As WordprocessingDocument = docStream.GetWordprocessingDocument()
Dim flag As Boolean
Dim Margin As New PageMargin
Margin.Top = 721
Margin.Right = 361
Margin.Bottom = 2341
Margin.Left = 1521
Margin.Header = 431
Margin.Footer = 431
Dim body As Body = wpSource.MainDocumentPart.Document.Body
For Each paragraph In body.ChildElements
If paragraph.GetType().Name = "Paragraph" Then
For Each paragraphProp In paragraph.ChildElements
If paragraphProp.GetType().Name = "ParagraphProperties" Then
flag = False
For Each sect In paragraphProp.ChildElements
If sect.GetType().Name = "SectionProperties" Then
For Each sectProp In sect.ChildElements
If sectProp.GetType().Name = "PageMargin" Then
sectProp.Remove()
sect.AppendChild(Margin.CloneNode(False))
flag = True
End If
Next
If Not flag Then
sect.AppendChild(Margin.CloneNode(False))
flag = True
End If
End If
Next
If Not flag Then
paragraphProp.AppendChild(New SectionProperties().AppendChild(Margin.CloneNode(False)).CloneNode(False))
End If
End If
Next
End If
Next
End Using
Dim sources As New List(Of Source)
Dim WmlHolder As New WmlDocument(docStream.GetModifiedWmlDocument)
sources.Add(New Source(WmlHolder, True))
DocumentBuilder.BuildDocument(sources, WP_TEMP)
End Using
Catch ex As Exception
End Try

有关问题的解决方案,请参阅上面的代码

相关内容

  • 没有找到相关文章

最新更新