2013-12-20 21 views
0

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

当前代码块我工作情况如下,并在进展中的工作,所以一些代码可能无法正常工作:

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天。有没有可能是我需要与之合作的另一种类型的对象?

UPDATE: 我想通了......我用错了对象模型

此代码的工作完美

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 

回答

0

看到上面的代码解决这个问题

相关问题