PDFlib

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

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

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

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

URL からのイメージ

PDFlib で、URL からイメージを読み、PDF 文書に配置するサンプルプログラムです。

URL からイメージを読み込み、PDFlib 仮想ファイル (PVF) に格納します。PVF からイメージデータを読み込み、ページ上に配置します。

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


/*
 * Image from URL:
 * Read an image from an URL and place it in a PDF document
 * 
 * Read an image from the URL and store into a PDFlib virtual file (PVF). 
 * Then, load the image data from the PVF and place it on the page.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.images;

import java.io.*;
import java.net.*;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

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

        pdflib p = null;
        int exitcode = 0;
        final String image_url = "https://www.pdflib.com/fileadmin/cookbooks/PDFlib/PDFlib-cookbook/input/PDFlib-logo.tif";
        byte[] imageData = null;
        URL url = null;

        try {
            byte[] buffer = new byte[1024];

            /* Read an image from a Web site */
            url = new URL(image_url);

            InputStream inp = url.openStream();
            ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();

            int bytesRead;
            while ((bytesRead = inp.read(buffer)) != -1) {
                bufferStream.write(buffer, 0, bytesRead);
            }

            imageData = bufferStream.toByteArray();

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

                /*
                 * Store the image in a PDFlib virtual file (PVF) called "/pvf/image"
                 */
                p.create_pvf("/pvf/image", imageData, "");

                /* Load the image from the PVF */
                int image = p.load_image("auto", "/pvf/image", "");
                if (image == -1)
                    throw new Exception("Error: " + p.get_errmsg());

                /* Start a page, place the image, and finish the page */
                p.begin_page_ext(400, 200, "");
                p.fit_image(image, 50, 100, "");
                p.end_page_ext("");

                /* Delete the virtual file to free the allocated memory */
                p.delete_pvf("/pvf/image");

                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);
            }
        } catch (IOException e) {
            System.err.println("Exception occurred: URL " + image_url + " is not available");
            e.printStackTrace();
            exitcode = 1;
            System.exit(exitcode);
        }
    }
}
(Apr 3, 2007 - Oct 21, 2022)
<