Lab-6808: Working with PDF and Java

Expected Duration: 120 minutes
Contacts: Duane Nickull
Begin Product Tab Sub Links

Exercise 4: Grabbing PDF Annotations



    - Objective:  Learn about annotations (kinds, properties) and how to grab the annotations out of a PDF package.
    - Expected duration:  Approximately 13 minutes

About annotations:

An annotation associates an object such as a note, sound, or movie with a location on a page of a PDF document, or provides a way to interact with the user by means of the mouse and keyboard. PDF includes a wide variety of standard annotation types, described in detail in Section 8.4.5 of the ISO PDF Specification, “Annotation Types.”

Many of the standard annotation types may be displayed in either the open or the closed state. When closed, they appear on the page in some distinctive form, such as an icon, a box, or a rubber stamp, depending on the specific annotation type. When the user activates the annotation by clicking it, it exhibits its associated object, such as by opening a pop-up window displaying a text note or by playing a sound or a movie.

Discuss these slides:

Step 1 –  Navigate in Eclipse to open up the java file PDFAnnotationExtractor.java.

Step 2 - As with previous exercises, you will have to set the arguments to point at the path to the "DuanesWorld.pdf" document.

Step 3 – note that it is similar to the previous files except we have added some base functionality to allow for writing files out to the local hard drive. 

 

package org.duanesworldtv.livecycle.samples;

import java.io.InputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
// this is from xpaaj.jar - check licenses before using.
// LiveCycle ES has newer JAVA libraries for manipulating PDF
import com.adobe.pdf.*;

public class PDFAnnotationExtractor { public static void main(String[] args) throws FileNotFoundException, IOException {
String inPDFName; if (args.length != 1) { System.out.println("Usage: java <class_name> <PDF File>"); return; } else { System.out.println("Starting to open PDF document"); inPDFName = new String(args[0]); PDFExtract(inPDFName); } }

public static void PDFExtract(String inPdfName) throws FileNotFoundException, IOException { // open PDF System.out.println("\nOpen PDF Document ... "); PDFDocument doc = null; FileInputStream inPdfFile = new FileInputStream(inPdfName); try
{
doc = PDFFactory.openDocument(inPdfFile);
} catch (IOException e) {
System.out.println("Error opening PDF file :" + inPdfName); System.out.println(e); } if(doc == null) { System.out.println("Cannot open PDF file : " + inPdfName); } else { System.out.println( inPdfName + " was successfully opened."); System.out.println("\nTrying to Extract annotaion data ..."); } // TOD): EXPORT ANNOTATIONS HERE }
public static boolean saveFile(InputStream is, String filePath)
throws Exception
{
boolean retVal=false;
byte[] buffer = new byte[10240];
FileOutputStream outStream = null;
try
{
outStream = new FileOutputStream(filePath);
int len=0;
while (true)
{
len = is.read(buffer);
if (len == -1)
break;
outStream.write(buffer, 0, len);
}
outStream.close();
retVal = true;
}catch (IOException io) {
System.out.println("Writing the array of bytes into the file "
+ filePath + " failed.");
throw new Exception(
"Writing the array of bytes into the file "+ filePath +
" failed in saveFile");
}
return retVal;
}

// save text string to a file.
public static boolean saveStringToFile(String text, String filePath)
{
boolean b = false;
try {
BufferedWriter outTxtFile = new BufferedWriter(new FileWriter(filePath));
outTxtFile.write(text, 0, text.length());
outTxtFile.close();
b = true;
} catch (IOException e) {
System.out.println("Error saving text file.");
}
return b;
}
}

/******* End of file *********************************/

 

Step 4: Navigate down to where you see the TODO comments and add the code from the Solutions comment. Note that you can view all TODO's in a project by using eclipse's Task viewer.

/**
* TODO: Add Method PDFExtract to open and extract data from a PDF file.
* The solution code is commented out right below. When writing the solution,
* you may use this as reference material.
*/

/**** SOLUTION ****
System.out.println("\nTrying to Extract annotation data ...");
InputStream inputStream;
Boolean b = false;
inputStream = doc.exportAnnotations();
if(inputStream == null)
{
System.out.println("Invalid input stream.");
} else {
String annotFile = "Annotations.xml";
//b = false;
try {
b = saveFile(inputStream, annotFile);
} catch (Exception e) {
System.out.println("Error saving annotation file.");
System.out.println(e);
}
if(b == true)
System.out.println ("Annotations were saved to file : " + annotFile);
else
System.out.println("Annotations were not saved to file.");
}*/

Step 5: Ensure you have set the runtime configuration to accept the argument <lab_root>/PutUnderWorkspace/JavaOne2009_docs/DuanesWorld.PDF

Step 6: Run the project. The console should spit out:

Step 7: navigate to the root directory of the package and you will see the file annotations.xml. Open up that file and behold your annotations.

Discussions:

- what file format is the annotations file? What secondary libraries would you use to work with it (JDom)?

- Can you also set or programmatically inject annotations into a document?

- how would you use this in the real world? Collaborative workflows?

If you did, congtratulations!

Proceed to exercise 5

 

Back to top
Next exercise