PDFlib Cookbook

cookbook

general/repeated_contents

Download Java Code      Switch to PHP Code     Show Output PDF

/* $Id: repeated_contents.java,v 1.8 2013/12/30 11:31:54 stm Exp $
 * Repeated contents:
 * Create contents which are used identically on multiple pages, such as fixed
 * headers or footers.
 * 
 * Create a PDFlib template (which will result in a PDF Form XObject) for
 * a business letter header which holds an image and some text. Then place
 * the template. When placing the template on multiple pages (or multiply on
 * the same page), the actual PDF operators for constructing the template are
 * only included once in the PDF file, thereby saving PDF output file size. 
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
package com.pdflib.cookbook.pdflib.general;

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

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

    pdflib p = null;
    String imagefile = "kraxi_header.tif";
    int i, j, font, templ, image;
    int x = 30, y;
    int pagewidth=595, pageheight=842;
    String[][] addresses =
    {
    	{ "Mr Bee", "3, Rose Gardens", "London" },
    	{ "Miss Hopper", "26, Shakespeare Road", "Hopperfield" },
    	{ "Mr Duck", "128, Chapel Hill", "Bournemouth" },
    	{ "Miss Haley", "50, Virginia Street", "Southport" }
    };

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

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

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

        /* Load the image to be contained in the template */
        image = p.load_image("auto", imagefile, "");

        if (image == -1)
        throw new Exception("Error: " + p.get_errmsg());
        
        /* Define a template with a business letter header containing the 
         * image loaded and some text for the sender's address
         */
        templ = p.begin_template_ext(pagewidth, pageheight, "");
        
        /* Place the image into the template */        
        p.fit_image(image, pagewidth-230, pageheight-140, 
            "boxsize={200 100} fitmethod=meet");
        
        /* Place the text into the template */
        p.fit_textline("Kraxi Systems, Inc., 17, Aviation Road, Paperfield",
            30, pageheight-160, "font=" + font + " fontsize=8");
        
        /* Finish the template */
        p.end_template_ext(0, 0);
        
        /* Close the image */
        p.close_image(image);
        
        /* Create four letters consisting of one page each: On each page, place
         * the template with the sender's address information, the individual
         * customer's address as well as some informative text
         */
        for (i = 0; i < 4; i++) {
            p.begin_page_ext(pagewidth, pageheight, "");
            y = pageheight - 165;
        
            /* Place the template on the page, just like using an image */
            p.fit_image(templ,  0.0,  0.0, "");
            
            /* Place the customer address */
            for (j = 0; j < 3; j++) {
            	 p.fit_textline(addresses[i][j], x, y-=15,
                         "font=" + font + " fontsize=12");     
            }
        
            /* Place the actual page contents on the page */
            i++;
            p.fit_textline("Dear customer, this is the actual page contents " +
                "of page " + i + ".", x, y-=80, "font=" + font +
                " fontsize=12");
            i--;
            p.fit_textline("The image and the sender's address above are " +
                "part of a template.", x, y -=20, "font=" + font +
                " fontsize=12");
 
            p.end_page_ext("");
        }
        
        /* If required, place the template on further pages. Then close it. */
        p.close_image(templ);

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