|
FAQPDF
Frequently-asked questions about creating PDFs
Featured PDF Questions
Versions of iText SupportedRelease 8 supports version 2.x of iText, was shipped with 2.0.8 and we have users who report using 2.1.7; we believe 2.1.7 should be more or less identical to 4.2.0. For iText 5.x support, we do have a contributed itext5 branch on GitHub though for anybody using iText 5+. The changes were mostly just package renames along with a couple of other tweaks. We keep the itext5 branch in sync with master so it should always be current. Watermarks in PDFHow can I "watermark" PDF's created through FS? Either use the background property inside of @page or PdfStamper after the PDF is already created. PdfStamper is part of the iText API. My PDF isn't picking up my CSS!PDF is treated as "print" media; see the CSS 2.1 specification section on media types. Make sure you have specified the media type for your CSS when you link or embed it; use type "print" or "all". How can I print multiple pages on to one PDF, if they come from multiple documents?For the first document in the set (e.g. starting with the first page in the PDF you want to render) take an ITextRenderer and call setDocument(), layout() and createPDF(); for each subsequent document, use the same renderer and call setDocument(), layout(), and writeNextDocument(). Call finishPDF() to complete the process. A sample follows below; a cleaned-up example, PDFRenderToMultiplePages, is in our demos/samples directory in our source distribution. String root = "J:/Temp/fs";
String[] inputs = new String[] { "input1.html", "input2.html", "input3.html" };
String output = "output.pdf";
OutputStream os = null;
try {
os = new FileOutputStream(new File(root, output));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File(root, inputs[0]));
renderer.layout();
renderer.createPDF(os, false);
for (int i = 1; i < inputs.length; i++) {
renderer.setDocument(new File(root, inputs[i]));
renderer.layout();
renderer.writeNextDocument();
}
renderer.finishPDF();
os.close();
os = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}My header is not showing up correctly in my PDF.The values of -fs-flow-top/right/bottom/left and -fs-move-to-flow must be strings and not keywords (e.g. -fs-flow-top: 'header' instead of -fs-flow-top: header ).The new CSS parser validates this (and will give you a warning message if you have logging turned on). How do I set a custom UserAgentCallback when generating PDF?ITextRenderer renderer = new ITextRenderer(); ITextUserAgent callback = new ITextUserAgent(renderer.getOutputDevice()); callback.setSharedContext(renderer.getSharedContext()); renderer.getSharedContext().setUserAgentCallback(callback); renderer.setDocument(url); renderer.layout(); renderer.createPDF(os); How can I set the document properties when generating a PDF?You can set a PDFCreationListener on your ITextRenderer instance to do whatever you want with the PDF either before the iText document is opened or right before it's about to be closed. What about PDF bookmarks?See HowTo_PDF_bookmarks_from_xhtml_markup for more information. Embedding fonts using CSS @font-faceYou can embed custom fonts via CSS. Make sure the path to the font file is relative to the path of the CSS file. @font-face {
font-family: "UbuntuMono";
src: url("UbuntuMono-R.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
* {
font-family: "UbuntuMono";
}
|