Hi Tilmann,
I made a small project which creates 32 pdfs with PDFBox and iText. The
output is:
Time elapsed PDFBox: 2521 msec. (2305msec loading)
Time elapsed iText: 661 msec.
It is possible to improve the load function?
It is possible to create a template document before the loop?
Tested with PDFBox 2.0.6 and iText 2.1.7.
I'm not allowed to attach the fonts because of email size limit.
Uwe
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class PDFBoxLoadTTF {
public static void main (final String[] args) throws Exception {
/* Test with PDFBox
*
*/
File file = new File("DejaVuSans.ttf");
FileInputStream fis = null;
fis = new FileInputStream(file);
TrueTypeFont ttf = new TTFParser().parse( fis);
file = new File("DejaVuSans-Bold.ttf");
fis = new FileInputStream(file);
TrueTypeFont ttfBold = new TTFParser().parse( fis);
long timeLoad = 0;
long timeStart = System.currentTimeMillis();
for (int i = 0;i<32; i++ ) {
PDDocument document = new PDDocument();
long timeStartLoad = System.currentTimeMillis();
PDFont ttfFont = PDType0Font.load(document, ttf,true);
PDFont ttfBoldFont = PDType0Font.load(document, ttfBold,true);
timeLoad += (System.currentTimeMillis()-timeStartLoad);
PDPage page1 = new PDPage(PDRectangle.A4);
PDRectangle rect = page1.getMediaBox();
document.addPage(page1);
PDPageContentStream cos = new PDPageContentStream(document, page1);
cos.beginText();
cos.setFont(ttfFont, 12);
cos.newLineAtOffset(100, rect.getHeight()-100);
cos.showText(ttfFont.getName());
cos.endText();
cos.beginText();
cos.setFont(ttfBoldFont, 12);
cos.newLineAtOffset(100, rect.getHeight()-150);
cos.showText(ttfBold.getName());
cos.endText();
cos.close();
document.save("LoadTTF_PDFBox_"+i+".pdf");
document.close();
}
long timeEnd = System.currentTimeMillis();
System.out.println("Time elapsed PDFBox: " + (timeEnd - timeStart) + " msec. (" + timeLoad + "msec loading)");
/* Test with iTextx
*
*/
BaseFont baseFont = BaseFont.createFont("DejaVuSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(baseFont, 12);
BaseFont baseFontBold = BaseFont.createFont("DejaVuSans-Bold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font fontBold = new Font(baseFontBold, 12);
timeStart = System.currentTimeMillis();
for (int i = 0;i<32; i++ ) {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("LoadTTF_iText_"+i+".pdf"));
document.open();
document.add(new Paragraph(font.getFamilyname(), font));
document.add(new Paragraph(fontBold.getFamilyname(), fontBold));
document.close();
}
timeEnd = System.currentTimeMillis();
System.out.println("Time elapsed iText: " + (timeEnd - timeStart) + " msec.");
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]