PDFlib

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

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

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

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

ページカラー

pCOS インターフェースで、全てのカラースペースを一覧するサンプルプログラムです。

必要な製品:pCOS インターフェース(PDI、PPS、TET、PLOP、PLOP DS、TET PDF IFilter に内蔵されています。)


/*
 * List the colorspaces used on each page.
 *
 * 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_colorspaces 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 pagecount = (int) p.pcos_get_number(doc, "length:pages");

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

    /**
     * Analyze and print the colorspaces for a given page number.
     * 
     * @param p
     *            IpCOS object
     * @param doc
     *            Document handle
     * @param page
     *            Page number
     * 
     * @throws Exception
     */
    private void print_colorspaces(IpCOS p, int doc, int page)
        throws Exception {

        int colorspacecount = (int) p.pcos_get_number(doc, "length:pages["
            + page + "]/colorspaces");

        if (colorspacecount > 0) {
            System.out.println("Page " + (page + 1));

            System.out.print("  " + colorspacecount + " color space");
            if (colorspacecount != 1) {
                System.out.print("s");
            }
            if (colorspacecount > 0) {
                System.out.print(":");
            }
            System.out.println();

            for (int i = 0; i < colorspacecount; i++) {
                print_colorspace(p, doc, 0,
                    "pages[" + page + "]/colorspaces[" + i + "]");
            }
        }
    }

    /**
     * Print information about a single colorspace.
     * 
     * @param p
     *            IpCOS object
     * @param doc
     *            Document handle
     * @level
     *            Recursion level, used for indenting
     * @param colorspace_path
     *            The pCOS path to the colorspace of interest
     * 
     * @throws Exception
     */
    private void print_colorspace(IpCOS p, int doc, int level, String colorspace_path)
        throws Exception {
        String prefix = get_prefix(level);
        
        String colorspace = p.pcos_get_string(doc, colorspace_path + "/name");
        System.out.println(prefix + "  " + colorspace);
        
        int pcosinterface = (int) p.pcos_get_number(doc, "pcosinterface");
        
        int componentcount = (int) p.pcos_get_number(doc,
            colorspace_path + "/components");
        System.out.println(prefix + "    " + componentcount + " component"
            + (componentcount == 1 ? "" : "s"));
        
        if (colorspace.equals("ICCBased")) {
            System.out.println(prefix + "    size of ICC profile in bytes: "
                + (int) p.pcos_get_number(doc, colorspace_path + "/Length"));
            
            if (pcosinterface >= 10) {
                int iccprofileid = (int) p.pcos_get_number(doc,
                    colorspace_path + "/iccprofileid");
                
                String errormessage = p.pcos_get_string(doc,
                    "iccprofiles[" + iccprofileid + "]/errormessage");
                
                /* Check whether the embedded profile is damaged */
                if (errormessage != null && errormessage.length() > 0) {
                    System.out.println(" (" + errormessage + ")");
                }
                else {
                    String profilename = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/profilename");
                    System.out.println(prefix + "    name: '" + profilename + "'");
                    
                    String profilecs = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/profilecs");
                    System.out.println(prefix + "    cs: '" + profilecs + "'");
                    
                    String deviceclass = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/deviceclass");
                    System.out.println(prefix + "    deviceclass: '" + deviceclass + "'");
                    
                    String iccversion = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/iccversion");
                    System.out.println(prefix + "    iccversion: '" + iccversion + "'");
                }
            }
        }
        else if (colorspace.equals("Lab")) {
            System.out.print(prefix + "    WhitePoint: ");
            for (int j = 0; j < 3; j += 1) {
                System.out.print(p.pcos_get_number(doc, colorspace_path
                    + "/csarray[1]/WhitePoint[" + j + "]")
                    + " ");
            }
            System.out.println();
        }
        else if (colorspace.equals("CalGray") || colorspace.equals("CalRGB")) {
            System.out.print(prefix + "    WhitePoint: ");
            for (int j = 0; j < 3; j += 1) {
                System.out.print(p.pcos_get_number(doc, colorspace_path
                    + "/csarray[1]/WhitePoint[" + j + "]")
                    + " ");
            }
            System.out.println();
        }
        else if (colorspace.equals("Separation")) {
            String colorant = p.pcos_get_string(doc, colorspace_path
                + "/colorantname");
            System.out.println(prefix + "    colorant \"" + colorant + "\"");
            int alternateid = (int) p.pcos_get_number(doc, colorspace_path
                + "/alternateid");
            System.out.println(prefix + "    alternate color space:");
            print_colorspace(p, doc, level + 1, "colorspaces[" + alternateid + "]");
        }
        else if (colorspace.equals("DeviceN")) {
            int colorantcount = (int) p.pcos_get_number(doc, "length:"
                + colorspace_path + "/colorantnames");
            System.out.println(prefix + "    colorants:");
            for (int j = 0; j < colorantcount; j += 1) {
                System.out.println(prefix + "      \""
                    + p.pcos_get_string(doc, colorspace_path
                        + "/colorantnames[" + j + "]") + "\"");
            }
            int alternateid = (int) p.pcos_get_number(doc, colorspace_path
                + "/alternateid");
            System.out.println(prefix + "    alternate color space:");
            print_colorspace(p, doc, level + 1, "colorspaces[" + alternateid + "]");
        }
        else if (colorspace.equals("Indexed")) {
            int palettesize = (int) p.pcos_get_number(doc, colorspace_path
                + "/csarray[2]");
            System.out.println(prefix + "    palette size: " + palettesize);
            
            int baseid = (int) p.pcos_get_number(doc, colorspace_path
                + "/baseid");
            System.out.println(prefix + "    base color space: ");
            print_colorspace(p, doc, level + 1, "colorspaces[" + baseid + "]");
        }
        else if (colorspace.equals("Pattern")) {
            /*
             * Check whether the Pattern color space has an underlying color
             * space, and print it recursively if present.
             * 
             * This only works with a pcosinterface >= 10; for pcosinterface
             * < 10 Pattern colorspaces are not present in the colorspaces[]
             * array.
             */
            int baseid = (int) p.pcos_get_number(doc, colorspace_path
                + "/baseid");
            if (baseid != -1) {
                System.out.println(prefix + "    underlying color space:");
                print_colorspace(p, doc, level + 1, "colorspaces[" + baseid + "]");
            }            
        }
    }
    
    /**
     * Get a prefix string consisting of spaces that can be used to indent
     * lines.
     * 
     * @param level
     *            indentation level
     * @return
     *            indentation string          
     * 
     * @throws Exception
     */
    private String get_prefix(int level) {
        StringBuilder prefix = new StringBuilder();
        
        for (int i = 0; i < level; i += 1) {
            prefix.append("    ");
        }
        
        return prefix.toString();
    }

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

    public static void main(String argv[]) {
        page_colorspaces example = new page_colorspaces(argv, "Colorspaces per page",
            SEARCH_PATH);
        example.execute();
    }
}
(Nov 10, 2010 - Sep 30, 2022)