PDFlib

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

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

本サンプルプログラムは、PDF の情報を取得する pCOS インターフェースの基本的な機能を実際のプログラムで紹介したものです。

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

ページイメージ

pCOS インターフェースで、PDF 文書に含まれる全てのイメージについての情報を出力するサンプルプログラムです。


/*
 * Print information about all images in the document.
 *
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.pages;

import com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

public class page_images extends pcos_cookbook_example {

    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    public void example_code(IpCOS p, int doc) throws Exception {

        System.out.println("File name: " + p.pcos_get_string(doc, "filename"));

        int imagecount = 0;
        int pagecount = (int) p.pcos_get_number(doc, "length:pages");

        for (int page = 0; page < pagecount; page += 1) {
            imagecount += print_page_image_info(p, doc, page);
        }

        System.out.println("Image count: " + imagecount);
    }

    private int print_page_image_info(IpCOS p, int doc, int page)
        throws Exception {
        int images_on_page = (int) p.pcos_get_number(doc, "length:pages["
            + page + "]/images");
        if (images_on_page > 0) {
            System.out.println("Page " + (page + 1));
            for (int image = 0; image < images_on_page; image += 1) {
                System.out.print("  Image " + (image + 1) + ", ");

                String objtype = p.pcos_get_string(doc, "type:pages[" + page
                    + "]/images[" + image + "]/Filter");

                if (objtype.equals("name")) {
                    String filter = p.pcos_get_string(doc, "pages[" + page
                        + "]/images[" + image + "]/Filter");
                    System.out.print("Filter=" + filter);
                }
                else if (objtype.equals("array")) {
                    String filter = p.pcos_get_string(doc, "pages[" + page
                        + "]/images[" + image + "]/Filter[0]");
                    System.out.print("Filter=" + filter);

                    int filtercount = (int) p.pcos_get_number(doc,
                        "length:pages[" + page + "]/images[" + image
                            + "]/Filter");

                    for (int f = 1; f < filtercount; f++) {
                        filter = p.pcos_get_string(doc, "pages[" + page
                            + "]/images[" + image + "]/Filter[" + f + "]");
                        System.out.print("+" + filter);
                    }
                }

                System.out.print(", "
                    + (int) p.pcos_get_number(doc, "pages[" + page
                        + "]/images[" + image + "]/Width")
                    + "x"
                    + (int) p.pcos_get_number(doc, "pages[" + page
                        + "]/images[" + image + "]/Height") + ", ");

                System.out.print(
                        (int) p.pcos_get_number(doc, "images[" + image + "]/bpc")
                        + " bits per component, ");

                check_colorspace(p, doc, page, image);

                System.out.println();
            }
        }
        return images_on_page;
    }

    private static void check_colorspace(IpCOS p, int doc, int page, int image)
        throws Exception {
        String objtype = p.pcos_get_string(doc, "type:pages[" + page
            + "]/images[" + image + "]/ColorSpace");

        if (objtype.equals("name")) {
            System.out.print(p.pcos_get_string(doc, "pages[" + page
                + "]/images[" + image + "]/ColorSpace"));
        }
        else if (objtype.equals("array")) {
            String cs = p.pcos_get_string(doc, "pages[" + page + "]/images["
                + image + "]/ColorSpace[0]");
            System.out.print(cs);

            if (cs.equals("Indexed") || cs.equals("Separation")) {
                System.out.print(" "
                    + p.pcos_get_string(doc, "pages[" + page + "]/images["
                        + image + "]/ColorSpace[1]"));
            }
        }
        else if (objtype.equals("null")) {
            System.out.print("ImageMask");
        }
        else {
            System.out.print("(complex ColorSpace)");
        }
    }

    public page_images(String[] argv, String readable_name, String search_path) {
        super(argv, readable_name, search_path);
    }

    public static void main(String argv[]) {
        page_images example = new page_images(argv, "Image XObjects",
            SEARCH_PATH);
        example.execute();
    }
}
(Nov 10, 2010 - Oct 26, 2020)