How to set line spacing when using XML Worker?

Category: 
Tags: XML WorkerleadingCSSspace between linesiText 5

I am using XML Worker to parse an HTML string into a PDF Document, and cannot find a way to control the line spacing of the PDF being generated.

Posted on StackOverflow on Mar 26, 2015 by Jon Z

If you want to have a different line-height for different paragraphs, you have to define a different value for the line-height attribute in your CSS. I have made a very simple example with some very simple inline CSS:

Some XML and the resulting PDF
Some XML and the resulting PDF

As you can see, the line-height of the paragraph starting with Non eram nescius is 16pt. As I use the default font which is 12 pt Helvetica. The paragraph looks fine.

For the paragraph that starts with Contra quos omnis, I use a line-height of 25pt and you see that there are big gaps between the lines.

For the paragraph that starts with Sive enim ad, I use a line-height of 13pt which is only 1 pt more than the font height. The lines are very close together for this paragraph.

It doesn't matter where you define the line-height. Your options are to define it inline in the tag, in the <head> section of your HTML or in an external CSS file that is either referenced from the header of your HTML or loaded into XML Worker separately. Whatever you like most is OK. We convert this HTML+CSS to PDF using the ParseHtml5 example.

Thanks Bruno, I ended up using an external CSS file that sets proper line-height, and loading that into XML Worker. Initially I had some trouble with consecutive paragraph tags all rendering on top of each other, but I think there was something improper with my HTML string, and not an issue with ITextSharp.