/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/26755315/how-can-i-convert-xhtml-nested-list-to-pdf-with-itext */ package sandbox.xmlworker; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.ElementList; import com.itextpdf.tool.xml.XMLWorker; import com.itextpdf.tool.xml.XMLWorkerHelper; import com.itextpdf.tool.xml.html.Tags; import com.itextpdf.tool.xml.parser.XMLParser; import com.itextpdf.tool.xml.pipeline.css.CSSResolver; import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline; import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline; import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline; import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; /** * * @author Bruno Lowagie (iText Software) */ @WrapToTest public class NestedListHtml { public static final String HTML = "resources/xml/list.html"; public static final String DEST = "results/xmlworker/nested_list.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new NestedListHtml().createPdf(DEST); } public void createPdf(String file) throws IOException, DocumentException { // Parse HTML into Element list // CSS CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); htmlContext.autoBookmark(false); // Pipelines ElementList elements = new ElementList(); ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null); HtmlPipeline html = new HtmlPipeline(htmlContext, end); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new FileInputStream(HTML)); // step 1 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); for (Element e : elements) { document.add(e); } document.add(Chunk.NEWLINE); Paragraph para = new Paragraph(); for (Element e : elements) { para.add(e); } document.add(para); document.add(Chunk.NEWLINE); PdfPTable table = new PdfPTable(2); table.addCell("Nested lists don't work in a cell"); PdfPCell cell = new PdfPCell(); for (Element e : elements) { cell.addElement(e); } table.addCell(cell); document.add(table); document.close(); } }