Last active
June 10, 2016 22:00
-
-
Save JoelGeraci-Datalogics/797be465ac8406bc4197 to your computer and use it in GitHub Desktop.
This sample finds fields in a PDF file and identifies any barcode fields then prints it's value to System.out.
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
/* | |
* Copyright Datalogics, Inc. 2015 | |
*/ | |
package pdfjt.cookbook.forms; | |
import com.adobe.internal.io.ByteReader; | |
import com.adobe.internal.io.InputStreamByteReader; | |
import com.adobe.pdfjt.pdf.document.PDFDocument; | |
import com.adobe.pdfjt.pdf.document.PDFOpenOptions; | |
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationWidget; | |
import com.adobe.pdfjt.pdf.interactive.forms.PDFField; | |
import com.adobe.pdfjt.pdf.interactive.forms.PDFInteractiveForm; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Iterator; | |
/** | |
* This sample finds fields in a PDF file and identifies any barcode fields then | |
* prints it's value to System.out. | |
*/ | |
public class ReadBarcodeFieldFromPDF { | |
private static final String inputPDFURL = "http://dev.datalogics.com/cookbook/forms/ReadBarcodeField.pdf"; | |
public static void main(String[] args) throws Exception { | |
/* | |
* Read in PDF input file | |
*/ | |
URLConnection connection = new URL(inputPDFURL).openConnection(); | |
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); | |
connection.connect(); | |
InputStream fis = connection.getInputStream(); | |
ByteReader byteReader = new InputStreamByteReader(fis); | |
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance()); | |
PDFInteractiveForm pdfInteractiveForm = pdfDocument.getInteractiveForm(); | |
if (pdfInteractiveForm != null) { | |
// Get Field iterator | |
Iterator<PDFField> fieldIterator = pdfInteractiveForm.iterator(); | |
// Iterator over form fields | |
while (fieldIterator.hasNext()) { | |
PDFField pdfField = (PDFField) (fieldIterator.next()); | |
/* | |
* Barcode fields are really text fields with special | |
* appearances so we only need to look for PDFFieldText objects. | |
*/ | |
if (pdfField.getClass().getSimpleName().matches("PDFFieldText")) { | |
/* | |
* Assume the field is terminal and likely to be combined | |
* with it's annotation. | |
*/ | |
if (pdfField.isAnnotation()) { | |
/* | |
* Get the first, and probably only, annotation | |
* associated with the field. If the barcode appears on | |
* more than one page, this may not be the case. | |
*/ | |
PDFAnnotationWidget pdfAnnotationWidget = (PDFAnnotationWidget) pdfField.getAnnotationsIterator().next(); | |
/* | |
* Finally we have an object that we can interrogate to | |
* see if it is a barcode widget. PMD stands for | |
* "Paper Metadata". | |
*/ | |
if (pdfAnnotationWidget.hasPMD()) { | |
// We've identified a barcode widget | |
if (pdfField.getValueList() != null) { | |
System.out.println("Barcode Field Found: " + pdfField.getQualifiedName()); | |
System.out.println(pdfField.getValueList().get(0)); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment