/* $Id: clone_page_boxes.java,v 1.3 2008/04/22 19:13:52 tm Exp $
 * 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.
 *
 * Required software: PDFlib+PDI/PPS 7
 * 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";
    String optlist;
    int indoc, pageno, endpage, page;
    boolean have_cropbox = false;
    double[] artbox, bleedbox, cropbox, mediabox, trimbox;

    artbox = new double[4];
    bleedbox = new double[4];
    cropbox = new double[4];
    mediabox = new double[4];
    trimbox = new double[4];

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

        p.set_info("Creator", "PDFlib Cookbook");
        p.set_info("Title", title + " $Revision: 1.3 $");

        /* Open the input PDF */
        indoc = p.open_pdi_document(pdffile, "");
        if (indoc == -1)
            throw new Exception("Error: " + 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++)
        {
            /* The "pdiusebox" option can be used to specify the box dimensions
             * to be used for determining the imported page’s size. By default,
             * the dimensions of the CropBox will be used if present and the
             * MediaBox otherwise.
             */
            page = p.open_pdi_page(indoc, pageno, "");

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

            /* --------------------------------------------------------------
             * Retrieve the *Box coordinates from the input page and use them
             * to assemble the option list for starting the output page
             * --------------------------------------------------------------
             */
            optlist = "";

            have_cropbox = false;

            /* If the ArtBox is present on the page retrieve its four
             * coordinates and add them to the option list
             */
            if (!p.pcos_get_string(indoc,
                "type:pages[" + (pageno-1) + "]/ArtBox").equals("null"))
            {
                optlist = optlist + "artbox={";
                for (int i = 0; i < 4; i++)
                {
                    artbox[i] = p.pcos_get_number(indoc,
                        "pages[" + (pageno-1) + "]/ArtBox[" + i + "]");

                    optlist = optlist + artbox[i] + " ";
                }
                optlist = optlist + "} ";
            }

            /* If the BleedBox is present on the page retrieve its four
             * coordinates and add them to the option list
             */
            if (!p.pcos_get_string(indoc,
                "type:pages[" + (pageno-1) + "]/BleedBox").equals("null"))
            {
                optlist = optlist + "bleedbox={";
                for (int i = 0; i < 4; i++)
                {
                    bleedbox[i] = p.pcos_get_number(indoc,
                        "pages[" + (pageno-1) + "]/BleedBox[" + i + "]");

                    optlist = optlist + bleedbox[i] + " ";
                }
                optlist = optlist + "} ";
            }

            /* If the CropBox is present on the page retrieve its four
             * coordinates and add them to the option list
             */
            if (!p.pcos_get_string(indoc,
                "type:pages[" + (pageno-1) + "]/CropBox").equals("null"))
            {
                optlist = optlist + "cropbox={";
                for (int i = 0; i < 4; i++)
                {
                    cropbox[i] = p.pcos_get_number(indoc,
                        "pages[" + (pageno-1) + "]/CropBox[" + i + "]");

                    optlist = optlist + cropbox[i] + " ";
                }
                optlist = optlist + "} ";

                have_cropbox = true;
            }

            /* Retrieve the coordinates of the MediaBox and add them to the
             * option list. Since the MediaBox is always present on the page no
             * check is needed.
             */
            optlist = optlist + "mediabox={";
            for (int i = 0; i < 4; i++)
            {
                mediabox[i] = p.pcos_get_number(indoc,
                    "pages[" + (pageno-1) + "]/MediaBox[" + i + "]");

                optlist = optlist + mediabox[i] + " ";
            }
            optlist = optlist + "} ";

            /* If the TrimBox is present on the page retrieve its four
             * coordinates and add them to the option list
             */
            if (!p.pcos_get_string(indoc,
                "type:pages[" + (pageno-1) + "]/TrimBox").equals("null"))
            {
                optlist = optlist + "trimbox={";
                for (int i = 0; i < 4; i++)
                {
                    trimbox[i] = p.pcos_get_number(indoc,
                        "pages[" + (pageno-1) + "]/TrimBox[" + i + "]");

                    optlist = optlist + trimbox[i] + " ";
                }
                optlist = optlist + "} ";
            }

            /* ----------------------------------------------------------------
             * Start and place the page. Zero width and height will be
             * overridden by the "mediabox" option.
             * PDFlib places the imported page at (0, 0) of the "visible" page,
             * which is determined by the CropBox if present, and the MediaBox
             * otherwise. The imported page must therefore be placed in the
             * lower left corner of the relevant box.
             * ----------------------------------------------------------------
             */
            p.begin_page_ext(0, 0, optlist);

            if (have_cropbox)
                p.fit_pdi_page(page, cropbox[0], cropbox[1], "");
            else
                p.fit_pdi_page(page, mediabox[0], mediabox[1], "");

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