简体   繁体   English

如何在<span>使用itextsharp将html传递给pdf时</span>应用字体属性

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

I am converting html to pdf using itextsharp and I want to set the font size for tags. 我正在使用itextsharp将html转换为pdf,我想设置标签的字体大小。 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. 但它没有用。

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. HtmlTags中列出的常量实际上是HTML标签和HTML和CSS属性和值的大杂烩,有时候弄清楚要使用什么可能有点棘手。

In your case try HtmlTags.SIZE instead of HtmlTags.FONTSIZE and you should get what you want. 在你的情况下尝试HtmlTags.SIZE而不是HtmlTags.FONTSIZE ,你应该得到你想要的。

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. 例如,在ElementFactory类中有一个名为GetFont()的方法,它显示了如何解析字体信息。 Specifically on line 130 (of revision 229) you'll see where the HtmlTags.SIZE is used. 特别是在第130行 (修订版229)上,您将看到使用HtmlTags.SIZE位置。 However, the actual value for the size is parsed in ChainedProperties in a method called AdjustFontSize() . 但是,在ChainedProperties在名为AdjustFontSize()的方法中解析大小的实际值。 If you look at it you'll see that it first looks for a value that ends with pt such as 12pt . 如果你看它,你会发现它首先查找以pt结尾的值,例如12pt If it finds that then it drops the pt and parses the number literally. 如果它发现然后它丢弃了pt并按字面解析数字。 If it doesn't end with pt it jumps over to HtmlUtilities to a method called GetIndexedFontSize() . 如果它没有以pt结尾,它跳转到HtmlUtilities到一个名为GetIndexedFontSize()的方法。 This method is expecting either values like +1 and -1 for relative sizes or just integers like 2 for indexed sizes. 对于相对大小,此方法期望值为+1-1 ,或者对于索引大小,此方法只需要像2这样的整数。 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. 根据HTML规范,用户代理应该接受字体大小的值1到7,并将它们映射到逐渐增加的字体大小列表。 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. 这意味着你的9f值实际上不是传递给它的有效值,你可能应该通过9pt而不是。

Anyway, you kind of half to jump around in the source to figure out what's being parsed where. 无论如何,你有一半在源头跳转来找出解析的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM