PDFlib Cookbook

cookbook

pdf_import/rotate_pages updated

Rotate the pages of an existing PDF document.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Rotate Pages:
 * Rotate the pages of an existing PDF document
 *
 * Import a PDF page and place it in the output document with a different
 * orientation using different methods:
 * - option "orientate": the simplest approach
 * - option "rotate": requires translation of the reference point
 * - option "transform": allows arbitrary transformations of the page
 *   including rotations which are not necessarily a multiple of 90 degrees
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF document
 */
package com.pdflib.cookbook.pdflib.pdf_import;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class rotate_pages
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";

        /* By default annotations are also imported. In some cases this
         * requires the Noto fonts for creating annotation appearance streams.
         * We therefore set the searchpath to also point to the font directory.
         */
        String fontpath = "../resource/font";
        String outfile = "rotate_pages.pdf";
        String title = "Rotate Pages";

        pdflib p = null;
        int exitcode = 0;
        String pdffile = "PDFlib-real-world.pdf";
        int indoc,page;
        double pagewidth, pageheight;

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");

            p.set_option("searchpath={" + fontpath + "}");

            /* This means we must check return values of open_pdi_document() etc. */
            p.set_option("errorpolicy=return");

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            
            /* *************************************************
             *  Variant 1: PDI page option "orientate"
             *  This is the simplest method; it can be used for each of
             *  the four directions north/east/south/west without any
             *  additional options or adjustments.
             */

            /* Open input PDF */
            indoc = p.open_pdi_document(pdffile, "");
            if (indoc == -1)
                throw new Exception("Error: " + p.get_errmsg());

            page = p.open_pdi_page(indoc, 1, "");

            if (page == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Page size with swapped width and height */
            p.begin_page_ext(50, 50, "");

            /* Orientate the page to the west and use the lower left corner
             * (0, 0) as reference point for placing the page.
             * 
             * The option "adjustpage" changes the dimensions of the output
             * page to those of the placed PDI page.
             */
            p.fit_pdi_page(page, 0, 0, "orientate=west adjustpage");
            
            p.close_pdi_page(page);
            p.close_pdi_document(indoc);
            p.end_page_ext("");
            

            /* *************************************************
             *  Variant 2: PDI page fitting option "rotate"
             */
            
            /* Open input PDF */
            indoc = p.open_pdi_document(pdffile, "");
            if (indoc == -1)
                throw new Exception("Error: " + p.get_errmsg());

            page = p.open_pdi_page(indoc, 1, "");

            if (page == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Retrieve page dimension with pCOS and supply output page size as
             * swapped width and height of the input page.
             */
            pagewidth = p.pcos_get_number(indoc, "pages[0]/width");
            pageheight = p.pcos_get_number(indoc, "pages[0]/height");

            p.begin_page_ext(pageheight, pagewidth, "");

            /* Use the "rotate" option; we must move the reference point
             * for placing the page from the lower left corner (0, 0) of the
             * output page to the lower right corner (pageheight, 0).
             */
            p.fit_pdi_page(page, pageheight, 0, "rotate=90");

            p.close_pdi_page(page);
            p.close_pdi_document(indoc);
            p.end_page_ext("");
            
            
            /* *************************************************
             * Variant 3: PDI page open option "transform"
             * The most general approach is the "transform" option.
             * It must be applied to p.open_pdi_page(). Since it sticks to
             * the imported page, the page can be placed at the lower left corner
             * and p.fit_pdi_page() doesn't require any changes.
             */
            
            /* Open input PDF */
            indoc = p.open_pdi_document(pdffile, "");
            if (indoc == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Translate and rotate the page; additional transformations can
             * be added and will be applied in the specified order.
             */
            page = p.open_pdi_page(indoc, 1, "transform={translate={" + pageheight + " 0} rotate=90}");

            if (page == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Retrieve page dimension with pCOS and supply output page size as
             * swapped width and height of the input page.
             */
            pagewidth = p.pcos_get_number(indoc, "pages[0]/width");
            pageheight = p.pcos_get_number(indoc, "pages[0]/height");

            p.begin_page_ext(pageheight, pagewidth, "");
            
            /* No additional options required for fitting the page */
            p.fit_pdi_page(page, 0, 0, "");

            p.close_pdi_page(page);
            p.close_pdi_document(indoc);
            p.end_page_ext("");
            
            p.end_document("");

        } catch (PDFlibException e) {
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname()
                + ": " + e.get_errmsg());
            exitcode = 1;
        } catch (Exception e) {
            System.err.println(e);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}