PDFlib Cookbook

cookbook

pagination/crop_page

Crop an A4 page to A5 format.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Crop page:
 * Crop an A4 page to an A5 page
 *
 * Create a page with A4 format and Portrait orientation and crop it to an A5 page.
 * Open the A5 page with the same zoom as chosen for the A4 page to fit completely
 * into the window.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
package com.pdflib.cookbook.pdflib.pagination;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

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

	pdflib p = null;
	String optlist;
	int font, action;
	int exitcode = 0;
     
	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);

	    font = p.load_font("Helvetica-Bold", "unicode", "");
	    if (font == -1)
		throw new Exception("Error: " + p.get_errmsg());
	    

	    /* ----------------------------------
	     * Create the first page in A4 format
	     * ----------------------------------
	     */
	    p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
	    
	    /* Draw a violet rectangle in A5 format */
	    p.setcolor("fill", "rgb", 0.7, 0, 0.6, 0);
	    p.rect(87, 123, 421, 595);
	    p.fill();
	    
	    /* Output some explanatory text */
	    p.fit_textline("A4 page uncropped", 50, 50, 
		"font=" + font + " fontsize=20  fillcolor={gray 0}");
	    
	    /* Create a "GoTo" action for opening the current page (page=0) with
	     * a zoom to completely fit it into the window (type=fitwindow) 
	     */
	    optlist = "destination={page=0 type=fitwindow}";
	    action = p.create_action("GoTo", optlist);
	    
	    /* Finish the page with the "GoTo" action being applied. When the page
	     * is opened in the viewer it will completely fit into the window.
	     */
	    p.end_page_ext("action={open=" + action + "}");
	    
	    
	    /* -------------------------------------------------------------
	    /* Create the second page in A4 format and crop it to an A5 page
	     * -------------------------------------------------------------
	     */
	    p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
	    
	    /* Draw a violet rectangle in A5 format */
	    p.setcolor("fill", "rgb", 0.7, 0, 0.6, 0);
	    p.rect(87, 123, 421, 595);
	    p.fill();
	    
	    /* Output some explanatory text */
	    p.fit_textline("A4 page cropped to A5", 95, 140, 
		"font=" + font + " fontsize=20 fillcolor={gray 0}");
	    
	    /* Create a "GoTo" action for opening the current page with the same
	     * zoom as chosen for the A4 page. This is accomplished by supplying
	     * the size of the A4 page as the rectangle to completely fit into
	     * the window.
	     */
	    optlist = "destination={page=0 type=fitrect left=0 bottom=0 " +
		"right=595 top=842}";
	    action = p.create_action("GoTo", optlist);
	    
	    /* Finish the page and crop it to the A5 format. In addition, the
	     * "GoTo" action is applied. When the page is opened in the viewer it
	     * will be displayed with the same zoom as the A4 page.
	     */
	    p.end_page_ext("cropbox={87 123 508 718} action={open=" + action + "}");
	    
	    p.end_document("");

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