detecting fonts un a PDF

pieter vankeerberghen <[email protected]> Sat, 28 Mar 2015 10:22:15 +0100
Newsgroups gmane.comp.java.lib.itext.general
Message-ID <[email protected]>
Dear,

We try to list the fonts used in a PDF, and further to see whether they 
are embedded.  The java source, is attached - notice it is based on the 
itext 2 book listing.

Unfortunately, for the PDF attached (and adobe preflight from version 
9.5 show that the PDF is OK), we cannot find the calibri font and one 
arial bold font, which the Adobe reader document properties -> fonts 
show these are truetype fonts.  The three subset embedded fonts are 
detected.

We use itext 5.5.3 and I already went through the latest version of the 
stack overflow Q&A.  With RUPS, I failed to find the font Calibri font.

Can you point us to detect the missing fonts ?

Thank you and kind regards,
Pieter

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/

_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
fonts.java (text/plain, 5.4 KB)
package fonts;

import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JFileChooser;

public class fonts
{
  public static String RESULT = "\\output.txt";
  static PrintWriter out;

  public static String getISOPdfversion(PdfReader reader)
  {
    String version = null;

    String catalog_version = null;
    try
    {
      PdfDictionary root = reader.getCatalog();
      catalog_version = root.getAsName(PdfName.VERSION).toString();
    }
    catch (Throwable e)
    {
      int headerversion = Integer.parseInt(Character.toString(reader.getPdfVersion()));
      version = "1." + Integer.toString(headerversion);
      catalog_version = null;
    }

    if (catalog_version != null)
    {
      int headerversion = Integer.parseInt(Character.toString(reader.getPdfVersion()));
      int catalogversion = Integer.parseInt(catalog_version.substring(3, 4));
      if (catalogversion > headerversion)
        version = "1." + Integer.toString(catalogversion);
      else {
        version = "1." + Integer.toString(headerversion);
      }
    }

    return version;
  }

  public Set<String> listFonts(String src)
    throws IOException
  {
    Set set = new TreeSet();
    PdfReader reader = new PdfReader(src);

    for (int k = 1; k <= reader.getNumberOfPages(); k++) {
      PdfDictionary resources = reader.getPageN(k).getAsDict(PdfName.RESOURCES);
      if (resources != null)
        processResource(set, resources);
    }
    return set;
  }

  public static void processResource(Set<String> set, PdfDictionary resource)
  {
    if (resource == null)
      return;
    PdfDictionary xobjects = resource.getAsDict(PdfName.XOBJECT);
    if (xobjects != null) {
      for (PdfName key : xobjects.getKeys()) {
        processResource(set, xobjects.getAsDict(key));
      }
    }
    PdfDictionary fonts = resource.getAsDict(PdfName.FONT);
    if (fonts == null) {
      return;
    }
    for (PdfName key : fonts.getKeys()) {
      PdfDictionary font = fonts.getAsDict(key);

      if ((font == null) || (font.length() == 0)) {
        continue;
      }
      if (font.getAsName(PdfName.BASEFONT) == null)
        continue;
      String name = font.getAsName(PdfName.BASEFONT).toString();

      if ((name.length() > 8) && (name.charAt(7) == '+')) {
        name = String.format("%s subset (%s)", new Object[] { name.substring(8), name.substring(1, 7) });
      }
      else {
        name = name.substring(1);
        PdfDictionary desc = font.getAsDict(PdfName.FONTDESCRIPTOR);
        if (desc == null)
          name = name + " nofontdescriptor";
        else if (desc.get(PdfName.FONTFILE) != null)
          name = name + " (Type 1) embedded";
        else if (desc.get(PdfName.FONTFILE2) != null)
          name = name + " (TrueType) embedded";
        else if (desc.get(PdfName.FONTFILE3) != null)
          name = name + " (" + font.getAsName(PdfName.SUBTYPE).toString().substring(1) + ") embedded";
      }
      set.add(name);
    }
  }

  public static void recurseintofolder(File dir)
  {
    String pattern = ".pdf";
    boolean corrupt = false;
    char mypdfversion = '0';
    int mypermissions = 0;

    File[] listFile = dir.listFiles();

    if (listFile != null)
      for (int i = 0; i < listFile.length; i++)
        if (listFile[i].getName().endsWith(pattern))
        {
          System.out.println(listFile[i].getPath());

          PdfReader reader = null;
          corrupt = false;
          try
          {
            reader = new PdfReader(listFile[i].getPath());
          }
          catch (Throwable e)
          {
            System.out.print("\tcorrupt\n");
            out.println(listFile[i].getPath() + "\tcorrupt\n");

            corrupt = true;
          }

          if (corrupt)
            continue;
          if (reader.isEncrypted())
          {
            System.out.print("\tencrypted\n");
            out.println(listFile[i].getPath() + "\tencrypted\n");
          }
          else
          {
            out.println(listFile[i].getPath() + " -- PDF version: " + getISOPdfversion(reader));
            HashMap info = reader.getInfo();
            out.println("Creator of PDF: " + (String)info.get("Creator"));

            Set mset = null;
            try {
              mset = new fonts().listFonts(listFile[i].getPath());
            }
            catch (IOException e) {
              e.printStackTrace();
            }

            for (Object fontname : mset) {
              out.println(fontname);
            }
            out.println("");
          }

        }
        else
        {
          if (!listFile[i].isDirectory())
            continue;
          recurseintofolder(listFile[i]);
        }
  }

  public static void main(String[] args)
    throws Exception
  {
    JFileChooser fc = new JFileChooser("c:/");

    fc.setFileSelectionMode(1);
    int returnVal = fc.showOpenDialog(null);

    out = new PrintWriter(new FileOutputStream(fc.getSelectedFile() + RESULT));

    recurseintofolder(fc.getSelectedFile());

    out.flush();
    out.close();
  }
}
gtoc2.pdf (application/pdf, 103.5 KB) - not displayed