PDFlib
BASKET
PDFlib

pdf_import/pdfmerge_with_adjusted_pdfversion

Merge multiple PDF documents with the output PDF having as version number the maximum of the PDF version numbers of all imported documents.
Only the pages from the imported documents are merged; interactive elements will be ignored.

Download Java Code      Show Output PDF

/* $Id: pdfmerge_with_adjusted_pdfversion.java,v 1.1 2008/04/08 12:33:42 katja Exp $

 * PDF merge with adjusted PDF version:

 * Merge multiple PDF documents with the output PDF having as version number

 * the maximum of the PDF version numbers of all imported documents.

 *

 * Only the pages from the imported documents are merged; interactive elements

 * will be ignored.

 *

 * Required software: PDFlib+PDI/PPS 7

 * Required data: PDF documents

 */

package com.pdflib.cookbook.pdflib.pdf_import;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class pdfmerge_with_adjusted_pdfversion

{

    public static void main (String argv[])

    {

    /* This is where the data files are. Adjust as necessary. */

    String searchpath = "../input";

    String outfile = "pdfmerge_with_adjusted_pdfversion.pdf";

    String title = "PDF Merge with adjusted PDF Version";


    pdflib p = null;

    String pdffiles[] =

    {

        "PDFlib-real-world.pdf",

        "PDFlib-datasheet.pdf",

        "TET-datasheet.pdf",

        "PLOP-datasheet.pdf",

        "pCOS-datasheet.pdf"

    };

    int i, indoc;

    double pdfver = 0, maxpdfver = 0;


    try {

        p = new pdflib();


        p.set_parameter("SearchPath", searchpath);


        /* This means we must check return values of load_font() etc. */

        p.set_parameter("errorpolicy", "return");


        /* -----------------------------------------------------------------

         * Loop over all input documents to retrieve the highest PDF version

         * used

         * -----------------------------------------------------------------

         */

        for (i=0; i < pdffiles.length; i++) {

            /* Open the input PDF */

            indoc = p.open_pdi_document(pdffiles[i], "");

            if (indoc == -1) {

                System.err.println("Error: " + p.get_errmsg());

                continue;

            }

       

            /* Retrieve the PDF version of the current document. If it is higher

             * than the maximum version retrieved until now make it to be the

             * maximum version.

             */ 

            pdfver = p.pcos_get_number(indoc, "pdfversion")/10;

            if (pdfver > maxpdfver) {

                maxpdfver = pdfver;

            }

           

            /* Close the input document.

             * Depending on the number of PDFs and memory strategy, PDI handles

             * to all documents could also be kept open between the first and

             * second run (requires more memory, but runs faster). We close all

             * PDFs after checking the version number, and reopen them in the

             * second loop (requires less memory, but is slower).

             */

            p.close_pdi_document(indoc);

        }


        /* ---------------------------------------------------------------

         * Open the output document with the maximum PDF version retrieved

         * ---------------------------------------------------------------

         */

        String optlist = "compatibility=" + Double.toString(maxpdfver);

             

        if (p.begin_document(outfile, optlist) == -1)

            throw new Exception("Error: " + p.get_errmsg());


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

        p.set_info("Title", title + " $Revision: 1.1 $");

       

        /* --------------------------------------------------------------------

         * Loop over all input documents to merge them into the output document       * used

         * --------------------------------------------------------------------

         */

        for (i=0; i < pdffiles.length; i++) {

            int endpage, pageno, page;


            /* Open the input PDF */

            indoc = p.open_pdi_document(pdffiles[i], "");

            if (indoc == -1) {

                System.err.println("Error: " + p.get_errmsg());

                continue;

            }

       

            endpage = (int) p.pcos_get_number(indoc, "/Root/Pages/Count");


            /* Loop over all pages of the input document */

            for (pageno = 1; pageno <= endpage; pageno++) {

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


                if (page == -1) {

                    System.err.println("Error: " + p.get_errmsg());

                    continue;

                }

                /* Dummy page size; will be adjusted later */

                p.begin_page_ext(10, 10, "");


                /* Create a bookmark with the file name */

                if (pageno == 1)

                    p.create_bookmark(pdffiles[i], "");


                /* Place the imported page on the output page, and

                 * adjust the page size

                 */

                p.fit_pdi_page(page, 0, 0, "adjustpage");

                p.close_pdi_page(page);


                p.end_page_ext("");

            }

            /* Close the input document */

            p.close_pdi_document(indoc);

        }


        p.end_document("");


        } catch (PDFlibException e) {

            System.err.print("PDFlib exception occurred:\n");

            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +

                ": " + e.get_errmsg() + "\n");

        } catch (Exception e) {

            System.err.println(e.getMessage());

        } finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}