PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib サンプル集(クックブック)

本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした PDFlib は、一部機能の制限を除き、評価版として無償でお使いいただけます。

ブロック矩形の上書き

ブロック矩形を上書きします。矩形の座標を変えながら、インポートした PDF ページ内の PDFlib ブロックを埋めていき、出力します。ブロックのうち "name", "business_address", "business_city" にはフォント、フォントサイズ、色が定義されています。加えて、フォントの増加により、ブロック矩形が他の矩形と重複しないように、ブロック矩形は移動されます。


/*
 * Override block rectangle:
 * Fill some blocks of an imported PDF page while changing the rectangle 
 * coordinates.
 * 
 * Output an imported PDF page containing blocks. The blocks "name", 
 * "business_address", and "business_city" have a defined font, font size, and
 * color. Fill those blocks by using a different font and a larger font size.
 * In addition, due to the increased font, increase the block rectangle and
 * move its position to prevent it from overlapping with the other blocks.   
 * 
 * Required software: PPS 9
 * Required data: PDF document containing blocks, font
 */
package com.pdflib.cookbook.pdflib.blocks;

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

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

    pdflib p = null;
    double width, height;
    String infile = "stationery_blocks.pdf";
    int inpage, indoc;
    String blockname = "", blocktype = "";
    String text_optlist;
    double block_llx, block_lly, block_urx, block_ury;
    double newblock_llx, newblock_lly, newblock_urx, newblock_ury;
    int i, j, blockcount;
    double blockfontsize, newfontsize, enlarge;
    int exitcode = 0;
    
    final double incrfactor = 3;
    final int percentage = 30;
     
    final int nblocks = 3; // number of blocks with properties to be overridden
    
    /* Names of blocks with properties to be overridden */
    String blocknames[] = {
        "name", "business_address", "business_city" 
    };
    
    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);
        
        /* Open a PDF containing blocks */
        indoc = p.open_pdi_document(infile, "");
        if (indoc == -1)
        throw new Exception("Error: " + p.get_errmsg());
        
        /* Open the first page */
        inpage = p.open_pdi_page(indoc, 1, "");
        if (inpage == -1)
        throw new Exception("Error: " + p.get_errmsg());
        
        /* Get the width and height of the imported page */
        width = p.pcos_get_number(indoc, "pages[0]/width");
        height = p.pcos_get_number(indoc, "pages[0]/height");
        
        /* Start the output page with the size given by the imported page */ 
        p.begin_page_ext(width, height, "");

        /* Place the imported page on the output page */
        p.fit_pdi_page(inpage, 0, 0, "");
        
        /* Retrieve the number of blocks contained on the first page (which is
         * page no. 0) of the PDF opened
         */     
        blockcount = (int) p.pcos_get_number(indoc,
        "length:pages[0]/blocks");

        if (blockcount == 0)
        throw new Exception("Error: " + infile +
            "does not contain any PDFlib blocks");
        
        /* For three Text blocks on the first page, increase
         * the font size by 20%. Increase the size of the block rectangle by
         * a defined percentage and move the block down to prevent it from
         * overlapping with another block.
         */
        for (i = 0; i < blockcount; i++)
        {
        /* Get the name of the block */
        blockname = p.pcos_get_string(indoc,
            "pages[0]/blocks[" + i + "]/Name");
        
        /* Get the type of the block */
        blocktype = p.pcos_get_string(indoc,
            "pages[0]/blocks[" + i + "]/Subtype");
        
        for (j = 0; j < nblocks; j++) {
            if (blockname.equals(blocknames[j])) {
            /* Check if it is a Text block */
            if (blocktype.equals("Text"))
            {
            /* Get the font size of the block */
            blockfontsize = p.pcos_get_number(indoc,
                "pages[0]/blocks[" + i + "]/fontsize");
                
            /* Retrieve the rectangle coordinates of the block */
            block_llx = (int) p.pcos_get_number(indoc,
                "pages[0]/blocks[" + i + "]/Rect[0]");
            
            block_lly = (int) p.pcos_get_number(indoc,
                "pages[0]/blocks[" + i + "]/Rect[1]");
            
            block_urx = (int) p.pcos_get_number(indoc,
                "pages[0]/blocks[" + i + "]/Rect[2]");
            
            block_ury = (int) p.pcos_get_number(indoc,
                "pages[0]/blocks[" + i + "]/Rect[3]");
            
            /* Increase the font size by the percentage defined */
            newfontsize = blockfontsize * (percentage + 100) / 100;
            
            /* Specify the enlargement by the difference between the old
             * and the new font size multiplied by an increment factor 
             */
            enlarge = incrfactor * (newfontsize - blockfontsize);
            
            /* Increase the block rectangle by the enlargement 
             * specified. In addition, move the rectangle down by j
             * times the enlargment to prevent the blocks from 
             * overlapping.
             */
            newblock_llx = block_llx;
            newblock_lly = block_lly - (1 + j) * enlarge;
            newblock_urx = block_urx + enlarge;
            newblock_ury = block_ury - j * enlarge;
            
            text_optlist = 
                "fontname=NotoSerif-Regular encoding=unicode fontsize=" + 
                newfontsize +
                " refpoint={" + block_llx + " " + newblock_lly + "}" + 
                " boxsize={" + (newblock_urx - newblock_llx) + " " + 
                (newblock_ury - newblock_lly) + "}" +
                " fillcolor={rgb 0 0.7 0}";
            
            if (p.fill_textblock(inpage, blockname, blockname, 
                text_optlist) == -1)
                System.err.println("Warning: " + p.get_errmsg());
            }
            break;
            }
        }
        }
     
        p.end_page_ext("");

        p.close_pdi_page(inpage);

        p.end_document("");
        p.close_pdi_document(indoc);

        } 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.toString());
        exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
        System.exit(exitcode);
        }
    }
}
(Dec 11, 2012 - May 23, 2019)