Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Aug 28, 2019 12:41 pm

How do i merge different PDF pages into one PDF file. in Spire.PDF for java.

Safder
 
Posts: 21
Joined: Fri Sep 28, 2018 8:08 am

Thu Aug 29, 2019 3:35 am

Hi,

Thanks for your inquiry.
Do you want to merge selected pages from one PDF file or merge selected pages form multiple PDF files into one?

For case one, please refer to the code below.
Code: Select all
//merge selected pages from one PDF file
PdfDocument doc=new PdfDocument();
doc.loadFromFile("……\\……\\……\\filename.pdf");
PdfDocument doc1 = new PdfDocument();
doc1.insertPage(doc,0);
doc1.insertPage(doc,1);
doc1.saveToFile("output.pdf");


For case two, here is the code for your reference.
Code: Select all
//Merge selected pages from multiple PDF files into one
String[] files = new String[]
      {
              "……\\……\\……\\filename1.pdf",
              "……\\……\\……\\filename2.pdf",
              "……\\……\\……\\filename3.pdf",
};

PdfDocument[] docs = new PdfDocument[files.length];
for (int i = 0; i < files.length; i++)
{
    docs[i] = new PdfDocument(files[i]);
}

PdfDocument doc = new PdfDocument();
doc.insertPage(docs[0],0);
doc.insertPage(docs[1],1);
doc.insertPage(docs[2],3);

doc.saveToFile("output.pdf");
doc.close();


If my code couldn’t solve your issue, please offer us your input PDF files and describe your requirement in detail.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Aug 29, 2019 11:24 am

I mean i have developed multiple PdfPageBase pages i want to put them all in one document.

Safder
 
Posts: 21
Joined: Fri Sep 28, 2018 8:08 am

Fri Aug 30, 2019 7:54 am

Hi,

Thanks for your reply.
You could merge your developed pages by creating templates then drawing these pages on them.
Below is the code for your reference.

Code: Select all
//get the pages you want to merge
PdfPageBase page1 = doc1.getPages().get(1);
Dimension2D size1=page1.getSize();

PdfPageBase page2=doc2.getPages().get(1);
Dimension2D size2=page2.getSize();

//Create templates then draw these pages on them
PdfPageBase newpage = doc.getPages().add(size1);
newpage.getCanvas().drawTemplate(page1.createTemplate(), new Point(0,0));

PdfPageBase newpage2=doc.getPages().add(size2);
newpage2.getCanvas().drawTemplate(page2.createTemplate(),new Point(0,0) );


If my code couldn’t solve your issue, please offer us your input PDF files(if any) and the complete code you were using for further investigation.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Sep 16, 2019 9:58 am

Hi,

Greetings from E-iceblue.
Has you tried the code I offered you last time? Does it solve your issue?
Could you please give us some feedback at your convenience?

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF