BASKET
Search
PDFlib

pdf_import/import_repeated_contents

Download Java Code        Switch to PHP Code      Show Output PDF

package com.pdflib.cookbook.pdflib.pdf_import;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


/**

 * $Id: import_repeated_contents.java,v 1.3 2014/01/30 14:05:27 rjs Exp $

 *

 * Repeated contents: In order to keep the output file size as small as possible

 * handles to imported PDF pages or images can and should be reused for repeated

 * contents.

 *

 * Required software: PDFlib+PDI/PPS 9

 * Required data: PDF document, image file

 */

public class import_repeated_contents {

    public static void main(String argv[]) {

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

        String searchpath = "../input";

        String outfile = "import_repeated_contents.pdf";

        String title = "Repeated Contents";


        int pagecount = 3;


        pdflib p = null;

        String imagefile1 = "cambodia_preah_khan1.jpg";

        String imagefile2 = "cambodia_preah_khan2.jpg";

        String imagefile3 = "cambodia_preah_khan3.jpg";

        String background = "card.pdf";

        int exitcode = 0;


        try {

            p = new pdflib();


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


            /* This means we must check return values of load_font() 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 + " $Revision: 1.3 $");

           

            /* Load the font */

            int font = p.load_font("Helvetica", "unicode", "");

            if (font == -1)

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


            /* Load the three images */

            int image1 = p.load_image("auto", imagefile1, "");

            if (image1 == -1)

                throw new Exception("Error loading image " + imagefile1 + ": "

                    + p.get_errmsg());

           

            int image2 = p.load_image("auto", imagefile2, "");

            if (image2 == -1)

                throw new Exception("Error loading image " + imagefile2 + ": "

                    + p.get_errmsg());


            int image3 = p.load_image("auto", imagefile3, "");

            if (image3 == -1)

                throw new Exception("Error loading image " + imagefile3 + ": "

                    + p.get_errmsg());


            /* Open the card file */

            int card = p.open_pdi_document(background, "");

            if (card == -1)

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

           

            /* Load the first page of the card file */

            int cardpage = p.open_pdi_page(card, 1, "");

            if (cardpage == -1)

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


            /* Create textflow for explanation */

            String explanation =

                "Reuse PDI page, image and textflow handles across "

                    + "multiple pages";

            String optlist = "alignment=left font=" + font + " fontsize=20";

            int tf = p.create_textflow(explanation, optlist);

            if (tf == -1)

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

           

            /* Query the width and height of the first page of the card */

            double cardwidth = p.pcos_get_number(card, "pages[0]/width");

            double cardheight = p.pcos_get_number(card, "pages[0]/height");


            /* Offset factor from page border for content */

            double offset = 0.15;

           

            /*

             * Now reuse the PDI page handle, the image handles and the

             * textflow handle on all the pages.

             */

            for (int page = 1; page <= pagecount; page += 1) {

                p.begin_page_ext(cardwidth, cardheight, "");


                /* Place the card page as background*/

                p.fit_pdi_page(cardpage, 0, 0, "");


                /* Place images */

                p.fit_image(image1,

                    cardwidth * offset, cardheight * offset, "");

                p.fit_image(image2,

                    cardwidth * (1 - offset), cardheight * offset,

                    "position={right bottom}");

                p.fit_image(image3,

                    cardwidth * (1 - offset), cardheight * (1 - offset),

                    "position={right top}");

               

                /* Place textflow, must be rewound each time */

                p.fit_textflow(tf, cardwidth * offset, cardheight * 0.5,

                    cardwidth * 0.5, cardheight * (1 - offset), "rewind=1");

               

                /* Create a page number at the bottom center of the page */

                p.fit_textline("- " + page + " -",

                    cardwidth * 0.5, cardheight * offset / 2,

                    "font=" + font + " fontsize=12 position=center");


                p.end_page_ext("");

            }


            /* Clean up all handles */

            p.close_image(image1);

            p.close_image(image2);

            p.close_image(image3);

            p.close_pdi_page(cardpage);

            p.close_pdi_document(card);

            p.delete_textflow(tf);


            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.getMessage());

            exitcode = 1;

        }

        finally {

            if (p != null) {

                p.delete();

            }

            System.exit(exitcode);

        }

    }

}