PDFlib
BASKET
PDFlib

pages/page_fonts

Create a list of all fonts contained in a PDF document and check if they are embedded.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.pages;


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 check if they are embedded.

 * <p>

 * Required software: pCOS interface 3 (pCOS 2.x, PDFlib 7.x, TET 2.2,

 * PLOP 3.x)<br>

 * Required data: PDF document

 *

 * @version $Id: page_fonts.java,v 1.2 2007/09/19 08:45:30 stm Exp $

 */

public class page_fonts 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.get_string(doc, "filename"));


        int pagecount = (int) p.get_number(doc, "length:pages");


        for (int page = 0; page < pagecount; page++) {

            /* retrieve the number of fonts on the page */

            int fontcount =

                (int) p.get_number(doc, "length:pages[" + page + "]/fonts");


            if (fontcount > 0) {

                System.out.println(fontcount + " fonts found on page " + (page + 1));


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

                    System.out.print(p.get_string(doc,

                        "pages[" + page + "]/fonts[" + i + "]/name"));

                    boolean embedded =

                        p.get_number(doc, "pages[" + page + "]/fonts[" + i + "]/embedded") != 0;

                    if (embedded)

                        System.out.println(" (embedded)");

                    else

                        System.out.println(" (not embedded)");

                }

            }

        }


        p.close_document(doc);

    }

       

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

        page_fonts example = new page_fonts(argv, "Fonts",

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

        example.execute();

    }

}