PDFlib
BASKET
PDFlib

resources/advanced_font_properties

Create a list of all fonts contained in a PDF document and print advanced properties of the fonts.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.resources;


import java.util.regex.Matcher;

import java.util.regex.Pattern;


import com.pdflib.pCOS;

import com.pdflib.pCOSException;

import com.pdflib.cookbook.pcos.pcos_cookbook_example;


/**

 * Create a list of all fonts contained in a PDF document and print advanced

 * properties of the fonts.

 * <p>

 * Required software: pCOS interface 6 (pCOS 3.x, TET 4.x) <br>

 * Required data: PDF document

 *

 * @version $Id: advanced_font_properties.java,v 1.2 2010/11/02 07:34:13 stm Exp $

 */

public class advanced_font_properties 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(pCOS p, String filename) throws pCOSException,

        Exception {

        /* Open the PDF document */

        int doc = p.open_document(filename, "");

        if (doc == -1)

            throw new Exception("Error: " + p.get_errmsg());


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


        /* retrieve the number of fonts in the document */

        int fontcount = (int) p.pcos_get_number(doc, "length:fonts");

        System.out.println(fontcount + " fonts found in "

            + p.pcos_get_string(doc, "filename"));


        for (int i = 0; i < fontcount; i++) {

            print_font_info(p, doc, i);

        }


        p.close_document(doc);

    }


    /**

     * Print advanced information about a single font.

     *

     * @param p

     *          pCOS object

     * @param doc

     *          document handle

     * @param f

     *          index of font in the global "fonts" array

     *         

     * @throws pCOSException

     */

    private void print_font_info(pCOS p, int doc, int f) throws pCOSException {

        String prefix = "  ";

       

        System.out.println(p.pcos_get_string(doc, "fonts[" + f + "]/name"));

       

        boolean embedded = p.pcos_get_number(doc, "fonts[" + f

            + "]/embedded") != 0;

        System.out.println(prefix + "embedded: "+ (embedded ? "yes" : "no"));

       

        if (embedded) {

            /*

             * Only for an embedded font is makes sense to determine whether

             * the font is subsetted. This can be found out by looking at the

             * fullname. If it starts with six uppercase letters and a '+'

             * character, the font is a subset.

             * The presence of the prefix is determined with a regular

             * expression here: Exactly six uppercase letters A-Z, followed

             * by a '+' character, followed by at least one additional

             *  character.

             */

            String fullname = p.pcos_get_string(doc,

                                            "fonts[" + f + "]/fullname");

            Pattern subset_prefix = Pattern.compile("[A-Z]{6}\\+.+");

            Matcher matcher = subset_prefix.matcher(fullname);

            System.out.println(prefix + "subset: "

                                + (matcher.matches() ? "yes" : "no"));

        }

        else {

            System.out.println(prefix + "subset: n/a");

        }

       

        System.out.println(prefix + "writing mode: " +

            (p.pcos_get_number(doc, "fonts[" + f + "]/vertical") != 0

                ? "vertical"

                : "horizontal"));


        String objtype = p.pcos_get_string(doc,

            "type:fonts[" + f + "]/FontDescriptor/Ascent");

        if (objtype.equals("number"))

        {

            int ascender = (int) p.pcos_get_number(doc,

            "fonts[" + f + "]/FontDescriptor/Ascent");

            System.out.println(prefix + "ascender: " + ascender);

        }

        else {

            System.out.println(prefix + "ascender: n/a");

        }

       

        objtype = p.pcos_get_string(doc,

                    "type:fonts[" + f + "]/FontDescriptor/Descent");

       

        /* check whether the entry actually exists */

        if (objtype.equals("number"))

        {

            int descender = (int) p.pcos_get_number(doc,

            "fonts[" + f + "]/FontDescriptor/Descent");

            System.out.println(prefix + "descender: " + descender);

        }

        else {

            System.out.println(prefix + "descender: n/a");

        }

    }


    public advanced_font_properties(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[]) {

        advanced_font_properties example = new advanced_font_properties(argv,

            "Advanced Font Properties", SEARCH_PATH,

            "$RCSfile: advanced_font_properties.java,v $", "$Revision: 1.2 $");

        example.execute();

    }

}