Create a gist now

Instantly share code, notes, and snippets.

This sample locates article threads and outputs their coordinates to the console
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.document;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.pdf.document.PDFCatalogThreads;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.interactive.navigation.PDFBead;
import com.adobe.pdfjt.pdf.interactive.navigation.PDFBead_First;
import com.adobe.pdfjt.pdf.interactive.navigation.PDFThread;
import com.adobe.pdfjt.pdf.interactive.navigation.PDFThreadInfo;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* This sample locates article threads and outputs their coordinates to the
* console.
*/
public class LocateArticleThreads {
private static final String inputPDFURL = "http://dev.datalogics.com/cookbook/document/ArticleThreads.pdf";
static public void main(String[] args) throws Exception {
// First read in the PDF 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());
// Get the list of threads
PDFCatalogThreads threads = pdfDocument.requireCatalog().getThreads();
// There's only one thread in the sample file so we can just get the
// first one.
PDFThread pdfThread = threads.get(0);
// Get the thread metadata (information) and output the properties
PDFThreadInfo pdfThreadInfo = pdfThread.getI();
System.out.println("Subject: " + pdfThreadInfo.getSubject());
System.out.println("Title: " + pdfThreadInfo.getTitle());
// Get the firstbead onthe thread and output the properties
PDFBead_First pdfBeadFirst = pdfThread.getF();
System.out.println("Bead: Page " + pdfBeadFirst.getPage().getPageNumber());
System.out.println(" llx: " + pdfBeadFirst.getRect().llx());
System.out.println(" lly: " + pdfBeadFirst.getRect().lly());
System.out.println(" width: " + pdfBeadFirst.getRect().width());
System.out.println(" height: " + pdfBeadFirst.getRect().height());
System.out.println("");
// Get the next bead then iterate through all of them until we get to
// the first one again. Just like in Acrobat, the last bead references
// the first.
PDFBead pdfBead = pdfBeadFirst.getNext();
while (pdfBead.getCosObject().getObjNum() != pdfBeadFirst.getCosObject().getObjNum()) {
System.out.println("Bead: Page " + pdfBead.getPage().getPageNumber());
System.out.println(" llx: " + pdfBead.getRect().llx());
System.out.println(" lly: " + pdfBead.getRect().lly());
System.out.println(" width: " + pdfBead.getRect().width());
System.out.println(" height: " + pdfBead.getRect().height());
System.out.println("");
pdfBead = pdfBead.getNext();
}
// Save the file.
System.out.println("Done!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment