I write this email
in sincerity; not to start a flame war of any kind. We are currently
using an old version of pdflib and are looking into switching to iText.
The iText library looks very capable and easy to use. I genuinely want to
use iText so any help in resolving my issue would be
appreciated.
Performance is an
issue for us; we want to switch to iText but not give up the speed to which
we're accustomed. I'm pasting a small program I used to measure pdflib
4.0.3 (yes we're on an old version) against iText 1.3. The tests were run
on my Windows 200 machine.
The program shows
iText to be an order of magnitude slower then pdflib. It generates a pdf
file with 1000 letter-size pages each having the text "Hello" at coordinate 100,
100. For pdflib we render each page to a memory buffer and write it
to disk. For iText I tried writing to a buffered FileWriter AND separately
to a ByteArrayOutputStream in memory with the file write occurring at the end of
processing. The # of milliseconds each test took is displayed
at the end.
Generate big
PDF with 1000 pages
Pdflib took 344
iText took 3188
iText to Memory took 2781
Pdflib took 344
iText took 3188
iText to Memory took 2781
I'm not an iText guru by any means -- far from it! I cobbled this
code together from the examples. I'm hoping you can spot something
inefficient that I'm doing or make suggestions as to improvements I can make to
this program.
Again, I'd love to use iText! Please help me use it
effectively. Thanks!
package
com.system;
import
com.pdflib.pdflib;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.BaseFont;
import
java.io.*;
public class
PdfPerformance
{
private final static float LETTER_WIDTH = 612f;
private final static float LETTER_HEIGHT = 792f;
{
private final static float LETTER_WIDTH = 612f;
private final static float LETTER_HEIGHT = 792f;
private static final int PAGECOUNT =
1000;
public static void main(String[] args) throws
IOException, DocumentException
{
PdfPerformance perf = new PdfPerformance();
{
PdfPerformance perf = new PdfPerformance();
System.out.println("Generate
big PDF with " + PAGECOUNT + " pages");
long
pdflibTime =
perf.generateBigPdfUsingPdflib(PAGECOUNT);
long itextTime = perf.generateBigPdfUsingIText(PAGECOUNT);
long itextToMemory = perf.generateBigPdfUsingITextWithBuffer(PAGECOUNT);
long itextTime = perf.generateBigPdfUsingIText(PAGECOUNT);
long itextToMemory = perf.generateBigPdfUsingITextWithBuffer(PAGECOUNT);
System.out.println("Pdflib
took " +
pdflibTime);
System.out.println("iText took " + itextTime);
System.out.println("iText to Memory took " + itextToMemory);
}
System.out.println("iText took " + itextTime);
System.out.println("iText to Memory took " + itextToMemory);
}
private long generateBigPdfUsingPdflib(int pagecount)
throws IOException
{
long start = System.currentTimeMillis();
{
long start = System.currentTimeMillis();
pdflib pdflib = new
pdflib();
if (pdflib.open_file("") == -1)
throw new IOException("Can't open PDF output buffer");
if (pdflib.open_file("") == -1)
throw new IOException("Can't open PDF output buffer");
OutputStream os = new
BufferedOutputStream(new FileOutputStream("pdflib.pdf",
false));
int helv =
pdflib.findfont("Helvetica", "host", 0);
for( int i = 0; i <
pagecount; i++ )
{
pdflib.begin_page(LETTER_WIDTH, LETTER_HEIGHT);
{
pdflib.begin_page(LETTER_WIDTH, LETTER_HEIGHT);
pdflib.setfont(helv,
10);
pdflib.show_xy("Hello", 100, 100);
pdflib.show_xy("Hello", 100, 100);
pdflib.end_page();
os.write(pdflib.get_buffer());
}
os.write(pdflib.get_buffer());
}
pdflib.close();
os.write(pdflib.get_buffer());
os.close();
os.write(pdflib.get_buffer());
os.close();
return
System.currentTimeMillis() - start;
}
}
private long generateBigPdfUsingIText(int pagecount)
throws IOException, DocumentException
{
long start = System.currentTimeMillis();
{
long start = System.currentTimeMillis();
Document document = new
Document();
PdfWriter writer = PdfWriter.getInstance(document, new BufferedOutputStream(new FileOutputStream("itext.pdf")));
PdfWriter writer = PdfWriter.getInstance(document, new BufferedOutputStream(new FileOutputStream("itext.pdf")));
document.setPageSize(new
Rectangle(LETTER_WIDTH,
LETTER_HEIGHT));
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
for( int i = 0; i <
pagecount; i++ )
{
cb.beginText();
cb.setFontAndSize(bf, 10);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "Hello", 100, 100, 0);
cb.endText();
document.newPage();
}
{
cb.beginText();
cb.setFontAndSize(bf, 10);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "Hello", 100, 100, 0);
cb.endText();
document.newPage();
}
document.close();
return
System.currentTimeMillis() - start;
}
}
private long generateBigPdfUsingITextWithBuffer(int
pagecount) throws IOException, DocumentException
{
long start = System.currentTimeMillis();
{
long start = System.currentTimeMillis();
Document document = new
Document();
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
OutputStream os = new BufferedOutputStream(new FileOutputStream("itext2.pdf", false));
PdfWriter writer = PdfWriter.getInstance(document, baos);
OutputStream os = new BufferedOutputStream(new FileOutputStream("itext2.pdf", false));
document.setPageSize(new
Rectangle(LETTER_WIDTH,
LETTER_HEIGHT));
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
for( int i = 0; i <
pagecount; i++ )
{
cb.beginText();
cb.setFontAndSize(bf, 10);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "Hello", 100, 100, 0);
cb.endText();
document.newPage();
}
{
cb.beginText();
cb.setFontAndSize(bf, 10);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "Hello", 100, 100, 0);
cb.endText();
document.newPage();
}
document.close();
os.write(baos.toByteArray());
os.close();
os.write(baos.toByteArray());
os.close();
return
System.currentTimeMillis() - start;
}
}
}
}
Confidentiality Notice:
If you are not the intended recipient, or the employee or agent responsible for delivering this information to the intended recipient, please do not read, distribute, or reproduce this transmission, you are hereby notified that any dissemination, disclosure, distribution, copying or taking of any action in reliance on the contents of this information is strictly prohibited. This message, including any attachments, may contain CONFIDENTIAL AND/OR LEGALLY PRIVILEGED information. Information contained in this e-mail and any attachments thereto is intended solely for use of the recipient(s) above and may be privileged, confidential, and/or proprietary and may not be disseminated to any other party without XIFIN's written permission. If you have received this e-mail transmission in error, please notify the sender immediately by reply e-mail or at the phone number above and then delete this email.