To create the PdfReader:
     if (strPassword != null)
     {
        reader = new PdfReader(strFile, strPassword.getBytes());
     }
     else
     {
        reader = new PdfReader(strFile);
     }

Then, I get a copy of the RandomAccessFileOrArray:
     file = reader.getSafeFile();
     file.reOpen();

Extracting the stream is kinda complex. I use reader.getPageN() to get the PdfDictionary for each page, and pass it to the following recursive function which walks through the pdf until it gets to the single image stream for the page, and returns it.
  private PRStream getImageStream(PdfObject obj, int page)
  {
     PdfName im = new PdfName("Im" + page);
     PdfName xf = new PdfName("Xf" + page);
     PdfDictionary dict;
     if (obj.isStream())
     {
        dict = (PdfDictionary)((PRStream)obj).get(PdfName.RESOURCES);
     }
     else
     {
        dict = (PdfDictionary)((PdfDictionary)obj).get(PdfName.RESOURCES);
     }

     dict = (PdfDictionary)dict.get(PdfName.XOBJECT);
     if (dict.contains(im))
     {
        return (PRStream)PdfReader.getPdfObject(dict.get(im));
     }
     return getImageStream(PdfReader.getPdfObject(dict.get(xf)), page);
  }

Next, I use PdfReader.getStreamBytesRaw() to get the raw bytes for the image stream. Then, I use TIFFFaxDecoder to decode the byte stream (our images are CCITT fax encoded). Finally, I use JAI functionality to wrap a BufferedImage around the byte stream and use TIFFImageEncoder to encode the image to a FileOutputStream. Works like a charm except for the "too many files open" exception every few thousand or so pdfs.
John Pruitt

Paulo Soares wrote:

How are you creating the PdfReader? How are you extracting the stream?
-----Original Message-----
From: John Pruitt [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 20, 2005 4:38 PM
To: Paulo Soares
Cc: [email protected]
Subject: Re: [iText-questions] Possible File Resource Leak

Actually, I am converting pdfs to tiff. We have a special case where our pdfs all consist of full-page images. I use iText to get to the image streams in the pdf, and use JAI to encode the images to tiff. I am closing both the RandomAccessFileOrArray, and the PdfReader I use (although I think PdfReader.close() also closes the RandomAccessFileOrArray). Is there anything else I need to close?
John Pruitt

Paulo Soares wrote:

I suppose you are converting tiff to pdf and not the other
way around.
Check that your code is something like this:

RandomAccessFileOrArray ra = null;
try {
  ra = new RandomAccessFileOrArray(imgb);
  Image img = TiffImage.getTiffImage(ra, 1);
  return img;
} finally {
  if (ra != null)
      ra.close();
}

It will make sure that the file is closed if some exception
is throwned.


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of John Pruitt
Sent: Tuesday, December 20, 2005 3:15 PM
To: [email protected]
Subject: [iText-questions] Possible File Resource Leak

I have a program that basically batch converts pdfs to multi-page tiffs using iText. I am running into a problem. In my testing, I had the program batch convert 12,000 pdf files to multi-page tiff. The program worked like a charm for 8 hours, then it hit an exception: "To many open files." The program logged the exception and continued for 8 more hours before hitting the same exception again. After that, the exception came more frequently, every 2 hours or so. I have checked my code, and am closing file streams as I
finish with
them. I was wondering how likely it is that iText may not
be properly
releasing resources, and where I should look in the code to see.
Thank you,
John Pruitt


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to