Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] Out of memory copying pdf documents

Odino Mauro

2005-06-23


Hi,
i'm trying to append a large number (about 2000 or more) of small pdf
documents (1 or 2 pages) in single document, by my program runs out of
memory in a short time.

This is my code:

  public int appendDocs(Connection conn, String arg1, int arg2, int arg3)
throws Exception{
    DataSet ds = GestCedo.getSqlLayer().getCedolini(conn, arg1, arg2,
arg3);
    ResultSet rs = ds.getRs();
    Document destination = new Document(PageSize.A4);
    PdfCopy writer = new PdfCopy(destination, new
FileOutputStream(fileName));
    destination.open();
    int n = 0;
    int totPages = 0;
    try {
       while (rs.next()) {
          n++;
          BLOB pdf = ((OracleResultSet)rs).getBLOB(1);
          InputStream is = pdf.getBinaryStream();
          PdfReader reader = new PdfReader(is);
          reader.consolidateNamedDestinations();
          totPages = reader.getNumberOfPages();
          for (int i = 1; i<=totPages; i++){
            PdfImportedPage page = writer.getImportedPage(reader,
i);
            writer.addPage(page);
          }
          writer.freeReader(reader);
          is.close();
          is = null;
          reader = null;
          pdf = null;
          if (n%100==0) {
            Runtime.getRuntime().gc(); //unuseful try
          }
       }
    } catch (Exception e){
       System.out.println(e.getMessage());
    }
    destination.close();
    rs.close();
    ds.getSt().close();
    return n;
  }

Anyone can suggest me a solution?

Mauro Odino
AMT Genova spa
Direzione Servizi Informatici
010-5582404
mauro.odino@(protected)



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
©2008 junlu.com - Jax Systems, LLC, U.S.A.