PDFlib

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

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

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

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

PDFlib で、文字列に脚注番号をつけ、文末の脚注へのリンクを設定するサンプルプログラムです。

脚注番号テキストフローを配置します。テキストフローは、それぞれの脚注番号に対応した脚注のためのテキスト行を配置します。脚注番号は、脚注に跳ぶリンクとして参照を提供します。

必要な製品:PDFlib および PDFlib+PDI および PPS

 
/*
 * Footnotes in Text:
 * Create footnotes (superscript text) in a Textflow provided with links to
 * jump to the footnote text. 
 * 
 * Place a Textflow with some superscript numbers representing footnote 
 * references. Below the Textflow place a text line for each footnote (starting
 * with a superscript number). Provide the footnote references with links to
 * jump to the footnote texts.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

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

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

        pdflib p = null;
        int exitcode = 0;

        String outfile = "footnotes_in_text.pdf";
        String title = "Footnotes in Text";
        int tf = -1, font, action;
        final double llx= 50, lly=50, urx=500, ury=800, lasty;
        String result;

        /* Define an option list including two macros to be used to start and 
         * finish a footnote
         */
        String optlist =
            "fontname=NotoSerif-Regular fontsize=14 " +
            "fillcolor={gray 0} alignment=justify charref leading=140% " +
            "macro {ft_start {textrise=60% fontsize=10 fontname=NotoSerif-Bold " +
             "fillcolor={cmyk 1 0.5 0.2 0} " +
            "matchbox={name=footnote margin=-6} } ft_end {textrise=0 " +
            "fontname=NotoSerif-Regular fontsize=14 " +
            "fillcolor={gray 0} matchbox=end} }";

        /* Text with some pieces of text included in the inline option "textrise"
         * which defines the percentage of the font size the text is shifted up.
         * To shift some text down use a negative textrise value.
         * The inline options matchbox and matchbox end are used to indicate the
         * footnote rectangles. They are used later to create hypertext links to
         * jump to the actual footnote text. 
         * Softhyphens are marked with the character reference "­" (character
         * references are enabled by the charref option).
         */
        final String text=
            "Our paper planes<&ft_start>1<&ft_end> are the ideal way " +
            "of passing the time. We offer re&shy;volu&shy;tionary new " +
            "develop&shy;ments 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 " +
            "fol&shy;ded from one paper sheet. They are exclu&shy;sively folded " +
            "with&shy;out using any adhesive. Several models are equipped with a " +
            "folded landing gear enabling a safe landing<&ft_start>2<&ft_end> on " +
            "the intended loca&shy;tion 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.";
        
        final String footnote1 = "For more information, see www.kraxi.com.";
        final String footnote2 = "For a safe landing strategy, see our How you " +
            "aim well paper.";
            
        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);
            
            /* Load the font */
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            p.setfont(font, 14);
            
            /* Just some informational text */
            p.fit_textline("Click on the superscripted blue footnote to zoom it " +
                "in!", llx, ury - 100, "fillcolor={cmyk 1 0.5 0.2 0}");
            
            /* Create and place a Textflow. Use the "textrise" inline option to
             * shift the footnote numbers upwards.
             */
            tf = p.create_textflow(text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            result = p.fit_textflow(tf, llx, lly-400, urx, ury-200, "");
            if (!result.equals("_stop")) {
                    /* Check for further action */
            }
            
            /* Retrieve the y coordinate of the current text position after having
             * placed the Textflow. 
             */
            lasty = p.info_textflow(tf, "textendy");
            
            p.delete_textflow(tf);
                    
            /* Create two text lines with the first shifted upwards. For subscript
             * text use a negative textrise value.
             */
            p.fit_textline("1", llx, lasty - 30, "textrise=60% fontsize=8");
            p.fit_textline(footnote1, llx+6, lasty - 30, "fontsize=10");
            
            /* Create two text lines with the first shifted upwards. (For subscript
             * text you would use a negative textrise value.)
             */
            p.fit_textline("2", llx, lasty - 45, "textrise=60% fontsize=8");
            p.fit_textline(footnote2, llx+6, lasty - 45, "fontsize=10");
            
            /* Create a "GoTo" action for jumping from the respective footnote 
             * reference to the footnote text. In addition, zoom-in the footnote
             * text and display it on the top left of the window.
             */
            optlist = "destination={page=1 type=fixed left=0 top=" + lasty +
                " zoom=150%}";
            action = p.create_action("GoTo", optlist);

            /* Create a Link annotation with the "GoTo" action on all rectangles
             * defined by the matchbox "footnote". 0 rectangle coordinates will
             * be replaced with matchbox coordinates.
             */
            optlist = "action={activate " + action + "} linewidth=0 " +
                      "usematchbox={{footnote}}";
            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 - Jun 20, 2024)