Created
February 25, 2015 02:19
-
-
Save kBashar/283c353aa8d76da5c791 to your computer and use it in GitHub Desktop.
A class to find Font Encodings in a PDF file using PDFBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kbashar.pdfboxtry; | |
import org.apache.pdfbox.encoding.Encoding; | |
import org.apache.pdfbox.pdmodel.PDDocument; | |
import org.apache.pdfbox.pdmodel.PDPage; | |
import org.apache.pdfbox.pdmodel.PDResources; | |
import org.apache.pdfbox.pdmodel.font.PDFont; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by kbashar on 2/24/15. | |
*/ | |
public class FontEncodingFinder { | |
public void showEncodings(String fileName) throws IOException { | |
PDDocument document = PDDocument.load(fileName); | |
List<PDPage> pages = document.getDocumentCatalog().getAllPages(); | |
for (PDPage page:pages) { | |
PDResources resources = page.getResources(); | |
Map<String,PDFont> fonts = resources.getFonts(); | |
for (Map.Entry<String,PDFont> entry:fonts.entrySet()) { | |
Encoding encoding = entry.getValue().getFontEncoding(); | |
if (encoding!=null) { | |
String[] string = encoding.toString().replaceAll("org.apache.pdfbox.encoding.","").split("@"); | |
System.out.println(string[0]); | |
} | |
//if there is no explicit encoding then | |
// default Encoding "StandardEncoding will be shown" | |
else { | |
System.out.println("StandardEncoding"); | |
} | |
} | |
} | |
} | |
public static void main(String[] args) { | |
if (args.length!=1) { | |
System.out.println("Please provide file name"); | |
return; | |
} else { | |
FontEncodingFinder finder = new FontEncodingFinder(); | |
try { | |
finder.showEncodings(args[0]); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment