PDFlib

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

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

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

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

ページにスタンプを追加

既存の PDF 文書からすべてのページをインポートして、スタンプを重ねて出力します。

必要な製品:PDFlib+PDI または PDFlib PPS


/*
 * Stamp PDF pages:
 * Import all pages from an existing PDF document and place a stamp somewhere
 * on the page
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF document
 */
package com.pdflib.cookbook.pdflib.pdf_import;

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

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

    pdflib p = null;
    String pdffile = "PDFlib-real-world.pdf";
    int indoc, pageno, endpage, page, font;
    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);

        /* Open the input PDF */
        indoc = p.open_pdi_document(pdffile, "");
        if (indoc == -1)
        throw new Exception("Error: " + p.get_errmsg());

        endpage = (int) p.pcos_get_number(indoc, "length:pages");

        /* Loop over all pages of the input document */
        for (pageno = 1; pageno <= endpage; pageno++)
        {
        page = p.open_pdi_page(indoc, pageno, "");

        if (page == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Page size may be adjusted by fit_pdi_page(). */
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

        /* Place the imported page on the output page, and
         * adjust the page size
         */
        p.fit_pdi_page(page, 0, 0, "adjustpage");

        font = p.load_font("Helvetica-Bold", "unicode", "");

        if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Fit the text line like a stamp with red outlines in the
         * specified box. The stamp will be placed diagonally from the 
         * upper left to the lower right.
         */
        p.fit_textline("PRELIMINARY", 50, 50, "font=" + font +
           " fontsize=1 textrendering=1 boxsize={500 700} stamp=ul2lr" +
           " strokecolor=red strokewidth=2");

        p.close_pdi_page(page);

        p.end_page_ext("");
        }

        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);
        }
    }
}
(Dec 11, 2012 - Sep 30, 2022)