This will work correctly:
private PdfReader emptyPdfPage;
� private PdfReader getEmptyPDFPage(){
� � if (emptyPdfPage==null){
� � � ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
� � � try {
� � � � Document emptyDoc = new Document(document.getPageSize());
� � � � PdfWriter w = PdfWriter.getInstance(emptyDoc, baos);
� � � � emptyDoc.open();
� � � � emptyDoc.getDirectContent().setLiteral(' ');
� � � � emptyDoc.close();
� � � }
� � � catch (DocumentException ex) {
� � � � ex.printStackTrace();
� � � � throw new RuntimeException();
� � � }
� � � this.emptyPdfPage = new PdfReader(baos.toByteArray());
� � }
� � return new PdfReader(emptyPdfPage);
� }
Best Regards,
Paulo Soares
-----Original Message-----
From: [EMAIL PROTECTED] [SMTP:[EMAIL PROTECTED] On Behalf Of Sascha Schmidt
Sent: Thursday, December 04, 2003 14:05
To: [EMAIL PROTECTED]
Subject: Antwort: Re: Antwort: Re: Antwort: AW: [iText-questions] Merging PDFs with blank pages
Paulo, you are the man! Good idea! Here comes a solution for those people who have similar problems:
� private void append(PdfReader reader) throws BadPdfFormatException,
� � � IOException, DocumentException {
� � reader.consolidateNamedDestinations();
� � if (document == null) {
� � � document = new Document(reader.getPageSizeWithRotation(1));
� � � writer = new PdfCopy(document, out);
� � � document.open();
� � }
� � final int n = reader.getNumberOfPages();
� � for (int i = 0; i < n; )
� � � writer.addPage(writer.getImportedPage(reader, ++i));
� � if (reader.getAcroForm() != null)
� � � writer.copyAcroForm(reader);
� � if (forceEvenPageCount && (n%2 ==1) ){
� � � writer.addPage(writer.getImportedPage(new PdfReader(getEmptyPDFPage()),1));
� � }
� }
� private byte[] getEmptyPDFPage(){
� � if (emptyPdfPage==null){
� � � // resulting pdf has a size of 934 bytes
� � � ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
� � � try {
� � � � Document emptyDoc = new Document(document.getPageSize());
� � � � PdfWriter w = PdfWriter.getInstance(emptyDoc, baos);
� � � � emptyDoc.open();
� � � � emptyDoc.add(new Chunk(" "));
� � � � emptyDoc.close();
� � � }
� � � catch (DocumentException ex) {
� � � � ex.printStackTrace();
� � � � throw new RuntimeException();
� � � }
� � � this.emptyPdfPage = baos.toByteArray();
� � }
� � return emptyPdfPage;
� }
Sascha
Paulo Soares <[EMAIL PROTECTED]>
04.12.2003 14:08 An
Sascha Schmidt <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
Kopie
Thema
Re: Antwort: Re: Antwort: AW: [iText-questions] Merging PDFs with blank pages
PdfCopy only appends existing documents. If you want
an empty page, create in memory a new pdf with a blank
page, read it and include it with PdfCopy.
Best Regards,
Paulo Soares
--- Sascha Schmidt <[EMAIL PROTECTED]>
wrote:
> Hello Isabelle!
>
> Unfortunately this doesn't work too. But thanks for
> this tip. I tried
> this:
>
> � � � document.add(new Chunk("test123"));
> � � � document.newPage();
> � � � document.add(new Chunk("test124"));
>
> ...and I couldn't find "test123" or "test124" in the
> resulting PDF
> document. It seems to me that the PdfCopy-Writer
> works different to the
> PdfWriter.
> Can somebody of the developers help me?
>
> Sascha
>
>
>
>
>
> Isabelle Guimiot <[EMAIL PROTECTED]>
> Gesendet von:
> [EMAIL PROTECTED]
> 04.12.2003 11:42
> Bitte antworten an
> [EMAIL PROTECTED]
>
>
> An
> [EMAIL PROTECTED]
> Kopie
>
> Thema
> Re: Antwort: AW: [iText-questions] Merging PDFs with
> blank pages
>
>
>
>
>
>
> Hi !
>
> you wrote :
> document.add(new Chunk(" "));
> document.newPage();
>
> You have to add the empty chunk AFTER calling
> newPage(), not before...
>
> Isabelle
>
>
>
> From: Sascha Schmidt
> <[EMAIL PROTECTED]>
> Date: Thu, 4 Dec 2003 11:04:44 +0100
>
> Dies ist eine mehrteilige Nachricht im MIME-Format.
>
------=_NextPartTM-000-0011a2c9-20b0-11d8-90e3-0050043ad580
> Content-Type: multipart/alternative;
> boundary="=_alternative 00373E27C1256DF2_="
>
> --=_alternative 00373E27C1256DF2_=
> Content-Type: text/plain; charset="ISO-8859-1"
> Content-Transfer-Encoding: quoted-printable
>
> Hello again!
>
> Forget my last mail, it was non-sense. But I'm still
> unable to insert
> an=20
> empty page after an imported document. I've debugged
> a
> little bit
> but=20
> cannot find the reason.
> Has somebody else an idea? This is my new snippet:
>
> private void append(PdfReader reader) throws
> BadPdfFormatException,
> IOException, DocumentException {
> reader.consolidateNamedDestinations();
>
> if (document =3D=3D null) {
> document =3D new
> Document(reader.getPageSizeWithRotation(1));
> writer =3D new MyPdfCopy(document, out);
> document.open();
> }
>
> final int n =3D reader.getNumberOfPages();
> for (int i =3D 0; i < n; )
> writer.addPage(writer.getImportedPage(reader,
> ++i));
> =20
> if (reader.getAcroForm() !=3D null)
> writer.copyAcroForm(reader);
>
> //if (forceEvenPageCount && (n%2 =3D=3D1) ){
> document.add(new Chunk(" "));
> document.newPage();
> //}
>
> }
>
>
> Thanks a lot guys.
>
> Sascha
>
>
>
____________________________________________________________________________________
> Do You Yahoo!? -- Avec Yahoo! soyez au coeur de la
> r�colte de dons pour le
> T�l�thon.
> http://fr.promotions.yahoo.com/caritatif/
>
>
>
-------------------------------------------------------
> This SF.net email is sponsored by: SF.net Giveback
> Program.
> Does SourceForge.net help you be more productive?
> Does it
> help you create better code? �SHARE THE LOVE, and
> help us help
> YOU! �Click Here: http://sourceforge.net/donate/
> _______________________________________________
> iText-questions mailing list
> [EMAIL PROTECTED]
>
https://lists.sourceforge.net/lists/listinfo/itext-questions
>
__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/
