I am using XMLWorker to parse an HTML string into a PDF Document, and cannot find a way to control the line spacing of the PDF being generated.
Document document = new Document(PageSize.LETTER, 72f, 72f, 108f, 90f);
MemoryStream stream1 = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream1);
document.Open();
//parse HTML into document
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, new StringReader(summary.Content));
The "summary.Content" is an HTML string that comes from a database field.
Now, I recently upgraded our ITextSharp library to 5.5.5.0, and upgraded to the new XMLWorker library. Using the code above, the line spacing ("leading" in PDF-speak) is much smaller than the PDF's being generated previously. I am required to make sure the line spacing looks the same as it used to.
I read that I can set the leading on the Paragraphs I build, but that doesn't help me when simply calling ParseXHtml(). I read that ITextSharp defaults to a leading size of 1.5 times the font size.
I read here itextsupport documentation that I can use this line to use the default.css that ships with XML Worker.
CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
I thought the default CSS might generate the PDF with the same leading as my old PDF's, but the following code resulted in the same output PDF's as when I just use ParseXHtml().
var sr = new StringReader(summary.Content);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, pdfWriter)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParse = new XMLParser(true, worker);
document.Open();
xmlParse.Parse(sr);
I need to control the line height (line-spacing, leading) in the PDF document I generate. Can anyone help point me in the right direction? I am trying to work through some options. Do either of these make sense?
- Create a CSS file in a directory that defines line-height for different HTML tags, read that in with a stream, and pass that to
parseXHtml(PdfWriter writer, Document doc, InputStream in, InputStream inCssFile)
. - Define a font for my document that specifies a line-height? I looked but didn't find a way to do this.