PDFlib

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

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

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

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

テキストブロックの関連付け

複数のテキストフローブロックを関連付けます。ある製品について複数のブロックを含む広告テンプレートを表示している PDF ページをインポートします。一部の製品の広告では、テキストフローの長さに応じて適切な数のブロックを埋めています。


/*
 * Linked text blocks:
 * Link multiple Textflow blocks
 * 
 * Import a PDF page representing an advertisement template containing multiple
 * blocks for one product offer each. Several product offers represented in a 
 * Textflow are filled into the appropriate number of blocks depending on the
 * length of the Textflow.
 * 
 * Required software: PPS 10
 * Required data: PDF document containing blocks
 */
package com.pdflib.cookbook.pdflib.blocks;

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

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

        /* By default annotations are also imported. In some cases this
         * requires the Noto fonts for creating annotation appearance streams.
         * We therefore set the searchpath to also point to the font directory.
         */
        String fontpath = "../resource/font";
        String outfile = "linked_textblocks.pdf";
        String title = "Linked Text Blocks";

        pdflib p = null;
        double width, height;
        String infile = "advertisement_blocks.pdf";
        int tf, i, inpage, indoc;
        String optlist;
        int exitcode = 0;
        
        /* Number of Textflow blocks which can be filled */
        final int nblocks = 6;
        
        /* Name prefix of the blocks contained on the imported page */
        final String blockname = "model_"; 
        
        final String models = 
            "Our paper planes are the ideal way of passing the time. We offer " +
            "revolutionary new developments of the traditional common paper " +
            "planes. If your lesson, conference, or lecture turn out to be " +
            "deadly boring, you can have a wonderful time with our planes. All " +
            "our models are folded from one paper sheet. They are exclusively " +
            "folded without using any adhesive. Several models are equipped with " +
            "a folded landing gear enabling a safe landing on the intended " +
            "location provided that you have aimed well. Other models are able " +
            "to fly loops or cover long distances. Let them start from a vista " +
            "point in the mountains and see where they touch the ground. Have a " +
            "look at our new paper plane models! With our Long Distance Glider " +
            "you can send all your messages even when sitting in a hall or in " +
            "the cinema pretty near the back. Try our Giant Wing, an " +
            "unbelievable sailplane!";
     
        try {
            p = new pdflib();

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

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

            /* 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, "");
            
            /* Fill one or more blocks with the Textflow */ 
            String text = models;
            
            tf = -1;
             
            for (i = 1; i <= nblocks; i++)
            {
                /* Option list for text blocks; the encoding should be defined
                 * and the Textflow handle supplied. In addition we slightly
                 * rotate the block rectangles by 3 degrees. Other options have
                 * already been set in the properties of the blocks contained in
                 * the input document, such as:
                 * "fitmethod=clip" to clip the text when it doesn't fit completely
                 * into the block while avoiding any text shrinking.
                 * "margin=4" to set some space between the text and the borders of
                 * the block rectangle.
                 * "fontsize=16" for a font size of 16.
                 * "backgroundcolor={gray 0.9}" to colorize the block rectangle with
                 * a light gray.
                 */
                optlist = "rotate=3 textflowhandle=" + tf;
                
                tf = p.fill_textblock(inpage, blockname+i, text, optlist);
                    
                text = "";
                    
                if (tf == -1) {
                    System.err.println("Warning: " + p.get_errmsg());
                    break;
                }
                    
                /* Check result of most recent call to fit_textflow() */
                int reason = (int) p.info_textflow(tf, "returnreason");
                String result = p.get_string(reason, "");
                    
                /* End loop if all text was placed */
                if (result.equals("_stop"))
                {
                    p.delete_textflow(tf);
                    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);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}
(Dec 11, 2012 - Jun 24, 2024)