Merge 2 DOCX files and export to PDF

2 posts, 0 answers
  1. Brad
    Brad  avatar
    9 posts
    Member since:
    Jun 2012

    Posted 03 Aug 2011 Link to this post

    Hello, 

    I'm trying to use a RadDocument to import 2 DOCX byte arrays into a single document.  Once imported, the idea is to export the results into a single document of any specified type.  In this case, I'm choosing PDF.  

    I've noticed the following:
     1) The 2nd file works flawlessly, but does not have header information
     2) The 1st file imports the details of the file, but does not import the page header/footer (necessary)
     3) Instead of merging Doc1 with Doc2 into a NewDoc, if I set NewDoc = Doc1 and then insert Doc2 it includes the Header/Footer, but ruins the formatting of Doc2

    Here is the code used:

       Public Shared Function MergeDocuments(ByVal Document1 As Byte(), ByVal Document2 As Byte()) As Byte()
          Dim iProvider As IDocumentFormatProvider = New DocxFormatProvider
     
          ' Create RadDocuments from the byte arrays (NOTE: currently only accepts DOCX)
          Dim iDoc1 As RadDocument = iProvider.Import(Document1)
          Dim iDoc2 As RadDocument = iProvider.Import(Document2)
          Dim iNewDocument As New RadDocument
     
          ' Have to do this to work with API of RadDocument
          iNewDocument.MeasureAndArrangeInDefaultSize()
          iDoc1.MeasureAndArrangeInDefaultSize()
          iDoc2.MeasureAndArrangeInDefaultSize()
     
          ' Create Document Fragments of each document
          'TODO: The header/footer is not getting selected
          iDoc1.Selection.SelectAll()
          Dim iDoc1Fragment As DocumentFragment = iDoc1.Selection.CopySelectedDocumentElements()
     
          iDoc2.Selection.SelectAll()
          Dim iDoc2Fragment As DocumentFragment = iDoc2.Selection.CopySelectedDocumentElements()
     
          ' Track the position of the New Document and insert the Fragments
          Dim iPosition As DocumentPosition = New DocumentPosition(iNewDocument)
     
          ' Move to the last charater of the new doc and paste Doc1
          iPosition.MoveToLastPositionInDocument()
          iNewDocument.InsertFragment(iDoc1Fragment, iPosition)
     
          ' Then insert a page break
          iNewDocument.InsertPageBreak()
     
          ' Finally move to the last character again and paste Doc2
          iPosition.MoveToLastPositionInDocument()
          iNewDocument.InsertFragment(iDoc2Fragment, iPosition)
     
          ' Export the files as a PDF and return it
          Dim iPDFProvider As IDocumentFormatProvider = New PdfFormatProvider
          Dim iPDFBytes As Byte() = iPDFProvider.Export(iNewDocument)
     
          Return iPDFBytes
       End Function
    
  2. Boby
    Admin
    Boby avatar
    697 posts

    Posted 08 Aug 2011 Link to this post

    Hi Brad ,
    For now, RadDocument can contain only one section. As headers/footers are properties of the section, you have to choose the headers/footers from one of the documents you merge, and set them to the first section of the resulting document:
    iNewDocument.Sections.First.Headers = iDoc1.Sections.First.Headers;
    iNewDocument.Sections.First.Footers = iDoc1.Sections.First.Footers;
    Allowing multiple sections in the document is currently scheduled, and hopefully will be implemented, for Q3 2011 version of controls.

    Don't hesitate to contact us if you have other questions.

    Best wishes,
    Boby
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Back to Top