This is a migrated thread and some comments may be shown as answers.
Merge 2 DOCX files and export to PDF
1 Answer 50 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 2
Brad asked on 03 Aug 2011, 04:41 PM

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
      ' Create Document Fragments of each document
      'TODO: The header/footer is not getting selected
      Dim iDoc1Fragment As DocumentFragment = iDoc1.Selection.CopySelectedDocumentElements()
      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
      iNewDocument.InsertFragment(iDoc1Fragment, iPosition)
      ' Then insert a page break
      ' Finally move to the last character again and paste Doc2
      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

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 08 Aug 2011, 04:27 PM
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,
the Telerik team

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

Asked by
Top achievements
Rank 2
Answers by
Telerik team
Share this question