PDFlib

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

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

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

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

名前付けされた跳び先

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


package com.pdflib.cookbook.pcos.interactive;

import com.pdflib.IpCOS;

import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/**
 * Print the names of all destinations which are defined in the document.
 * 
 * Required software: pCOS interface 3 (pCOS 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) 
 * Required data: PDF document
 * 
 * @version $Id: named_destinations.java,v 1.10 2015/11/16 11:53:16 stm Exp $
 */
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";

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

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

        int len = (int) p.pcos_get_number(doc, "length:" + KEY);
        System.out.println(len + " found");
        for (int i = 0; i < len; ++i) {
            int dest_page = (int) p.pcos_get_number(doc, KEY + "[" + i
                + "]/destpage");

            System.out.print(p.pcos_get_string(doc, KEY + "[" + i + "].key"));

            if (dest_page != -1) {
                System.out.print(": page " + dest_page);
            }
            System.out.println();
        }

        /**
         * Very old PDF 1.1 documents may contain named destinations in
         * /Root/Dests instead of names/Dests (which maps to /Root/Names/Dests).
         * Since pCOS does not provide the convenient "destpage" pseudo entry
         * for these, we dump only the destination names. This flavor is very
         * rare, and is present only in very old PDFs. It is nevertheless
         * checked here for completeness.
         */
        if (p.pcos_get_string(doc, "type:/Root/Dests").equals("dict")) {
            len = (int) p.pcos_get_number(doc, "length:/Root/Dests");

            System.out.println();
            System.out
                .println(len + " old-style (PDF 1.1) destinations found:");

            for (int i = 0; i < len; ++i) {
                System.out.println(p.pcos_get_string(doc, "/Root/Dests[" + i
                    + "].key"));
            }
        }
    }

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

    public static void main(String argv[]) {
        named_destinations example = new named_destinations(argv,
            "Named destinations", SEARCH_PATH,
            "$RCSfile: named_destinations.java,v $", "$Revision: 1.10 $");
        example.execute();
    }
}
(Nov 10, 2010 - Oct 26, 2018)