Bruno,
Really appreciate the quick feedback, based on what you said and the quick
example I was able to do what I needed and get it up and working in a matter of
minutes.
Big Thanks!
Adam
----- Original Message ----
From: Bruno Lowagie <[EMAIL PROTECTED]>
To: Post all your questions about iText here
<[email protected]>
Sent: Wednesday, June 6, 2007 5:25:08 AM
Subject: Re: [iText-questions] Read in single PDF, generate copies and stamp,
concatenate, output to ByteArrayOutputData
Adam Leszinski wrote:
> I'm looking to create a downloads page where users will have the ability
> to generate a number of copies of a given selected PDF document.
OK.
> Under the hood, each of these documents will be stamped with a number on
> the first page.
OK.
> Example:
>
> 1) User selects a PDF and selects to have 3 copies.
> 2) 3 numbers are going to be generated by our database (1 for each first
> page)
> 3) All three copies will be read, stamped, and sent to the
> ByteArrayOutputStream
> 4) user views a single PDF in the browser with three copies of the
> document (concatenated, 1 right after another, each first page has a
> unique number that was stamped).
OK.
> Currently I have a working version, but this simply reads the original
> PDF, stamps with the number, and is sent to the output stream.
That's too soon, you want to keep it in memory
for a short while longer, so that you can add
the stamped copies that follow.
> I've looked through several examples out there for some ideas, but where
> I see document concatenation the PDFCopy is being used. Where I
> see a document being stamped with something, I see the PDFStamper. Both
> are considered the "writer" so how could I use both?
That's discussed in chapter 2 of my book:
"2.3: creating a PDF in multiple passes".
http://itext.ugent.be/itext-in-action/chapter.php?chapter=2
You need a loop. This is some pseudo code resembling Java:
ByteArrayOutputStream realBaos = new ByteArrayOutputStream();
Document document = new Document();
PdfSmartCopy copy = new PdfSmartCopy(document, realBaos);
document.open();
while (needMoreCopies()) {
PdfReader reader = new PdfReader(template);
ByteArrayOutputStream tempBaos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, tempBaos);
doStuffWith(stamper);
stamper.close();
reader = new PdfReader(tempBaos.toByteArray());
while (hasMorePages(reader)) {
copy.addPage(copy.getImportedPage(reader, nextPage()));
}
}
document.close();
I use PdfSmartCopy instead of PdfCopy because you said
the template was always the same document, PdfSmartCopy
needs more memory than PdfCopy, but will result in smaller
PDF files (less bytes to be sent to the browser).
Methods needMoreCopies(), doStuffWith(), hasMorePages()
en nextPage() are not existing methods, I just used a
pseudo-method you can replace with actual code.
When I write new PdfReader(template), template isn't
necessarily a String or just one parameter, you can
improve performance by using another constructor.
br,
Bruno
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/