PDFlib

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

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

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

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

添付ファイル

pCOS インターフェースで、添付ファイルの総件数を確認するサンプルプログラムです。
・文書レベルの添付ファイル:PDF パッケージ(コレクション)
・全てのページの添付ファイル

/*
 * Determine the total number of file attachments:
 * - document-level attachments; identify PDF Portfolios (collections)
 * - file attachments on all pages.
 *
 * 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 attachments 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");

        /* Check whether the document represents a PDF Portfolio */
        String colltype = p.pcos_get_string(doc, "type:/Root/Collection");
        if (colltype.equals("dict")) {
            String defaultdoc;
            System.out.print("PDF Portfolio, default document: ");

            /*
             * Check the default document (may be different from container PDF);
             * we currently don't check whether this name is actually present in
             * the list of embedded files.
             */
            if (p.pcos_get_string(doc, "type:/Root/Collection/D").equals(
                "string")) {
                defaultdoc = "'" + p.pcos_get_string(doc, "/Root/Collection/D")
                    + "'";
            }
            else
                defaultdoc = "container PDF";

            System.out.println(defaultdoc);
        }

        /* Check for document file attachments */
        int filecount = (int) p.pcos_get_number(doc, "length:names/EmbeddedFiles");

        for (int file = 0; file < filecount; file++) {
            /* Check for Unicode file name */
            String objtype = p.pcos_get_string(doc, "type:names/EmbeddedFiles["
                + file + "]/UF");

            if (objtype.equals("string")) {
                String val = p.pcos_get_string(doc, "names/EmbeddedFiles["
                    + file + "]/UF");
                System.out.println("Document attachment '"
                    + val
                    + "', length "
                    + get_attachment_contents(p, doc, "names/EmbeddedFiles["
                        + file + "]/EF/F") + " bytes");
            }
            else {
                /* Check for ASCII/native encoded file name */
                objtype = p.pcos_get_string(doc, "type:names/EmbeddedFiles["
                    + file + "]/F");
                if (objtype.equals("string")) {
                    String val = p.pcos_get_string(doc, "names/EmbeddedFiles["
                        + file + "]/F");
                    System.out.println("Document attachment '"
                        + val
                        + "', length "
                        + get_attachment_contents(p, doc,
                            "names/EmbeddedFiles[" + file + "]/EF/F")
                        + " bytes");
                }
            }
        }

        System.out.println(filecount + " document attachments found");
        System.out.println();

        /* Check all pages for annotations of type FileAttachment */
        filecount = 0;

        for (int page = 0; page < pagecount; page++) {
            int annotcount = (int) p.pcos_get_number(doc, "length:pages["
                + page + "]/Annots");

            for (int annot = 0; annot < annotcount; annot++) {
                // pCOS path for a specific annotation on a page
                String page_annotation = "pages[" + page + "]/Annots[" + annot
                    + "]";
                String val = p.pcos_get_string(doc, page_annotation
                    + "/Subtype");

                if (val.equals("FileAttachment")) {
                    process_attachment(p, doc, page, page_annotation);
                    filecount++;
                }
            }
        }
        System.out.println(filecount + " attachment annotations found");
    }

    /**
     * Prints information about a file attachment and extracts it to disk.
     * The method expects the pCOS path to the attachment in the form
     * "pages[i]/Annots[j]", with i being the page number and j being the number
     * of the annotation of interest on that page.
     * 
     * @param p
     *            IpCOS object
     * @param doc
     *            document handle
     * @param page
     *            page number (zero based)
     * @param annotation
     *            pCOS path to the annotation
     * 
     * @throws Exception
     */
    private void process_attachment(IpCOS p, int doc, int page, String annotation)
        throws Exception {

        /* Check for Unicode file name */
        String objtype = p
            .pcos_get_string(doc, "type:" + annotation + "/FS/UF");

        if (objtype.equals("string")) {
            String val = p.pcos_get_string(doc, annotation + "/FS/UF");
            System.out.println("Page " + (page + 1) + ", attachment '" + val
                + "', length "
                + get_attachment_contents(p, doc, annotation + "/FS/EF/F")
                + " bytes");
        }
        else {
            /* Check for ASCII/native encoded file name */
            objtype = p.pcos_get_string(doc, "type:" + annotation + "/FS/F");
            if (objtype.equals("string")) {
                String val = p.pcos_get_string(doc, annotation + "/FS/F");
                System.out.println("Page " + (page + 1) + ", attachment '"
                    + val + "', length "
                    + get_attachment_contents(p, doc, annotation + "/FS/EF/F")
                    + " bytes");
            }
        }
    }

    /**
     * Retrieves the contents of the attachment.
     * 
     * @param p
     *            IpCOS object
     * @param doc
     *            document handle
     * @param attachment_stream_path
     *            pCOS path to embedded file stream
     * 
     * @return the length of the attachment in bytes
     * 
     * @throws Exception
     */
    private int get_attachment_contents(IpCOS p, int doc,
        String attachment_stream_path) throws Exception {
        byte[] attachment_contents = p.pcos_get_stream(doc, "",
            attachment_stream_path);

        return attachment_contents.length;
    }

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

    public static void main(String argv[]) {
        attachments example = new attachments(argv, "File attachments",
            SEARCH_PATH);
        example.execute();
    }
}


(Nov 10, 2010 - Oct 19, 2022)