Hi everybody,
I am using FOP to process XML to PDF.
FOP need a lot of memory to add a simple number of page in a document.
In order to resolve this problem, I am using Itext in post processing for adding a number of page.
I find 2 methods to do that :
1)
protected void imprimeNbPage(
Integer nbPages, byte[] pdf, OutputStream sortieItext)
throws Exception {
PdfReader originalPDF;
int nbPagesPrimitif = nbPages.intValue();
long debut = System.currentTimeMillis();
long fin = 0; try { originalPDF = new PdfReader(pdf);
PdfStamper stamp = new PdfStamper(originalPDF, sortieItext);
int i = 0;PdfContentByte cb;
BaseFont bf =
BaseFont.createFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
while (i < nbPagesPrimitif) {
i++;cb = stamp.getOverContent(i);
cb.beginText();
String text = new String("page " + i + "/" + nbPages);
cb.setFontAndSize(bf, 8);if (
PageSize.A4.height() == originalPDF.getPageSizeWithRotation(
i).height()) {
cb.setTextMatrix(525, 15);
} else {
// format paysage, equivalent � PageSize.A4.height() ==
// originalPDF.getPageSizeWithRotation(i).rotate().height()
cb.setTextMatrix(772, 15);
}
cb.showText(text);
cb.endText();
}stamp.close();
fin = System.currentTimeMillis();
} catch (Exception e) {
log.error(
e.getMessage(),
e);
} finally {
if (log.isEnabledFor(NiveauPerfLog4j.PERF)) {
String message =
"iText => ".concat(nbPages.toString()).concat(" ").concat(
Long.toString(fin - debut));
log.log(NiveauPerfLog4j.PERF, message);
}
try {
sortieItext.close();
} catch (IOException e) {
log.debug(
e.getMessage(),
e);
}
}
}2)
protected void imprimeNbPage(
Integer nbPages,
byte pdf[],
OutputStream sortieItext)
throws Exception {
PdfReader originalPDF; int nbPagesPrimitif = nbPages.intValue();
long debut = System.currentTimeMillis();
long fin = 0;
try {
originalPDF = new PdfReader(pdf);
Document document =
new
Document(originalPDF.getPageSizeWithRotation(1));
PdfWriter writer =
PdfWriter.getInstance(document, sortieItext);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
int rotation;
int i = 0;
while (i < nbPagesPrimitif) {
i++;
document.setPageSize(originalPDF.getPageSizeWithRotation(i));
document.newPage(); page = writer.getImportedPage(originalPDF, i);
rotation = originalPDF.getPageRotation(i);
BaseFont bf =
BaseFont.createFont(
BaseFont.HELVETICA,
BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
String text = "page " + i + "/" + nbPages;
cb.beginText();
cb.setFontAndSize(bf, 8);
if (PageSize.A4.height()
==
originalPDF.getPageSizeWithRotation(i).height()) {
cb.setTextMatrix(525, 15);
} else {
// format paysage, equivalent �
PageSize.A4.height() ==
//
originalPDF.getPageSizeWithRotation(i).rotate().height()
cb.setTextMatrix(772, 15);
}
cb.showText(text);
cb.endText(); // ajout de la page dans le document
if ((rotation == 90) || (rotation == 270)) {
// transformation rotation
[[0,-1,0][1,0,0][0,x,1]]
cb.addTemplate(
page,
0,
-1f,
1f,
0,
0,
originalPDF.getPageSizeWithRotation(i).height());
} else {
// transformation identit�
[[1,0,0][0,1,0][0,0,1]]
cb.addTemplate(page, 1f, 0, 0, 1f, 0,
0);
}
}
// fermeture du document
document.close();
fin = System.currentTimeMillis();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
if (log.isEnabledFor(NiveauPerfLog4j.PERF)) {
String message =
"iText =>
".concat(nbPages.toString()).concat(" ").concat(
Long.toString(fin - debut));
log.log(NiveauPerfLog4j.PERF, message);
}
try {
sortieItext.close();
} catch (IOException e) {
log.debug(e.getMessage(), e);
} }
}----------------------------------------------------------------------------------------------------------------------------------
The second method has better performance than the first.
Nethertheless I need to compare the MD5 sum of documents. The second method generate an ID in the PDF so the sum is never the same at each generation of the same document.
That's why I am using the first method.
Is it possible to avoid the ID in the document ? Or to improve the first method performance ?
------------------------------------------------------- This SF.Net email is sponsored by: NEC IT Guy Games. Get your fingers limbered up and give it your best shot. 4 great events, 4 opportunities to win big! Highest score wins.NEC IT Guy Games. Play to win an NEC 61 plasma display. Visit http://www.necitguy.com/?r=20 _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions
