Set line spacing when using XMLWorker to parse HTM

2019-06-26 09:28发布

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.

1条回答
爷的心禁止访问
2楼-- · 2019-06-26 09:52

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:

enter image description here

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.

查看更多
登录 后发表回答