PDFlib

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

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

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

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

透明グラフィック

PDFlib で、いくつかの透明グラフィックを作成するサンプルプログラムです。

黄色の矩形を定義して、オーバーラップする透明グラフィックオブジェクトをいくつか表示します。opacityfill オプションを 1 未満の値に設定した拡張グラフィックステートにより透過が作られます。

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


/*
 * Transparent graphics:
 * Create some transparent graphics objects
 *
 * Display a yellow rectangle and show some transparent graphics objects
 * overlapping with it. Create transparency by applying an extended graphics
 * state with the "opacityfill" option set to a value less than 1.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.graphics;

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

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

        pdflib p = null;
        
        int gstate, 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);
            
            /* Load the font. */
            font = p.load_font("Helvetica", "unicode", "");
            if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

            /* Start page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* Draw a yellow background rectangle */
            p.setcolor("fill", "rgb", 1, 0.9, 0.58, 0);
            p.rect(50, 50, 500, 750);
            p.fill();
            
            /* Display some descriptive red text */
            p.setcolor("fill", "rgb", 0.73, 0.12, 0.30, 0);
            p.fit_textline("Transparent vector graphics", 100, 100, "font=" + font +
                " fontsize=20");
            
            /* Save the current graphics state. The save/restore of the current
             * state is not necessarily required, but it will help you get back to
             * a graphics state without any transparency.
             */
            p.save();
            
            /* Create an extended graphics state with transparency set to 50%, using
             * create_gstate() with the "opacityfill" option set to 0.5.
             */
            gstate = p.create_gstate("opacityfill=.5");
            
            /* Apply the extended graphics state */
            p.set_gstate(gstate);
            
            /* Draw a blue rectangle which will be transparent according to the
             * graphics state set
             */
            p.setcolor("fill", "rgb", 0, 0.52, 0.64, 0);
            p.rect(100, 500, 200, 600);
            p.fill();
            
            /* Draw a red circle which will be transparent according to the
             * graphics state set
             */
            p.setcolor("fill", "rgb", 0.73, 0.12, 0.30, 0);
            p.arc(100, 100, 200, 0, 360);
            p.fill();
            
            /* Restore the current graphics state */
            p.restore();
            
            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);
        }
    }
}
(Apr 3, 2007 - Oct 23, 2022)