Friday, September 01, 2006

Using Adobe's XPAAJ.jar via Line Command on Mac OSX (Intel chipset)

For those of you who have been not using the XPAAJ.jar library only because you are Mac based, you may want to read this post. While the library does not claim to be supported on Mac, I did manage to get it to work but had to re-tweak a few things in the sample code and also study my Unix-Java Command line syntax. The tweaked code is at the bottom.

Adobe's XPAAJ:

XPAAJ is an API library from Adobe for PDF documents. The acronym means Adobe® XML/PDF Access API for Java™ (XPAAJ). It can now be downloaded freely from the Adobe Developer Network at http://www.adobe.com/devnet/livecycle/downloads/xpaaj.html. Once you download the zip, unzip it to a directory where you will work on it. The directory structure will be as follows:

Xpaaj_sdk
|
|-- xpaaj.jar
|---/samples
|---/docs

Open a terminal and navigate to the /xpaaj_sdk/samples/command-line/ConsoleSample/ directory. There you will see some sample classes and a test.pdf file. I did not have luck with all the sample files so I opened PDFExtract and modified it a bit.

For the record, my java –version is:
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-112)
Java HotSpot(TM) Client VM (build 1.5.0_06-64, mixed mode, sharing)
Mac is OSX 10.4.7 on the MacBook Pro.

If you want to run some samples, I recommend cutting and pasting the text at the end of this blog and saving it as the file “DuanePDFClass1.java” on your hard drive. One thing you also might want to consider is to also copy the xpaaj.jar file to the ~/ConsoleSample directory to make it easier to use to compile and run the DuanePDFClass1 class since you can simply cut and paste the line commands below.

From that directory, compile the sample by typing in the following:

javac -classpath ./XPAAJ.jar DuanePDFClass1.java

It should compile. Try typing ls –la to make sure that the resulting class file exists and that it is executable. To run it, type:

java -classpath ./xpaaj.jar: DuanePDFClass1 test.pdf

test.pdf is the argument for the test file that it will open, tell you a few tings about and save. You should see the following appear in your line command window:

duane-nickulls-computer:~/code/xpaaj_sdk/samples/command-line/ConsoleSample nickull$ java -classpath ./xpaaj.jar: DuanePDFClass1 test.pdf

Opening PDF with DuanePDFClass1...
test.pdf was successfully opened.
PDF version = %PDF-1.5
Number of pages = 1

Saving document ...
Document was saved to file : test_saved_by_DuanePDFClass1.pdf

Execution of DuanePDFClass1 has finished.

Anyhow, that is all for now. I will be demonstrating this during the Adobe Developer Week in London, UK next Wednesday. Here is the classfile text (use at your own risk, no guarantees blah blah blah....):

import java.io.*;
import java.util.*;
import java.awt.image.DataBuffer;
import com.adobe.pdf.*;

public class DuanePDFClass1 {

public static void main(String[] args)
throws FileNotFoundException, IOException
{
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( inPdfName + " was successfully opened.");


/* Try some methods here */
System.out.println ("PDF version = " + doc.getVersion());

System.out.println ("Number of pages = " + doc.getNumberOfPages());



/*Save PDF to file*/
System.out.println ("\nSaving document ... ");
int j = inPdfName.lastIndexOf(".");
String outPdfName = inPdfName.substring(0, j) + "_saved_by_DuanePDFClass1" + ".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;
}
/* TODO: add some more methods here?? */
}

2 comments:

  1. UPDATE:

    Please 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/

    ReplyDelete
  2. UPDATE:

    Please 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/
    # posted by Duane "Chaos" Nickull : 2010-03-15 9:37:00 A

    ReplyDelete

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.