-none- 2007-09-05 - By Bruno Lowagie
Back pchoul wrote: > Hello, > > I have a question about IText functionallity. > I was wondering if IText is capable of retrieving the size of the file > its writing to, before it closes that file. > > e.g. > If I concatenate some files in a folder using PdfCopyFields I will not be > able to retrieve the > size of the file im writing to using new File("fileName.pdf").length() > until I say PdfCopyFields.close() > > What I am trying to do is merge a lot of files into large files, but of a > certain size.
How much accuracy do you require? iText works as follows: you create a new Document object (1). You tell a PdfWriter it should listen to the document and write content to an OutputStream (2). When you open the document, the first bytes are written to the OutputStream (3). Then you start adding content (4). iText tries to write as much information as possible to the OutputStream: resources that are used (Images, Content Streams of completed pages,...). However there is always data that must stay in memory and that can only be written to the OutputStream at the moment the document is closed (5). This data includes: the Cross-Reference Table, Outlines, templates,...
Depending on the way you are using iText, you could use a ByteArrayOutputStream as OutputStream and check the number of bytes written at specific moment. This will not be accurate of course, because iText will at least need to add the Cross-Reference Table and the PDF trailer at the end of the file, but it could give you the indication you need.
If you are merging documents, I'd suggest that you create temporary document in memory (ByteArrayOutputStream) like this: (PSEUDO CODE)
MyPdf[] series_of_pdfs; bytes[] result = readBytes(series_of_pdfs[0]); int count = 1; while (count < series_of_pdfs.length) { PdfReader = new PdfReader(tmp); ByteArrayOutputStream baos = new ByteArrayOutputStream(); merge2Pdfs(baos, reader, series_of_pdfs[count]); byte[] tmp = baos.toByteArray(); if (tmp.length < MAXIMUM) { result = tmp; } else { break; } }
This piece of pseudo code is very incomplete. I think it's not very efficient, but maybe it can inspire you to build a better solution. br, Bruno
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ __ ____ ____ ____ ____ ____ ____ ____ ____ ____ iText-questions mailing list iText-questions@(protected) https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://itext.ugent.be/itext-in-action/
|
|