how to apply font properties on <span> while passing html to pdf using itextsharp

杀马特。学长 韩版系。学妹 提交于 2019-12-22 00:33:13

问题


I am converting html to pdf using itextsharp and I want to set the font size for tags. How can I do this?

Currently I am using:

StyleSheet

styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.SPAN, HtmlTags.FONTSIZE, "9f");
string contents = File.ReadAllText(Server.MapPath("~/PDF TEMPLATES/DeliveryNote.html"));

List

parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), styles);

But it didn't work.


回答1:


The constants listed in HtmlTags are actually a hodgepodge of HTML tags and HTML and CSS properties and values and it can be a little tricky sometimes figuring out what to use.

In your case try HtmlTags.SIZE instead of HtmlTags.FONTSIZE and you should get what you want.

EDIT

I've never really seen a good tutorial on what properties do what, I usually just go directly to the source code. For instance, in the ElementFactory class there's a method called GetFont() that shows how font information is parsed. Specifically on line 130 (of revision 229) you'll see where the HtmlTags.SIZE is used. However, the actual value for the size is parsed in ChainedProperties in a method called AdjustFontSize(). If you look at it you'll see that it first looks for a value that ends with pt such as 12pt. If it finds that then it drops the pt and parses the number literally. If it doesn't end with pt it jumps over to HtmlUtilities to a method called GetIndexedFontSize(). This method is expecting either values like +1 and -1 for relative sizes or just integers like 2 for indexed sizes. Per the HTML spec user agents are supposed to accept values 1 through 7 for the font size and map those to a progressively increasing font size list. What this means is that your value of 9f is actually not a valid value to pass to this, you should probably be passing 9pt instead.

Anyway, you kind of half to jump around in the source to figure out what's being parsed where.



来源:https://stackoverflow.com/questions/8063991/how-to-apply-font-properties-on-span-while-passing-html-to-pdf-using-itextshar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!