本文介绍了如何在< span>上应用字体属性使用itextsharp将html传递给pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用itextsharp将html转换为pdf,我想设置标签的字体大小。我该怎么做?

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

目前我正在使用:

StyleSheet

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

列表

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

但它不起作用。

推荐答案

HtmlTags 中列出的常量实际上是HTML标签和HTML和CSS属性和值的大杂烩,它可能有点棘手有时想弄清楚要使用什么。

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.SIZE 而不是 HtmlTags.FONTSIZE ,你应该得到你想要的。

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

编辑

我从来没有真正看过关于什么属性做什么的好教程,我通常只是直接去源代码。例如,在类有一个名为显示如何解析字体信息。具体在(修订版229)您将看到使用 HtmlTags.SIZE 的位置。但是,大小的实际值是在 .net / viewvc / itextsharp / trunk / src / core / iTextSharp / text / html / simpleparser / ChainedProperties.cs?revision = 285& view = markup#l136rel =nofollow> AdjustFontSize() 。如果你看一下,你会看到它首先查找一个以 pt 结尾的值,例如 12pt 。如果它发现然后它会丢弃 pt 并按字面解析数字。如果它不以 pt 结尾,它会跳转到到名为 GetIndexedFontSize() 。对于相对大小或者只有 +1 和 -1 之类的值。索引大小> 2 。根据,用户代理应该是接受值1到7的字体大小,并将它们映射到逐渐增加的字体大小列表。这意味着你的 9f 的值实际上不是传递给它的有效值,你应该传递 9pt 相反。

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.

这篇关于如何在< span>上应用字体属性使用itextsharp将html传递给pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:44