Q: How can I list all the resource fonts in a PDF document?

A: Here is a sample Java code to list all fonts referenced in a PDF document using Qoppa’s jPDFProcess library. Some fonts may only be referenced by name and other fonts may be embedded in the document.

In version v2016R1, a new package was made available to Qoppa’s jPDFProcess public API called com.qoppa.pdf.resources. This package contains interfaces allowing to get information about resources contained in a PDF, including IResourceManager, IFontResource, IImageResource.

package sample;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import javax.imageio.ImageIO;
 
import com.qoppa.pdf.PDFException;
import com.qoppa.pdf.resources.IFontResource;
import com.qoppa.pdf.resources.IImageResource;
import com.qoppa.pdf.resources.IResourceManager;
import com.qoppa.pdfProcess.PDFDocument;
 
public class AccessDocumentResources
{
	public static void main(String[] args)
	{
		try
		{
			PDFDocument pdfDoc = new PDFDocument("C:/sample/in.pdf", null);
			IResourceManager resourceManager = pdfDoc.getResourceManager();
 
			// List the fonts from the document
			System.out.println("List fonts:");
			List<? extends IFontResource> fontList = resourceManager.listFonts();
			for (IFontResource fontRes : fontList)
			{
				System.out.println("\t" + fontRes.getFontName());
			}
		}
		catch (PDFException ex)
		{
			ex.printStackTrace();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
}
Tagged: