We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Merge 2 documents into 1 page

I have 2 pdf files and both only have one line of text. I run the code below and it combines the files into one correctly but it has 2 pages. Is there any way to put these onto 1 page as the content would easily fit onto one page?

 PdfFileEditor pdfEditor = new PdfFileEditor();

pdfEditor.Concatenate(“FirstDoc.pdf”,“SecondDoc.pdf”,“thirdpdf.pdf”);

Firstdoc.pdf (34.9 KB)
SecondDoc.pdf (32.6 KB)
thirdpdf.pdf (88.3 KB)

@Matt_b

Thanks for contacting support.

Concatenating/Merging two different PDF documents, adds pages of one PDF into Page Collection of another. However, in case you want to obtain a PDF document with single page, having text from both PDF documents, you can extract text from both documents and add it into new PDF. Please check following code snippet where we have created a PDF document with single page containing text from both PDFs.

Document doc1 = new Document(dataDir + "Firstdoc.pdf");
Document doc2 = new Document(dataDir + "SecondDoc.pdf");
TextAbsorber textAbsorber = new TextAbsorber();
doc1.Pages.Accept(textAbsorber);
TextFragment textFragment1 = new TextFragment(textAbsorber.Text);
textAbsorber = new TextAbsorber();
doc2.Pages.Accept(textAbsorber);
TextFragment textFragment2 = new TextFragment(textAbsorber.Text);

Document newDoc = new Document();
Page page = newDoc.Pages.Add();
page.Paragraphs.Add(textFragment1);
page.Paragraphs.Add(textFragment2);
newDoc.Save(dataDir + "ThirdDocument.pdf");

ThirdDocument.pdf (1.9 KB)

For your kind reference, an output PDF is also attached. In case of any further assistance, please feel free to let us know.

Thanks this works pretty well. I assume there is no simple way to copy text style? For example some text in pdf is bold and underlined but this does not copy over as this just imports the text.

@Matt_b

Thanks for your feedback.

In order to copy text fragments with their styles, you need to use TextFragmentAbsorber instead of TextAbsorber. TextFragmentAbsorber will extract text along with their styles and stores it in its TextFragments Collection. You can then add text in new PDF document while iterating through that collection. Please check following code snippet, where we have created a new PDF document where one of the source PDFs, had text in bold style.

Document doc1 = new Document(dataDir + "Firstdoc.pdf");
Document doc2 = new Document(dataDir + "SecondDoc.pdf");
TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber();
doc1.Pages.Accept(textAbsorber);
List<TextFragment> textFragments = new List<TextFragment>();
foreach(TextFragment tf in textAbsorber.TextFragments)
{
  textFragments.Add(tf);
}
TextAbsorber textAbsorber2 = new TextAbsorber();
doc2.Pages.Accept(textAbsorber2);
textFragments.Add(new TextFragment(textAbsorber2.Text));

Document newDoc = new Document();
Page page = newDoc.Pages.Add();
foreach (TextFragment tf in textFragments)
{
 page.Paragraphs.Add(tf);
}
newDoc.Save(dataDir + "ThirdDocument.pdf");

For your kind reference, input/output documents are also attached:

ThirdDocument.pdf (2.3 KB)
Firstdoc.pdf (1.9 KB)
SecondDoc.pdf (32.6 KB)

However, please note that TextFragmentAbsorber extracts text from source PDF, in same way which was used to add text inside it. In case of any further assistance, please feel free to let us know.