PDFlib Cookbook

cookbook

blocks/create_blocks_with_poca

Create PDFlib Blocks with the POCA interface.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Create PDFlib Blocks with the POCA interface
 * 
 * Required software: PPS 9
 * Required data: none
 */

package com.pdflib.cookbook.pdflib.blocks;

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

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

        String outfile = "create_blocks_with_poca.pdf";
        String title = "Create PDFlib Blocks with POCA";

        int font;
        int exitcode = 0;
        pdflib p = null;

        try {
            p = new pdflib();
            int textblock, imageblock, pdfblock;
            int blockdict;
            int container1, container2, container3, container4;
            String blockname;

            /* This means we must check return values of load_font() etc. */
            p.set_option("errorpolicy=return");

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

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

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

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            font = p.load_font("NotoSerif-Bold", "unicode", "");

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

            p.fit_textline(
                "This document contains PDFlib Blocks generated with POCA",
                50, 800, "font=" + font + " fontsize=12");

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


            /* ---------------------------------------------------------
             * Create a Text Block
             * ---------------------------------------------------------
             */
            blockname = "job_title";
            
            textblock = p.poca_new("containertype=dict usage=blocks " +
                "type=name key=Type value=Block");

            container1 = p.poca_new("containertype=array usage=blocks " +
                "type=integer values={70 640 300 700}");

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

            p.poca_insert(textblock, "type=name key=Name value=" + blockname);
            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=string key=fontname value=NotoSerif-Regular");
            p.poca_insert(textblock,
                "type=string key=defaulttext value={Block created with POCA}");
            p.poca_insert(textblock, "type=float key=fontsize value=12");
            p.poca_insert(textblock,
                "type=percentage key=horizscaling value=105%");
            p.poca_insert(textblock,
                "type=color key=fillcolor value={rgb 0 0.3 0.57}");

            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=" + blockname 
                + " direct=false value=" + textblock);


            /* ---------------------------------------------------------
             * Create an Image Block
             * ---------------------------------------------------------
             */
            blockname = "logo";
            
            imageblock = p.poca_new("containertype=dict usage=blocks " +
                "type=name key=Type value=Block");

            container3 = p.poca_new("containertype=array usage=blocks " +
                "type=integer values={70 440 300 600}");

            p.poca_insert(imageblock, "type=array key=Rect value="
                                                            + container3);

            p.poca_insert(imageblock, "type=name key=Name value=" + blockname);
            p.poca_insert(imageblock, "type=name key=Subtype value=Image");
            p.poca_insert(imageblock, "type=name key=fitmethod value=auto");

            /* Hook up the Block in the page's Block dictionary */
            p.poca_insert(blockdict, "type=dict key=" + blockname
                + " direct=false value=" + imageblock);


            /* ---------------------------------------------------------
             * Create a PDF Block
             * ---------------------------------------------------------
             */
            blockname = "pdflogo";
            
            pdfblock = p.poca_new("containertype=dict usage=blocks " +
                "type=name key=Type value=Block");

            container4 = p.poca_new("containertype=array usage=blocks " +
                "type=integer values={70 240 300 400}");

            p.poca_insert(pdfblock, "type=array key=Rect value=" + container4);

            p.poca_insert(pdfblock, "type=name key=Name value=" + blockname);
            p.poca_insert(pdfblock, "type=name key=Subtype value=PDF");
            p.poca_insert(pdfblock, "type=name key=fitmethod value=meet");
            
            /* Hook up the Block in the page's Block dictionary */
            p.poca_insert(blockdict, "type=dict key=" + blockname
                + " direct=false value=" + pdfblock);

            /* Hook up the Block dictionary in the current page */
            p.end_page_ext("blocks=" + blockdict);

            p.end_document("");

            /* Clean up */
            p.poca_delete(blockdict, "recursive=true");
        }
        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);
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}