Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A sample which lists the fonts in a PDF Document, and extracts a bit more. #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ListDocFonts/.idea/runConfigurations/ListFonts.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

160 changes: 160 additions & 0 deletions ListDocFonts/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.datalogics.pdfl.samples</groupId>
<artifactId>ListFonts</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<profiles>
<profile>
<id>Windows64</id>
<activation>
<os>
<family>windows</family>
<arch>amd64</arch>
</os>
</activation>
<properties>
<jni.classifier>win-x86-64-jni</jni.classifier>
</properties>
</profile>
<profile>
<id>MacArm</id>
<activation>
<os>
<family>mac</family>
<arch>aarch64</arch>
</os>
</activation>
<properties>
<jni.classifier>mac-arm-64-jni</jni.classifier>
</properties>
</profile>
<profile>
<id>Linux64</id>
<activation>
<os>
<!-- Use OS <name> instead of <family> because the "unix" <family> also includes Mac -->
<name>Linux</name>
<arch>amd64</arch>
</os>
</activation>
<properties>
<jni.classifier>linux-x86-64-jni</jni.classifier>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<version>18.37.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<version>18.37.0</version>
</dependency>
<dependency>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<version>18.37.0</version>
<type>zip</type>
<classifier>${jni.classifier}</classifier>
</dependency>
<dependency>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<version>18.37.0</version>
<type>zip</type>
<classifier>resources</classifier>
</dependency>
<dependency>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<version>18.37.0</version>
<classifier>javadoc</classifier>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<classifier>resources</classifier>
<type>zip</type>
<outputDirectory>${project.build.directory}/lib/Resources</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
<execution>
<id>unpack-jni</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.datalogics.pdfl</groupId>
<artifactId>pdfl</artifactId>
<classifier>${jni.classifier}</classifier>
<type>zip</type>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.datalogics.pdfl.samples.ListDocFonts</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.datalogics.pdfl.samples;

import com.datalogics.PDFL.*;
import java.util.List;
import java.awt.Font;

/*
*
* A sample which demonstrates how to list
* the fonts used in a document.
*
* Copyright (c) 2007-2024, Datalogics, Inc. All rights reserved.
*
*/
public class ListDocFonts {
public static void main(String[] args) throws Throwable {
Library lib = new Library();

System.out.println("Initialized the library.");

String sInput = "../../Resources/Sample_Input/sample.pdf";

if (args.length > 0)
sInput = args[0];

System.out.println("Input file: " + sInput);

Document doc = new Document(sInput);

try {
List<com.datalogics.PDFL.Font> docFonts = doc.getFonts(0, doc.getNumPages() - 1);

for (com.datalogics.PDFL.Font font : docFonts) {
System.out.println(font.getName());
System.out.println("\t"+font.getFullName());
System.out.println("\t"+font.getType());
System.out.println("\tFont is" + (font.getEmbedded() ? "" : " not") + " embedded.");

System.out.println("\t"+font.getEncoding());

if (font.getType().equals("Type0") && font.getEmbedded()) {
PDFArray descFontsArray = (PDFArray) font.getPDFDict().get("DescendantFonts");
PDFDict descFont = (PDFDict) descFontsArray.get(0);
PDFDict fontDescriptor = (PDFDict) descFont.get("FontDescriptor");
if (fontDescriptor.contains("FontFile2")) {
PDFStream fontStream = (PDFStream) fontDescriptor.get("FontFile2");

java.awt.Font f = Font.createFont(Font.TRUETYPE_FONT, fontStream.getFilteredStream());

System.out.println("\t\t"+f.getName());
System.out.println("\t\t"+f.getFontName());
System.out.println("\t\t"+f.getFamily());
System.out.println("\t\t"+f.getPSName());
}
}
}
} finally {
lib.delete();
}
}
};