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
Tuesday, July 28, 2009
Do you like to test your application free?
| Java Application Tools | jDiffChaser | ||
| Web Application Tools | WebInject | Solex | |
| SOAP Application Tools | soapUI | iTKO LISA | MActor |
| AJAX Application Tools | GITAK | Selenium | |
| Database Tools | LReport | DBFire | DTM Data Generator |
| .Net Tools | SharpRobo | WatiN | |
| Linux Tools | BugnuX |
Use Open Source Software Testing Tools …..
Many test teams are paying over the odds for highly priced software test automation tools, in a market dominated by a small handful of commercial vendors. But many freeware and shareware alternatives are available today. In the above table, free testing tools are listed with the suitable application type.
Have a nice day!
Monday, July 27, 2009
Bring HTML, CSS power into PDF using Java
Today web technology has become a very powerful tool in communication. The technology of HTML, XHTML and CSS are very rich and powerful.
If you can convert HTML into PDF with CSS styles, the power of HTML/CSS will creep into PDFs. Yes it is possible,
You will need “Flying Saucer” & “iText” Libraries, which is are free Java libraries to convert XTML, CSS to PDF.
"Flying Saucer" bundles iText library in its binary distribution. The source distribution also includes set of demo and sample codes making it easy to understand the API.
iText : http://www.lowagie.com/iText/
Flying Saucer : https://xhtmlrenderer.dev.java.net/
Subscribe to:
Posts (Atom)