PDFlib
KAUFEN
PDFlib

pdf_import/clone_page_boxes

Import a PDF page, and clone all of its ArtBox, TrimBox, BleedBox, CropBox, and MediaBox entries. Import a PDF page and read the respective *Box entries with pCOS. Start the output page and replicate the *Box entries by using the corresponding *box options of begin_page_ext(). Then, place the imported page in the lower left corner of the CropBox if present, and the MediaBox otherwise.

Note: You can visualize the page boxes in Acrobat 8 as follows:
Choose "Edit, Preferences, General, Page Display, Page Content and Information" and select "Show art, trim & bleed boxes".
This setting will create colorized rectangles which indicate the size of the art, trim, and bleed boxes.

Download Java Code      Show Output PDF     Show Input PDF       Switch to PHP Code

/* $Id: clone_page_boxes.java,v 1.5 2009/10/09 10:33:42 stm Exp $

 *

 * Clone page boxes:

 * Import a PDF page, and clone all of its ArtBox, TrimBox, BleedBox, CropBox,

 * and MediaBox entries.

 *

 * Note: You can visualize the page boxes in Acrobat 8 as follows:

 * Choose "Edit, Preferences, General, Page Display, Page Content and

 * Information" and select "Show art, trim & bleed boxes".

 * This setting will create colorized rectangles which indicate the size of the

 * art, trim, and bleed boxes.

 *

 * Required software: PDFlib+PDI/PPS 8

 * Required data: PDF document

 */

package com.pdflib.cookbook.pdflib.pdf_import;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class clone_page_boxes {

    public static void main(String argv[]) {

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

        String searchpath = "../input";

        String outfile = "clone_page_boxes.pdf";

        String title = "Clone Page Boxes";


        pdflib p = null;

        String pdffile = "pageboxes.pdf";

        int indoc, pageno, endpage, page;


        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");


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

                throw new Exception("Error: " + p.get_apiname() + ": "

                        + p.get_errmsg());


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

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


            /* Open the input PDF */

            indoc = p.open_pdi_document(pdffile, "");

            if (indoc == -1)

                throw new Exception("Error: " + p.get_apiname() + ": "

                        + p.get_errmsg());


            endpage = (int) p.pcos_get_number(indoc, "length:pages");


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

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

                /*

                 * "cloneboxes" must be used with open_pdi_page() to read all

                 * relevant box values.

                 */

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


                if (page == -1)

                    throw new Exception("Error: " + p.get_apiname() + ": "

                            + p.get_errmsg());


                /*

                 * Start and place the page. "cloneboxes" must be used again as

                 * an option to fit_pdi_page() to apply the box values to the

                 * current page.

                 */

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


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


                p.end_page_ext("");


                p.close_pdi_page(page);

            }


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

            }

        }

    }

}