I am generating PDF using iTextSharp.dll, but the problem is that I am not able to apply that CSS. I have one div:
我正在使用iTextSharp生成PDF。dll,但问题是我不能应用那个CSS。我有一个div:
<div id="personal" class="headerdiv">
Personal Data
</div>
now my .aspx.cs code is like this:
现在我的. aspx。cs代码是这样的:
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle("#headerdiv", "height", "30px");
styles.LoadTagStyle("#headerdiv", "font-weight", "bold");
styles.LoadTagStyle("#headerdiv", "font-family", "Cambria");
styles.LoadTagStyle("#headerdiv", "font-size", "20px");
styles.LoadTagStyle("#headerdiv", "background-color", "Blue");
styles.LoadTagStyle("#headerdiv", "color", "White");
styles.LoadTagStyle("#headerdiv", "padding-left", "5px");
HTMLWorker worker = new HTMLWorker(document);
worker.SetStyleSheet(styles);
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
3 个解决方案
#1
12
There's a couple of things going on here. First and foremost, the HTML/CSS parser in iText and iTextSharp are far from complete. They're definitely very powerful but still have a ways to go. Each version gets better so always make sure that you're using the latest version.
这里发生了一些事情。首先,iText和iTextSharp中的HTML/CSS解析器还远远没有完成。他们确实很强大,但还有很长的路要走。每个版本都变得更好,所以一定要确保您使用的是最新版本。
Second, I've seen more HTML/CSS activity in an add-on for iText/iTextSharp called XMLWorker that you might want to look at. You don't "load styles" anymore, you just pass raw HTML/CSS in and it figures out a lot of things. You can see some examples here, see a list of supported CSS attributes here, download it here (and get the two missing files here and here).
其次,我已经在iText/iTextSharp的一个附加组件中看到了更多的HTML/CSS活动,您可能希望看到它。你不再“加载样式”了,你只需要传递原始的HTML/CSS,它就能计算出很多东西。您可以在这里看到一些示例,在这里查看支持的CSS属性列表,在这里下载(并在这里和这里获取两个丢失的文件)。
Third, LoadTagStyle
is for loading style attributes for HTML tags, not CSS IDs or Classes. You want to use LoadStyle
to load by class:
第三,LoadTagStyle是为HTML标签加载样式属性,而不是CSS id或类。您希望使用LoadStyle按类加载:
styles.LoadStyle("<classname>", "<attribute>", "<value>");
Unfortunately this method still doesn't do what you want it to do always. For instance, to change the font size you'd think you'd say:
不幸的是,这个方法仍然不能做你想要它一直做的事情。例如,要改变字体大小,你会认为你会说:
styles.LoadStyle("headerdiv", "font-size", "60ptx);
But to get it to work you can only use relative HTML font sizes (1,2,-1, etc) or PT sizes and you must use the size
property:
但是要让它工作,你只能使用相对HTML字体大小(1,2,-1,等等)或PT大小,你必须使用大小属性:
styles.LoadStyle("headerdiv", "size", "60pt");
//or
styles.LoadStyle("headerdiv", "size", "2");
The LoadStyle
honestly feels like an afterthought that was only partially completed and I recommend not using it actually. Instead I recommend writing the style attributes directly inline if you can:
真实的LoadStyle感觉就像一个事后的想法,只是部分完成了,我建议不要使用它。相反,如果可以的话,我建议直接使用内联方式编写样式属性:
string html = "<div id=\"personal\" class=\"headerdiv\" style=\"padding-left:50px;font-size:60pt;font-family:Cambria;font-weight:700;\">Personal Data</div>";
Obviously this defeats the points of CSS and once again, that's why they're working on the new XMLWorker above.
显然,这会破坏CSS的要点,这也是为什么他们要在上面使用新的XMLWorker。
Lastly, to use fonts by name you have to register them with iTextSharp first, it won't go looking for them:
最后,要使用字体,你必须先用iTextSharp注册它们,它不会去找它们:
iTextSharp.text.FontFactory.Register(@"c:\windows\fonts\cambria.ttc", "Cambria");
#2
1
In case someone is still having issues with this. The latest version of itextsharp (currently 5.3.2) significantly improves the HTMLWorker processor.
如果有人仍然有问题。最新版本的itextsharp(目前5.3.2)显著提高了HTMLWorker处理器。
you can get it here: http://sourceforge.net/projects/itextsharp/
您可以在这里找到:http://sourceforge.net/projects/itextsharp/。
#3
1
The correct way to reference the backgroud color is through the HtmlTags class
引用backgroud颜色的正确方法是通过HtmlTags类。
styles.LoadTagStyle(HtmlTags.HEADERCELL, HtmlTags.BACKGROUNDCOLOR, "Blue");
#1
12
There's a couple of things going on here. First and foremost, the HTML/CSS parser in iText and iTextSharp are far from complete. They're definitely very powerful but still have a ways to go. Each version gets better so always make sure that you're using the latest version.
这里发生了一些事情。首先,iText和iTextSharp中的HTML/CSS解析器还远远没有完成。他们确实很强大,但还有很长的路要走。每个版本都变得更好,所以一定要确保您使用的是最新版本。
Second, I've seen more HTML/CSS activity in an add-on for iText/iTextSharp called XMLWorker that you might want to look at. You don't "load styles" anymore, you just pass raw HTML/CSS in and it figures out a lot of things. You can see some examples here, see a list of supported CSS attributes here, download it here (and get the two missing files here and here).
其次,我已经在iText/iTextSharp的一个附加组件中看到了更多的HTML/CSS活动,您可能希望看到它。你不再“加载样式”了,你只需要传递原始的HTML/CSS,它就能计算出很多东西。您可以在这里看到一些示例,在这里查看支持的CSS属性列表,在这里下载(并在这里和这里获取两个丢失的文件)。
Third, LoadTagStyle
is for loading style attributes for HTML tags, not CSS IDs or Classes. You want to use LoadStyle
to load by class:
第三,LoadTagStyle是为HTML标签加载样式属性,而不是CSS id或类。您希望使用LoadStyle按类加载:
styles.LoadStyle("<classname>", "<attribute>", "<value>");
Unfortunately this method still doesn't do what you want it to do always. For instance, to change the font size you'd think you'd say:
不幸的是,这个方法仍然不能做你想要它一直做的事情。例如,要改变字体大小,你会认为你会说:
styles.LoadStyle("headerdiv", "font-size", "60ptx);
But to get it to work you can only use relative HTML font sizes (1,2,-1, etc) or PT sizes and you must use the size
property:
但是要让它工作,你只能使用相对HTML字体大小(1,2,-1,等等)或PT大小,你必须使用大小属性:
styles.LoadStyle("headerdiv", "size", "60pt");
//or
styles.LoadStyle("headerdiv", "size", "2");
The LoadStyle
honestly feels like an afterthought that was only partially completed and I recommend not using it actually. Instead I recommend writing the style attributes directly inline if you can:
真实的LoadStyle感觉就像一个事后的想法,只是部分完成了,我建议不要使用它。相反,如果可以的话,我建议直接使用内联方式编写样式属性:
string html = "<div id=\"personal\" class=\"headerdiv\" style=\"padding-left:50px;font-size:60pt;font-family:Cambria;font-weight:700;\">Personal Data</div>";
Obviously this defeats the points of CSS and once again, that's why they're working on the new XMLWorker above.
显然,这会破坏CSS的要点,这也是为什么他们要在上面使用新的XMLWorker。
Lastly, to use fonts by name you have to register them with iTextSharp first, it won't go looking for them:
最后,要使用字体,你必须先用iTextSharp注册它们,它不会去找它们:
iTextSharp.text.FontFactory.Register(@"c:\windows\fonts\cambria.ttc", "Cambria");
#2
1
In case someone is still having issues with this. The latest version of itextsharp (currently 5.3.2) significantly improves the HTMLWorker processor.
如果有人仍然有问题。最新版本的itextsharp(目前5.3.2)显著提高了HTMLWorker处理器。
you can get it here: http://sourceforge.net/projects/itextsharp/
您可以在这里找到:http://sourceforge.net/projects/itextsharp/。
#3
1
The correct way to reference the backgroud color is through the HtmlTags class
引用backgroud颜色的正确方法是通过HtmlTags类。
styles.LoadTagStyle(HtmlTags.HEADERCELL, HtmlTags.BACKGROUNDCOLOR, "Blue");