Converting Html To A Multi-column Pdf
I am trying to generate a multi-column PDF from HTML using iText for .NET. I am using CSS3 syntax to generate two columns. And below code is not working for me. CSS column-count
Solution 1:
The CSS property column-count
is not supported in XML Worker, and it probably never will.
However, this doesn't mean that you can't display HTML in columns.
If you go to the official XML Worker documentation, you'll find the ParseHtmlObjects where we parse a large HTML file and render it to a PDF with two columns: walden5.pdf
This is done by parsing the HTML into an ElementList
first:
// CSSCSSResolvercssResolver=
XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
// HTMLHtmlPipelineContexthtmlContext=newHtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.autoBookmark(false);
// PipelinesElementListelements=newElementList();
ElementHandlerPipelineend=newElementHandlerPipeline(elements, null);
HtmlPipelinehtml=newHtmlPipeline(htmlContext, end);
CssResolverPipelinecss=newCssResolverPipeline(cssResolver, html);
// XML WorkerXMLWorkerworker=newXMLWorker(css, true);
XMLParserp=newXMLParser(worker);
Once we have the list of Element
objects, we can add them to a ColumnText
object:
// step 1Documentdocument=newDocument(PageSize.LEGAL.rotate());
// step 2PdfWriterwriter= PdfWriter.getInstance(document, newFileOutputStream(file));
// step 3
document.open();
// step 4Rectangleleft=newRectangle(36, 36, 486, 586);
Rectangleright=newRectangle(522, 36, 972, 586);
ColumnTextcolumn=newColumnText(writer.getDirectContent());
column.setSimpleColumn(left);
booleanleftside=true;
intstatus= ColumnText.START_COLUMN;
for (Element e : elements) {
if (ColumnText.isAllowedElement(e)) {
column.addElement(e);
status = column.go();
while (ColumnText.hasMoreText(status)) {
if (leftside) {
leftside = false;
column.setSimpleColumn(right);
}
else {
document.newPage();
leftside = true;
column.setSimpleColumn(left);
}
status = column.go();
}
}
}
// step 5
document.close();
As you can see, you need to make some decisions here: you need to define the rectangles on the pages. You need to introduce new pages, etc...
Note: there is currently no C# port of this documentation. Please think of the Java code as if it were pseudo code.
Post a Comment for "Converting Html To A Multi-column Pdf"