以编程方式更改docx文件中每个段落的边距

时间:2013-12-20 15:12:34

标签: .net xml vb.net docx

我正在构建的应用程序为我工作的公司生成提案文档。这是复制,替换和删除DOCX文件和XLSX文件的特定部分的复杂系统。我正在使用XML powertools“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

1 个答案:

答案 0 :(得分:0)

请参阅上述代码以了解此问题的解决方案