Java One 2007 is full on! My favorite was the helicopter but I also can't wait to start developing with the SunSpot devices. I think I am going to build a solar tracking device.
Anyways - thanks to all of you who came to my talk. I was overwhelmed with the great reception and applause and felt really humble to be appreciated by such an esteemed and knowledgable peer group. I don't use Java every day so I was happy to see all the code I wrote for you compile the first time. Felt like I dodged a bullet.
Here is the first class (DuanePDFClass2.java). You will need to download the XPAAJ.jar file to make this work. The other class is directly below. You should also know that we are moving towards the XML friendly version of PDF ("Mars") which will allow any developer to work with PDF XML formats using the XML parser in their native language. For the long term, this is a much better solution than us developing libraries for each and every programming language.
import java.io.*;
import java.util.*;
import java.awt.image.DataBuffer;
import com.adobe.pdf.*;
public class DuanePDFClass2 {
public static void main(String[] args)
throws FileNotFoundException, IOException
/* Make sure we have the correct args.length() and call PDFExtract() */
{
String inPdfName;
if(args.length != 1 )
{
System.out.println("\nCommand line format: java classname pdf-file");
return;
}
else
{
inPdfName = new String(args[0]);
PDFExtract(inPdfName);
}
}
public static void PDFExtract(String inPdfName)
throws FileNotFoundException, IOException
{
System.out.println("\nOpening PDF with DuanePDFClass2...");
PDFDocument doc = null;
boolean b = false;
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 ("Document Version = " + doc.getVersion());
System.out.println ("PDF is " + doc.getNumberOfPages() + " pages long.");
/*Save PDF to file*/
System.out.println ("\nSaving document ... ");
int j = inPdfName.lastIndexOf(".");
String outPdfName = inPdfName.substring(0, j) + "_saved_by_Duane_at_JavaOne2007" + ".pdf";
InputStream inputStream;
inputStream = doc.save();
b = false;
try {
b = saveFile(inputStream, outPdfName);
} catch (Exception e) {
System.out.println("Error saving PDF file.");
System.out.println(e);
}
if(b == true)
System.out.println ("Document was saved to file : " + outPdfName);
else
System.out.println("Document was not saved to file.");
System.out.println("\nExecution of DuanePDFClass1 has finished.");
}
/**
method to save InputStream to a file.
*/
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;
}
}
Here is the second class which casts the input stream handed off by the Document factory class into a buffered stream reader and strips out the XMP metadata.
import java.io.*;
import java.util.*;
import java.awt.image.DataBuffer;
import com.adobe.pdf.*;
/* XMPExtractSample
* by Duane Nickull, Adobe Systems Inc. dnickull@adobe.com
* Copyright (c) October 25, 2006 - all rights reserved
*
* Use this at your own risk and don't whine/winge on to me if it doesn't work.
* You will need to have XPAAJ.jar from Adobe labs.
* Written and tested with JDK 1.5 on a mac w/osx 10.4.7
*/
public class XMPExtractSample {
public static void main(String[] args)
throws FileNotFoundException, IOException
/* Make sure we have the correct args.length() and call PDFExtract() */
{
String inPdfName;
if(args.length != 1 )
{
System.out.println("\nCommand line format: java DuanePDFClass1 pdf-file");
return;
}
else
{
inPdfName = new String(args[0]);
PDFExtract(inPdfName);
}
}
public static void PDFExtract(String inPdfName)
throws FileNotFoundException, IOException
{
System.out.println("\nOpening PDF with DuanePDFClass1...");
PDFDocument doc = null;
boolean b = false;
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( "\n" + inPdfName + " was successfully opened.");
// Export the xmp metadata from the document
try {
//Call the PDFDocument object's exportXMP method.
InputStream myXMPStream = doc.exportXMP();
//Get the byte size of the InputStream object.
int numBytes = myXMPStream.available();
System.out.println("\nNumber of XMP Bytes found is " + numBytes + "\n");
// Read into a Buffered Reader Stream.
BufferedReader d = new BufferedReader(new InputStreamReader(myXMPStream));
// Iterate through the XMP object and print each line
String xmpLine;
while((xmpLine = d.readLine()) != null)
{
System.out.println(xmpLine);
}
// Find the Physical Memory Reference of the object
System.out.println("\nXMP InputStream is in physical memory at -> " + d);
//Create an array of bytes. Allocate numBytes of memory.
byte [] MDBytes = new byte[numBytes];
//Read the XMP metadata by calling the InputStream object’s read method.
myXMPStream.read(MDBytes);
} catch (IOException e){
System.out.println("it went really bad" + e );
}
System.out.println("\nXMP Extraction has finished.");
}
}