How to merge or insert PDF pages in Android

To insert/copy/merge several PDF documents into one.

1PDFDoc new_doc = new PDFDoc();
2int page_num = 15;
3for (int i=1; i<=page_num; ++i)
4{
5 PDFDoc doc = new PDFDoc(filename + "_split_page_" + i + ".pdf");
6 new_doc.insertPages(i, doc, 1, doc.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);
7 doc.close();
8}
9new_doc.save(output_filename + "_merge_pages.pdf", SDFDoc.SaveMode.LINEARIZED, null);

Merge, copy, delete, and rearrange PDF pages
Full code sample which illustrates how to copy pages from one document to another, how to delete, and rearrange pages and how to use ImportPages().

About page copying/merging

The recommended way to copy pages from one document to another is with PDFDoc.InsertPages(). Its arguments are:

  • insertBeforeThisPage: An integer specifying where the pages should be inserted
  • sourceDoc: A PDFDoc from which the pages should be read
  • startPage: An integer specifying the first page number to insert
  • endPage: An integer specifying the last page number to insert
  • flag: A PDFDoc.InsertFlag value

For example, suppose we want to insert the third page of one document after the first page of a second document. The following code snippet performs this with an insertBeforeThisPage value of 2 and startPage and endPage values of 3.

1dest_doc.insertPages(2, source_doc, 3, 3, PDFDoc.InsertBookmarkMode.NONE, null);

We can also insert a range of pages. For example, the following code will insert the second, third, and fourth pages of one document into the end of the second document. We specify that we're inserting into the end of the document by using an insertBeforeThisPage value higher than the number of pages in the document:

1dest_doc.insertPages(dest_doc.getPageCount() + 1, source_doc, 2, 4, PDFDoc.InsertBookmarkMode.NONE, null);

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales