HtmlMovies2.java in Java

Drawing QR-Code in Java HtmlMovies2.java

Listing 9.16 HtmlMovies2.java
Create QR Code JIS X 0510 In Java
Using Barcode encoder for Java Control to generate, create QR Code JIS X 0510 image in Java applications.
www.OnBarcode.com
Recognize QR Code In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
HtmlMovies2 movies = new HtmlMovies2(); StyleSheet styles = new StyleSheet(); styles.loadTagStyle("ul", "indent", "10"); styles.loadTagStyle("li", "leading", "14"); styles.loadStyle("country", "i", ""); styles.loadStyle("country", "color", "#008080"); styles.loadStyle("director", "b", ""); styles.loadStyle("director", "color", "midnightblue"); movies.setStyles(styles); HashMap map = new HashMap(); map.put("font_factory", new MyFontFactory()); map.put("img_provider", new MyImageFactory()); movies.setProviders(map);
UPC-A Supplement 5 Generator In Java
Using Barcode creation for Java Control to generate, create GS1 - 12 image in Java applications.
www.OnBarcode.com
Matrix Drawer In Java
Using Barcode creation for Java Control to generate, create Matrix Barcode image in Java applications.
www.OnBarcode.com
Integrating iText in your web applications
Linear Barcode Maker In Java
Using Barcode encoder for Java Control to generate, create 1D image in Java applications.
www.OnBarcode.com
Creating Data Matrix ECC200 In Java
Using Barcode maker for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
Figure 9.11 HTML snippets converted to PDF using styles
Code-128 Drawer In Java
Using Barcode creator for Java Control to generate, create Code 128C image in Java applications.
www.OnBarcode.com
Creating Leitcode In Java
Using Barcode maker for Java Control to generate, create Leitcode image in Java applications.
www.OnBarcode.com
With the loadTagStyle() method, you can adapt the style of specific tags. In listing 9.16, you change the indentation of unordered lists and reduce the leading of the list items. The method loadStyle() is used to change the style of the tags with a class attribute. The style for country elements is changed to italic; the style is changed to bold for the director class. The color is changed too. The result is shown in figure 9.11. Observe that, although you didn t specify a font face or size in the StyleSheet, another font was used. You could change the font using loadTagStyle() or loadStyle(). For instance,
Encode QR Code In Java
Using Barcode generator for Android Control to generate, create QR image in Android applications.
www.OnBarcode.com
Encoding QR-Code In None
Using Barcode creator for Online Control to generate, create QR Code image in Online applications.
www.OnBarcode.com
styles.loadTagStyle("li", "face", "courier"); styles.loadStyle("country", "size", "10pt");
Printing PDF-417 2d Barcode In None
Using Barcode creator for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Paint PDF 417 In Visual Studio .NET
Using Barcode drawer for .NET framework Control to generate, create PDF417 image in .NET framework applications.
www.OnBarcode.com
For this example, you can use a custom FontProvider instead.
Encoding UPC-A In VB.NET
Using Barcode creator for .NET Control to generate, create UPC Symbol image in VS .NET applications.
www.OnBarcode.com
Denso QR Bar Code Creation In Visual Studio .NET
Using Barcode generator for .NET framework Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
IMPLEMENTING THE FONTPROVIDER INTERFACE
Generating GS1 - 12 In None
Using Barcode maker for Font Control to generate, create Universal Product Code version A image in Font applications.
www.OnBarcode.com
PDF 417 Creation In None
Using Barcode creator for Software Control to generate, create PDF-417 2d barcode image in Software applications.
www.OnBarcode.com
The next listing is a simple example of how you can implement the two methods of the FontProvider interface. The getFont() method returns a Times-Roman font, no matter what font is defined in the HTML snippet or the StyleSheet.
Encode Linear In VB.NET
Using Barcode encoder for .NET framework Control to generate, create 1D Barcode image in .NET framework applications.
www.OnBarcode.com
Data Matrix ECC200 Decoder In VS .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Listing 9.17 HtmlMovies2.java
Read PDF 417 In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Barcode Generator In Objective-C
Using Barcode drawer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
public static class MyFontFactory implements FontProvider { public Font getFont(String fontname, String encoding, boolean embedded, float size, int style, BaseColor color) { return new Font(FontFamily.TIMES_ROMAN, size, style, color); } public boolean isRegistered(String fontname) { return false; } }
Creating basic building blocks from HTML and XML
Another implementation of the FontProvider interface can be found in iText s source code. If you don t define a font_factory, HTMLWorker will use the class FontFactoryImp, which is much more elaborate than this simple MyFontFactory example. This class will be discussed in more detail in chapter 11. Figure 9.11 also shows that the HTML snippet used in this second HTML example is a tad more complex than the snippet used in the first one. It now involves a <table> tag that will result in a PdfPTable object and an <img> tag that should result in an Image object.
IMPLEMENTING THE IMAGEPROVIDER INTERFACE
The <img> tag, more specifically its src attribute, can cause a problem when you re using relative paths for the images. You re creating an HTML file with the HTML snippets in the ./results/part3/chapter09/ directory, but you re referring to images that are in the ./resources/posters/ folder. This is an example of such an <img> tag:
<img src="../../../resources/posters/0062622.jpg" />
If you use HTMLWorker without an ImageProvider, iText won t be able to find this image because it will be looking for it in the directory ../../../resources/posters/ instead of in the ./resources/posters/ folder. You can resolve this by defining an img_provider in listing 9.16. Here is the implementation.
Listing 9.18 HtmlMovies2.java
public static class MyImageFactory implements ImageProvider { public Image getImage(String src, HashMap h, ChainedProperties cprops, DocListener doc) { try { return Image.getInstance(String.format("resources/posters/%s", src.substring(src.lastIndexOf("/") + 1))); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
We re working with HTML snippets in these examples. We haven t defined at which point a snippet becomes a complete HTML page. If you inspect the source code of the HtmlMovies2 example, you ll see that there s also a createPdf() method that parses the complete HTML file. It s certainly possible to use iText to parse larger HTML files, but remember that iText wasn t designed for this kind of work. It s not a full-blown HTML parser; there are better tools available if converting HTML to PDF is your main purpose. The same goes for parsing XML. When talking about XML and PDF, people often refer to Formatting Objects (FO), and Formatting Objects Parsers (FOP), such as Apache FOP. But that doesn t mean you can t use iText to convert XML to PDF, as you ll see in the next section.
Copyright © OnBarcode.com . All rights reserved.