PDFlib

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

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

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

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

テキストを PDF/A-2b へ

PDF/A-2b をカラースペースやフォントに注意してテキストを出力します。


/* $Id: text_to_pdfa2b.java,v 1.2 2013/08/07 13:33:01 rp Exp $
 * Text to PDF/A:
 * Output text conforming to PDF/A-2b, taking care of color space and font
 * issues
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font file
 */
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

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

        pdflib p = null;
        int x = 30, y = 800;

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

            /* Output all contents conforming to PDF/A-2b */
            if (p.begin_document(outfile, "pdfa=PDF/A-2b") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title + " $Revision: 1.2 $");

            /*
             * Use sRGB as output intent since it allows the color spaces
             * CIELab, ICC-based, Grayscale, and RGB
             */
            p.load_iccprofile("sRGB", "usage=outputintent");

            p.begin_page_ext(595, 842, "");

            /*
             * Load the font "LuciduxSans-Oblique" with embedding, since font
             * embedding is required for PDF/A
             */
            int font = p.load_font("LuciduxSans-Oblique", "unicode",
                    "embedding");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.setfont(font, 20);

            /*
             * You might want to embed a PDF core font, e.g. "Helvetica". For a
             * PDF core font, the font metrics are already built into PDFlib,
             * but the font outline file has to be explicitly configured with
             * the "FontOutline" ressource category, e.g.
             * 
             * p.set_option("FontOutline={{Helvetica=HV______.pfb}}");
             */

            /*
             * We can use RGB text without any further color related options
             * since we already supplied an output intent profile.
             */
            p.setcolor("fill", "rgb", 0.7, 0.3, 0.3, 0);

            p.fit_textline("Text with an RGB color conforming to PDF/A-2b",
                            x, y -= 100, "");

            /*
             * Similarly, we can use Grayscale text without any further options
             * since we already supplied an output intent profile.
             */
            p.setcolor("fill", "gray", 0.5, 0, 0, 0);

            p.fit_textline("Text with a Grayscale color conforming to PDF/A-2b",
                            x, y -= 100, "");

            /*
             * For CMYK text we could use an ICC profile explicitly assigned;
             * the code would be as follows:
             * 
             * icc = p.load_iccprofile("ISOcoated.icc", "usage=iccbased");
             * p.set_value("setcolor:iccprofilecmyk", icc);
             * p.setcolor("fill", "iccbasedcmyk", 0.6, 0.6, 0, 0);
             * p.fit_textline(
             *     "Text with a CMYK color conforming to PDF/A-2b",
             *     x, y-=100, "");
             */

            p.end_page_ext("");

            p.end_document("");
        }
        catch (PDFlibException e) {
            System.err.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname()
                    + ": " + e.get_errmsg() + "\n");
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
        finally {
            if (p != null) {
                p.delete();
            }
        }
    }
}
(Apr 3, 2007 - Feb 20, 2014)