Wednesday, January 10, 2018

PDFBox to set PDF Initial View to 2-Page

One can set the initial view of a PDF document to make the PDF open with Two Page view as default. This can be done using Adobe Acrobat Professional application, and setting the document properties under the File > Properties > Initial View tab as shown below.

To do this programmatically using PDFBox, here's the code snippet:

 /*******************************************************
  * Set the PDF to open up in 2-Page view as default
  * 
  * @param pdfDoc
  *******************************************************/
 public static void set2PageView(PDDocument pdfDoc)
 {
    PDPageFitDestination dest = new PDPageFitDestination();
    dest.setPage(pdfDoc.getPage(0));
    PDActionGoTo action = new PDActionGoTo(); 
    action.setDestination(dest);
    
    pdfDoc.getDocumentCatalog().setOpenAction(action);

    // PagetLayout
    //    TWO_PAGE_LEFT = Two-up (Facing)
    //    TWO_PAGE_RIGHT = Two-up (Cover Page)

    pdfDoc.getDocumentCatalog().setPageLayout(PageLayout.TWO_PAGE_RIGHT);
 }

No comments:

Post a Comment