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.");
}
}
Canadian Cybertech assists with Clean Technology adoption ranging from software systems architecture, system design and advancement of user experiences/security. We have over 25 years of experience helping companies gather the full and auditable requirements for IT projects to ensure success.
Thursday, May 10, 2007
5 comments:
Do not spam this blog! Google and Yahoo DO NOT follow comment links for SEO. If you post an unrelated link advertising a company or service, you will be reported immediately for spam and your link deleted within 30 minutes. If you want to sponsor a post, please let us know by reaching out to duane dot nickull at gmail dot com.
Subscribe to:
Post Comments (Atom)
Dwain:
ReplyDeleteThanks. your talk rocked. Where are the slides? Can I get those too?
Hi,
ReplyDeleteHow is the status of xpaaj? Is the development and support dropped?
http://www.adobeforums.com/webx/.3c05d833
@anonymous: The slides are here: http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-93260.pdf
The library previously known as XPAAJ has been replaced with a newer set documented here:
ReplyDeletehttp://www.adobe.com/support/documentation/en/livecycle/
If you are interested, I can provide developer DVD's with these and other resources.
Ahhh, final a answer i can work with. Thanks! I have already read through the documantation of Lifecycle ES. Yes, a developerdvd would be great for evaluating the effort to migrate our prototype.
ReplyDeleteI send my Contact per mail to get in touch.
UPDATE:
ReplyDeletePlease be aware that XPAAJ is no longer supported or available from Adobe Systems.
Those customers familiar with XPAAJ will appreciate the new Java libraries which are now part of LiveCycle ES. These Java libraries provide more functionality than before, and allow Java developers to more easily build Java applications that work with PDF documents. Because we are now offering the entire LiveCycle ES suite in a trial version, XPAAJ is no longer available.
http://www.adobe.com/devnet/livecycle/trial/