The following Java code shows you how to provide the font face for the created PDF output as same as in the input HTML or XHTML.
public static void createPDF(String url, String pdf)
throws IOException, DocumentException {
OutputStream os = null;
try {
os = new FileOutputStream(pdf);
ITextRenderer renderer = new ITextRenderer();
ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
callback.setSharedContext(renderer.getSharedContext());
renderer.getSharedContext ().setUserAgentCallback(callback);
Document doc = XMLResource.load(new InputSource(url)).getDocument();
renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\Verdana.TTF","UTF-8", BaseFont.NOT_EMBEDDED);
renderer.setDocument(doc, url);
renderer.layout();
renderer.createPDF(os);
os.close();
os = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}
}
"renderer.getFontResolver().addFont()" is the key method used the set the font face information of the PDF generated.
"BaseFont.NOT_EMBEDDED" specify not to embed the font in generated PDF.
"url" is the input HTML/XHTML file path.
"pdf" is the generated PDF file path.
Please find the input XHTML/HTML below:
<html>
<head>
<style type="text/css">
data
{
font-family: "Verdana";
}
</style>
</head>
<body style="font-size:16px">
<data>
<center style="font-size:24px">
<b>Hi Prasad...</b></center>
</data>
</body>
</html>
You can similarly embed the font in the generated PDF by changing parameters passed to "renderer.getFontResolver().addFont"
Thanks, have a nice day!
Thursday, July 30, 2009
Set,Specify PDF font type in Flying Soucer & iText
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment