Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

concat pdf files

807588May 14 2009
Hi all,

I'm using iText to build pdf files.
In my process, I'm building lot of documents (suppose doc1.pdf, doc2.pdf..... doc1000.pdf).

Each document contains 2 pages (recto-verso if printed individually).

Now, I have to build a unique pdf file, assembling all the docs with the following rules ( constraints given for printing ):
The final doc size is twice.
On the first page, the 1st pages of doc 1 and 2,
On the second page, the 2nd pages of doc 1 and 2,
On the third page, the 1st pages of doc 3 and 4,
On the fourth page, the 2nd pages of doc 3 and 4,
and so on.

I made a prog and you will find after the source code.
My problem is a problem of size.
Each document is 7Mo, so when assembling 100 docs, the program stops at the "document.close().
Assembling 40 is ok, more is a problem.

I suppose ther is buffer and I'm running out of it. Is there a solution to write directly on disk so that I won't have a problem of size ?

Thanks if you could help.



Document document = new Document(finalPageSize, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream(finalFileName) );
document.open();
PdfContentByte cb = writer.getDirectContent();

int current = startNo;

for (int sheetNo=0; sheetNo<nbSheets; sheetNo++){
// Pages Recto
document.newPage();
for (int pageNo=1; pageNo<=2; pageNo++){

PdfImportedPage page;
String fileName = new String( pageComponent + (current+pageNo-1) + ".pdf");

PdfReader reader = new PdfReader(pdfFilesRep + fileName);
page = writer.getImportedPage(reader, 1);

Rectangle pageArea = reader.getPageSize(1);
float pageWidth = pageArea.getWidth();
float pageHeight = pageArea.getHeight();
float positionX = position[pageNo-1][0] * coefCmToPoints;
float positionY = position[pageNo-1][1] * coefCmToPoints;
cb.addTemplate(page, 1f, 0, 0, 1f, positionX, positionY);
reader.close();

}

// Pages Verso
document.newPage();
for (int pageNo=1; pageNo<=2; pageNo++){

PdfImportedPage page;
String fileName = new String( pageComponent + (current+pageNo-1) + ".pdf");

PdfReader reader = new PdfReader(pdfFilesRep + fileName);
page = writer.getImportedPage(reader, 2);

Rectangle pageArea = reader.getPageSize(1);
float pageWidth = pageArea.getWidth();
float pageHeight = pageArea.getHeight();
float positionX = position[pageNo-1][0] * coefCmToPoints;
float positionY = position[pageNo-1][1] * coefCmToPoints;
cb.addTemplate(page, 1f, 0, 0, 1f, positionX, positionY);
reader.close();

}
current += 2;
}
document.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 11 2009
Added on May 14 2009
0 comments
229 views