PDFlib Cookbook

cookbook

pdf_import/clip_imported_page new

Clip imported PDF page.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Clip imported PDF page:
 *
 * Use the "matchbox" option with the "clipping" suboption to place some
 * part of an imported PDF page. The following variants are demonstrated:
 * - clip a rectangular area of the page and place it in its original scaling.
 * - clip a rectangular area of the page and force-fit into a box with
 *   specified size.
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: input PDF
 */
package com.pdflib.cookbook.pdflib.pdf_import;

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

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

        pdflib p = null;
        int exitcode = 0;
        String pdffile = "lionel.pdf";
        
        try {
            int doc, page, gstate, font;
            String clipping_optlist, text;
            int llx = 50, lly = 550;

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

            /* Load page of the input PDF document */
            doc = p.open_pdi_document(pdffile, "");
            if (doc == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            page = p.open_pdi_page(doc, 1, "");
            if (page == -1)
                throw new Exception("Error: " + p.get_errmsg());

            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
       
            /* --------------------------------------------------------
             * Output the original page with transparency as background
             * --------------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
            
            p.setfont(font, 10);
            
            /* Output some descriptive text */
            text = "Place the full PDF page in its original size with transparency (for comparison)";
            p.fit_textline(text, llx, 520, "");
            
            /* Create an extended graphics state with transparency */
            gstate = p.create_gstate("opacityfill=.4");
            
            /* Fit the PDF page in its original size with the transparency gstate */
            p.fit_pdi_page(page, llx, lly-=500, "gstate=" + gstate);
            
            
            /* ----------------------------------------------
             * Place the clipped page in its original size
             * ----------------------------------------------
             */
            
            /* Define a rectangular part of the PDF page with the "matchbox" option
             * and the "clipping" suboption to set the lower left and upper right
             * corners of the clipping rectangle.
             */
            int c_llx = 100, c_lly = 100, c_urx = 350, c_ury = 375;
            
            clipping_optlist = "matchbox={clipping={" + 
                c_llx + " " + c_lly + " " + c_urx + " " + c_ury + "}}";
            
            /* Output some descriptive text */
            text = "Place the clipped PDF page in its original size:";
            p.fit_textline(text, llx, 505, "");
            
            text = "p.fit_pdi_page(page, " + (llx + c_llx) + " , " + (lly + c_lly) +
                ", \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx, 490, "");
                      
            /* Display the clipped area at the same position where it would
             * appear when displaying the complete PDF page.
             */
            p.fit_pdi_page(page, llx + c_llx, lly + c_lly, clipping_optlist);

            p.end_page_ext("");

            
            /* -----------------------------------------------------
             * Display the clipped area in a box with specified size
             * -----------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
              
            p.setfont(font, 10);
      
            lly = 550;
                         
            /* Define a rectangular part of the PDF page with the "matchbox" option
             * and the "clipping" suboption. The lower left and upper right corners
             * of the rectangle are specified as percentages of the original PDF page size. 
             */
            clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}}";
            
            text = "Place the clipped area in its original size:";
            p.fit_textline(text, llx, lly-=30, "");
            
            text = "p.fit_pdi_page(page, x, y, \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx, lly-=15, "");
            
            /* Place the clipped PDF page */
            p.fit_pdi_page(page, llx, lly-=220, clipping_optlist);


            /* Display the clipped PDF page in a box of the specified size. Scale the
             * clipped PDF page proportionally so that it fits into the specified box
             * with "fitmethod=meet".
             */
            clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}} " +
                "boxsize={200 100} fitmethod=meet showborder";
            
            /* Output some descriptive text */
            text = "Place the clipped area in a specified box:";
            p.fit_textline(text, llx, lly-=50, "");
            
            text = "p.fit_pdi_page(page, x, y, \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx, lly-=15, "");
            
            p.fit_pdi_page(page, llx, lly-=120, clipping_optlist);
            
            p.end_page_ext("");
            
            p.close_pdi_page(page);
            p.close_pdi_document(doc);
            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);
        }
    }
        
}