2013-12-20 23 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

看到上面的代碼解決這個問題

相關問題