PDFlib Cookbook

cookbook

blocks/clone_blocks

Download Java Code       Switch to PHP code      Show Output PDF

/*
 * $Id: clone_blocks.java,v 1.4 2013/01/17 11:15:09 stm Exp $
 *
 * Clone Blocks from another document. Use POCA to copy a modified Block.
 * 
 * It is possible to copy all blocks on a page, or blocks can be copied
 * selectively. The program demonstrates also how a Block can be retrieved
 * and written with modified properties.
 * 
 * Required software: PPS 9
 * Required data: PDF document with Blocks
 */

package com.pdflib.cookbook.pdflib.blocks;

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

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

        String outfile = "clone_blocks.pdf";
        String infile = "businesscard_blocks.pdf";
        String title = "Clone PDFlib Blocks";

        pdflib p = null;

        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.4 $");

            /* Open a PDF containing blocks */
            int indoc = p.open_pdi_document(infile, "");
            if (indoc == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            /* Dummy page size, adjusted later */
            p.begin_page_ext(10, 10, "");

            /* Copy all blocks from the page */
            if (p.process_pdi(indoc, 0,
                "action=copyallblocks block={pagenumber=1}") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Import the page content itself */
            p.fit_pdi_page(page, 0, 0, "adjustpage");

            p.end_page_ext("");

            /* Dummy page size, adjusted later */
            p.begin_page_ext(10, 10, "");

            /*
             * Query all the Blocks on the page with pCOS, and copy all blocks
             * except one. With POCA, create a Block with different properties
             * instead of the original block.
             */
            String block_to_modify = "business_title";
            int block_count = (int) p.pcos_get_number(indoc,
                "length:pages[0]/blocks");
            int i;

            /* Create the Block dictionary */
            int blockdict = p.poca_new("containertype=dict usage=blocks");

            for (i = 0; i < block_count; i += 1) {
                String block_name = p.pcos_get_string(indoc, "pages[0]/blocks["
                    + i + "].key");

                if (block_name.equals(block_to_modify)) {
                    /*
                     * Create a new block instead of copying the one from the
                     * input document. Retrieve the coordinates and modify them.
                     * Add more properties.
                     */
                    double llx = p.pcos_get_number(indoc, "pages[0]/blocks["
                        + i + "].val/Rect[0]");
                    double lly = p.pcos_get_number(indoc, "pages[0]/blocks["
                        + i + "].val/Rect[1]");
                    double urx = p.pcos_get_number(indoc, "pages[0]/blocks["
                        + i + "].val/Rect[2]");
                    double ury = p.pcos_get_number(indoc, "pages[0]/blocks["
                        + i + "].val/Rect[3]");

                    /*
                     * Shift the block downwards and enlarge it.
                     */
                    lly -= 20;
                    ury -= 10;

                    int textblock = p.poca_new("containertype=dict "
                        + "usage=blocks type=name key=Type value=Block");

                    int container1 = p.poca_new("containertype=array "
                        + "usage=blocks type=float values={" + llx + " " + lly
                        + " " + urx + " " + ury + "}");

                    p.poca_insert(textblock, "type=array key=Rect value="
                        + container1);

                    p.poca_insert(textblock, "type=name key=Name value="
                        + block_to_modify);
                    p.poca_insert(textblock,
                        "type=name key=Subtype value=Text");
                    p.poca_insert(textblock,
                        "type=name key=fitmethod value=auto");
                    p.poca_insert(textblock,
                        "type=name key=fontname value=Courier");
                    p.poca_insert(textblock,
                                    "type=float key=fontsize value=20");
                    p.poca_insert(textblock, "type=string key=defaulttext "
                        + "value={Block created with POCA}");

                    int container2 = p.poca_new("containertype=dict "
                        + "usage=blocks type=integer key=format value=5");

                    p.poca_insert(textblock, "type=dict key=Custom " + "value="
                        + container2);

                    /*
                     * Hook up the Block in the page's Block dictionary. The
                     * PDFlib Block specification requires that the key of the
                     * dictionary entry is identical to the "Name" value inside
                     * the block dictionary
                     */
                    p.poca_insert(blockdict, "type=dict key=" + block_to_modify
                        + " value=" + textblock);
                }
                else {
                    /*
                     * Copy the block.
                     */
                    if (p.process_pdi(indoc, 0,
                        "action=copyblock block={pagenumber=1 blockname={"
                            + block_name + "}}") == -1)
                        throw new Exception("Error: " + p.get_errmsg());
                }
            }

            /* Import the page content itself */
            p.fit_pdi_page(page, 0, 0, "adjustpage");

            p.end_page_ext("blocks=" + blockdict);

            p.end_document("");
        }
        catch (PDFlibException e) {
            System.err.print("PDFlib exception occurred in hello sample:\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();
            }
        }
    }
}