PDFlib

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

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

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

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

名前付けされた跳び先

pCOS インターフェースで、PDF 文書で定義された全ての飛び先の名前を出力するサンプルプログラムです。


/*
 * List the named destinations which are defined in the document.
 * For each valid destination its destination page, type and parameters are listed.
 *
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.interactive;

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

public class named_destinations extends pcos_cookbook_example {

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

    private final static String KEY = "names/Dests";
    
    // Convenience method for retrieving an object which may have type "number" or "null"
    private String get_number_or_null(IpCOS p, int doc, String path) throws Exception
    {
        String objtype = p.pcos_get_string(doc, "type:" + path);
        
        if (objtype.equals("number"))
            return (String) p.pcos_get_string(doc, path);
        else
            return "unchanged"; // treat everything else as "null" which means "unchanged"
    }

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

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

        int destinationCount = (int) p.pcos_get_number(doc, "length:" + KEY);
        int usedDestinationCount = 0;

        for (int i = 0; i < destinationCount; ++i) {
            /* The first element in the destination array contains the
             * destination page's object number. The "destpage" pseudo object
             * conveniently converts this to a page number.
             */
            int destpage = (int) p.pcos_get_number(doc, KEY + "[" + i + "]/destpage");

            /* Ignore destinations without any valid destination page. These
             * probably referred to pages which have been deleted
             */
            if (destpage == -1)
                continue;
            
            usedDestinationCount++;
            System.out.print("named destination '" + p.pcos_get_string(doc, KEY + "[" + i + "].key") + "' ");
            System.out.print("with destination page " + destpage);

            /* Read the fitting type of the destination and analyze the corresponding parameters */            
            String fittype = p.pcos_get_string(doc, KEY + "[" + i + "].val/D[1]");

            if (fittype.equals("XYZ")) {
                System.out.print("\n    fit with zoom (/XYZ):" +
                        " left=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]") +
                        " top="  + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[3]") +
                        " zoom=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[4]"));
            
            } else if (fittype.equals("Fit")) {
                System.out.print("\n    fit full page (/Fit)");
            
            } else if (fittype.equals("FitH")) {
                System.out.print("\n    fit horizontally (/FitH)" +
                        " top="  + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
            
            } else if (fittype.equals("FitV")) {
                System.out.print("\n    fit vertically (/FitV)" +
                        " left=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
            
            } else if (fittype.equals("FitR")) {
                System.out.print("\n    fit rectangle (/FitR) [" +
                    get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]") + ", " +
                    get_number_or_null(p, doc, KEY + "[" + i + "].val/D[3]") + ", " +
                    get_number_or_null(p, doc, KEY + "[" + i + "].val/D[4]") + ", " +
                    get_number_or_null(p, doc, KEY + "[" + i + "].val/D[5]") + "]");
            
            } else if (fittype.equals("FitB")) {
                System.out.print("\n    fit bounding box (/FitB)");
            
            } else if (fittype.equals("FitBH")) {
                System.out.print("\n    fit bounding box horizontally (/FitBH)" +
                        " top="  + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
            
            } else if (fittype.equals("FitBV")) {
                System.out.print("\n    fit bounding box vertically (/FitBV)" +
                        " left=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
            }
            System.out.println();
        }
        
        System.out.print(destinationCount + " named destinations found");
        System.out.println(" (" + usedDestinationCount + " used)");
    }

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

    public static void main(String argv[]) {
        named_destinations example = new named_destinations(argv,
            "Named destinations", SEARCH_PATH);
        example.execute();
    }
}
(Nov 10, 2010 - Oct 23, 2020)