Apache PDFBox: move last page to first page

I am writing a simple Java application with Apache PDFBox. I have several PDF files where the last page is the index of the content on the previous pages.

I need an index (last page), which became the first page of a PDF file.

Is it possible?

I also found the http://itextpdf.com/ library, which sounds better than the Apache PDFBox, but in this case I don’t know if I can do what I need,

Or maybe I can use this: http://saaspose.com/docs/display/pdf/How+to+Move+Page+within+a+Pdf+Document+%28Java+SDK%29

+4
source share
2 answers

With a PDFBox, you can open the original PDF file in a PDDocument, and then use getDocumentCatalog (). getAllPages () to get a list of pages. Reorder the list in the order you want and write each page in new documents.

PDDocument newDoc = new PDDocument(); PDDocument oldDoc = PDDocument.load (args[0]); List allPages = oldDoc.getDocumentCatalog().getAllPages(); // Code to rearrange the list goes here for ( int curPageCnt = 0; curPageCnt < allPages.size(); curPageCnt++ ) { newDoc.addPage( ( PDPage )allPages.get ( curPageCnt ) ); } // end for 
+5
source

I am using PDFBox version 2.0.0. This is how I move the last page to the first position:

 public static PDDocument moveLastPageToFirst(PDDocument documentToRearrangePages) { PDPageTree allPages = documentToRearrangePages.getDocumentCatalog().getPages(); if (allPages.getCount() > 1) { PDPage lastPage = allPages.get(allPages.getCount() - 1); allPages.remove(allPages.getCount() - 1); PDPage firstPage = allPages.get(0); allPages.insertBefore(lastPage, firstPage); } return documentToRearrangePages; } 
+1
source

All Articles