PDFlib

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

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

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

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

テキストのウェブリンク

PDFlib で、テキストフローを作成して、テキストにウェブリンクを色づけして割り当てるサンプルプログラムです。

ウェブリンクテキストを表示するためには、matchbox および matchbox end インラインオプションを使って、マッチボックスを定義します。

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


/*
 * Weblink in Text:
 * Create a Textflow and integrate colorized Web links in the text
 *
 * Using the inline options "matchbox" and "matchbox end" define
 * matchboxes which are used to indicate the pieces of text on which the
 * Web links are to be placed.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

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

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

        pdflib p = null;
        int act, tf = -1;
        String result, optlist, text;
        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");

            /* Set an output path according to the name of the topic */
            if (p.begin_document(outfile, "") == -1)
            throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /* Create page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* Create and fit a Textflow with two Web links. Using the inline
             * options "matchbox" and "matchbox end" define the two matchboxes
             * "kraxiweb" and "kraximail" which are used to indicate the pieces of
             * text on which the Web links are to be placed. Provide the matchboxes
             * with particular text color and decoration.
             */
            text =
                "For more information about the Giant Wing Paper Plane see the " +
                "Web site of <underline=true fillcolor={rgb 0 0.5 0.5} " +
                "strokecolor={rgb 0 0.5 0.5} " +
                "matchbox={name=kraxiweb boxheight={fontsize descender}}>Kraxi " +
                "Systems, Inc.<matchbox=end underline=false fillcolor={rgb 0 0 0}" +
                " strokecolor={rgb 0 0 0}> or contact us by email via " +
                "<matchbox={name=kraximail fillcolor={rgb 0 0.8 0.8} " +
                "boxheight={ascender descender}}>questions@kraxi.com" +
                "<matchbox=end fillcolor={rgb 0 0 0}>. You'll get all " +
                "information about how to fly the Giant Wing in a thunderstorm " +
                "as soon as possible.";

            optlist =
                "fontname=Helvetica fontsize=20 encoding=unicode leading=140%";

            tf = p.create_textflow(text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 100, 200, 500, 500, "fitmethod=auto");
            if (!result.equals("_stop"))
            {
                /* Check for errors */
            }

            /* Create URI action */
            optlist = "url={http://www.kraxi.com}";
            act = p.create_action("URI", optlist);

            /* Create Link annotation on matchbox "kraxiweb" */
            optlist = "action={activate " + act + 
                "} linewidth=0 usematchbox={kraxiweb}";
            p.create_annotation(0, 0, 0, 0, "Link", optlist);

            /* Create URI action */
            optlist = "url={mailto:questions@kraxi.com}";
            act = p.create_action("URI", optlist);

            /* Create Link annotation on matchbox "kraximail" */
            optlist = "action={activate " + act + 
                "} linewidth=0 usematchbox={kraximail}";
            p.create_annotation(0, 0, 0, 0, "Link", optlist);

            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.toString());
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}
(Apr 3, 2007 - Oct 21, 2022)