PDFlib

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

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

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

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

複数のページから PDFlib ブロックをコピー

複数のページから PDFlib ブロックをコピーします。

PDFlib ブロックを含む PDF のページをインポートし、fit_pdi_page() で出力の1ページに配置します。現在のページのテキストブロックにテキストを配置した後、後続のページを "blind" オプションを与えて配置します。これにより後から配置したページの内容は表示されませんが、そのページにある PDFlib ブロックにテキスト等を配置することができます。配置する際は、それぞれのページハンドルを指定します。


/*
 * Duplicate block:
 * Duplicate a PDFlib block to any number of pages
 * 
 * Import a PDF page containing a block and place it with fit_pdi_page() on
 * the first page of the output document. Fill the contained text block with the 
 * number of the current page.
 * On subsequent pages, place the imported PDF page with the "blind" option 
 * supplied so that the actual page contents will not be placed, but the block
 * can be filled nevertheless. Fill the text block with the number of the
 * respective page.
 * 
 * Required software: PPS 9
 * Required data: PDF document containing blocks
 */
package com.pdflib.cookbook.pdflib.blocks;

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

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

    pdflib p = null;
    double width, height;
    String infile = "announcement_blocks.pdf";
    int inpage, indoc;
    int exitcode = 0;
     
    
    /* Name of the block contained on the imported page */
    final String blockname = "page"; 
     
    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 a block to store the page number */
        indoc = p.open_pdi_document(infile, "");
        if (indoc == -1)
        throw new Exception("Error: " + p.get_errmsg());
        
        /* Open the first page of the imported document */
        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 */
        p.fit_pdi_page(inpage, 0, 0, "");
        
        /* Fill the text block with the number of the first page */
        p.fill_textblock(inpage, blockname, "1",
        "encoding=unicode");
        
        /* Finish page */
        p.end_page_ext("");
        
            
        /* -----------------------------------------------
         * Duplicate the block on several subsequent pages
         * -----------------------------------------------
         */
        for (int i = 2; i <= 5; i++)
        {
        /* 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 but supply the "blind"
         * option to suppress the actual page contents. The block is
         * available to be filled nevertheless.
         */
        p.fit_pdi_page(inpage, 0, 0, "blind");
        
        /* Fill the text block with the number of the current page */
        p.fill_textblock(inpage, blockname, String.valueOf(i),
            "encoding=unicode");
        
        /* Output some descriptive text */
        String optlist = "fontname=Helvetica fontsize=16 encoding=unicode";
        String text = "Page " + i + ": The page number is filled " +
            "in the block duplicated from page 1";
        
        p.fit_textline(text, 30, 400, optlist);
        
        /* Finish page */
        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 - May 23, 2019)