不应用CSS同时使用iTextsharp.dll生成PDF [英] Not applying the CSS while generating PDF using iTextsharp.dll
问题描述
我生成使用iTextSharp.dll PDF,但问题是,我不能够以应用CSS。我有一个DIV:
< DIV ID =个人级=headerdiv>
个人数据
< / DIV>
现在我.aspx.cs code是这样的:
iTextSharp.text.html.simpleparser.StyleSheet风格=新iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle(#headerdiv,高度,30像素);
styles.LoadTagStyle(#headerdiv,字体重量,大胆);
styles.LoadTagStyle(#headerdiv,字体家庭,坎布里亚);
styles.LoadTagStyle(#headerdiv,字体大小,20像素);
styles.LoadTagStyle(#headerdiv,背景色,蓝);
styles.LoadTagStyle(#headerdiv,色,白);
styles.LoadTagStyle(#headerdiv,填充左,为5px);
HTMLWorker工人=新HTMLWorker(文件);
worker.SetStyleSheet(样式);
//第4步:我们打开的文件,并开始对文档的工人
document.Open();
worker.StartDocument();
//第5步:分析HTML到文档
worker.Parse(读卡器);
//第6步:关闭文档和工人
worker.EndDocument();
worker.Close();
document.Close();
有两件事情会在这里的。首先,在iText的和iTextSharp的的HTML / CSS语法分析器还远远没有完成。他们绝对是非常强大的,但仍然有很长的路要走。每个版本都变得更好,所以始终确保您使用的是最新版本。
在附加组件的iText / iTextSharp的二,我看到更多的HTML / CSS的活动称为XMLWorker,你可能想看看。你不载入样式了,你只要通过原始的HTML / CSS的,它计算出了很多东西。你可以看到一些例子此处,看到的supported~~V CSS属性这里,的下载这里(并获得两个丢失的文件<一个href="http://itextsharp.svn.sourceforge.net/viewvc/itextsharp/trunk/src/extras/itextsharp.xmlworker/iTextSharp/errors/errors.properties?revision=286&content-type=text/plain">here和<一href="http://itextsharp.svn.sourceforge.net/viewvc/itextsharp/trunk/src/extras/itextsharp.xmlworker/iTextSharp/errors/errors_nl.properties?revision=286&content-type=text/plain">here).
三, LoadTagStyle
是HTML标记,而不是CSS ID或类加载样式属性。你想用 LoadStyle
通过类来加载:
styles.LoadStyle(&LT;类名&gt;中,&LT;属性&gt;中,&LT;价值&GT;);
不幸的是这种方法仍不能做你想要它做的始终是什么。例如,改变字体大小,你会觉得你会说:
styles.LoadStyle(headerdiv,字体大小,60ptx);
但是,为了得到它的工作,你只能使用相对HTML字体大小(1,2,-1,等)或PT的大小和您必须使用尺寸
属性
styles.LoadStyle(headerdiv,大小,60pt);
//要么
styles.LoadStyle(headerdiv,大小,2);
在 LoadStyle
诚实的感觉就像是只部分完成事后的想法,我建议不要使用它实际。相反,我建议写的样式属性直接,如果你可以内联
字符串的HTML =&LT; DIV ID = \个人\级= \headerdiv \风格= \填充左:50px的;字体大小:60pt;字体 - 家庭:坎布里亚;字体重量:700; \&GT;个人资料和LT; / DIV&gt;中;
这显然违背了CSS的点,再一次,这就是为什么他们正在开发新的XMLWorker以上。
最后,通过名称使用字体,你必须先用iTextSharp的登录时,不会去寻找他们:
iTextSharp.text.FontFactory.Register(@C:\ WINDOWS \字体\ cambria.ttc,坎布里亚);
I am generating PDF using iTextSharp.dll, but the problem is that I am not able to apply that CSS. I have one div:
<div id="personal" class="headerdiv">
Personal Data
</div>
now my .aspx.cs code is like this:
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();
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.
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).
Third, LoadTagStyle
is for loading style attributes for HTML tags, not CSS IDs or Classes. You want to use LoadStyle
to load by class:
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:
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:
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.
Lastly, to use fonts by name you have to register them with iTextSharp first, it won't go looking for them:
iTextSharp.text.FontFactory.Register(@"c:\windows\fonts\cambria.ttc", "Cambria");
这篇关于不应用CSS同时使用iTextsharp.dll生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!