vb.net 制作 pdf 时无法让 CSS 在 iTextSharp (5.4.3) 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18853309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot get CSS to work in iTextSharp (5.4.3) when making pdf
提问by kolin
I have a problem trying to apply a css file to my pdf using the iTextSharp (5.4.3) generation library. basically the css is not being applied at all.
我在尝试使用 iTextSharp (5.4.3) 生成库将 css 文件应用于我的 pdf 时遇到问题。基本上css根本没有被应用。
I have the following method in my vb.net file
我的 vb.net 文件中有以下方法
Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPreview.Click
Dim bytes As Byte()
bytes = System.Text.Encoding.UTF8.GetBytes(letterRadEdit.Content)
Dim tagProcessor As tool.xml.html.DefaultTagProcessorFactory()
Using input As New MemoryStream(bytes, False)
Dim ms As New MemoryStream()
Dim document As New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 36, 36, 36, 36)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, ms)
writer.CloseStream = False
document.Open()
Dim htmlContext As HtmlPipelineContext = New HtmlPipelineContext(Nothing)
htmlContext.SetAcceptUnknown(True)
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory())
Dim cssResolver As ICSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(False)
cssResolver.AddCssFile(HttpContext.Current.Server.MapPath("/assets/css/pdf.css"), True)
Dim pipeline As New CssResolverPipeline(cssResolver, New HtmlPipeline(htmlContext, New PdfWriterPipeline(document, writer)))
Dim pdfworker As New XMLWorker(pipeline, True)
Dim p As New XMLParser(True, pdfworker, New System.Text.UTF8Encoding)
Try
'p.AddListener(pdfworker)
'p.Parse(input, Encoding.UTF8)
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, input, New FileStream(HttpContext.Current.Server.MapPath("~/assets/css/pdf.css"), FileMode.Open, FileAccess.Read))
Catch
Finally
pdfworker.Close()
End Try
document.Close()
ms.Position = 0
Response.Buffer = True
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=preview.pdf")
Response.BinaryWrite(ms.GetBuffer())
Response.Flush()
End Using
End Sub
the CSS file simply contains :
CSS 文件只包含:
p{color:#e10000;margin-bottom:1.2em;}
(This is to test whether it's rendering correctly, all text should be red)
(这是为了测试它是否渲染正确,所有文字都应该是红色的)
My problem isthat the following command
我的问题是以下命令
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, input, New FileStream(HttpContext.Current.Server.MapPath("~/assets/css/pdf.css"), FileMode.Open, FileAccess.Read))
correctly produces the pdf, but doesn't apply the CSS to it. I know it's reading the CSS because I had a permissions exception until I applied the FileAccess.Read property
正确生成 pdf,但不对它应用 CSS。我知道它正在读取 CSS,因为在我应用 FileAccess.Read 属性之前我有一个权限异常
the method
方法
p.Parse(input, Encoding.UTF8)
doesn't produce any pdf, just an 'Element not allowed' exception, this is because the html (coming from a radeditor text box Q3 2013) is old html and the parse seems to have a problem with tables.
不生成任何 pdf,只是一个“不允许元素”的异常,这是因为 html(来自 radeditor 文本框 Q3 2013)是旧的 html,解析似乎有表格问题。
采纳答案by kolin
Well it would appear that the CSS was correctly being applied as I tested a
好吧,当我测试时,似乎正确应用了 CSS
td{
border:1px solid red;
padding:0.4em;
margin:0;
}
to the pdf, and all the cells got bordered in red, so it would appear that the pdf overrides certain styles. Not sure why.
到 pdf,并且所有单元格都以红色为边框,因此看起来 pdf 覆盖了某些样式。不知道为什么。
回答by Altaf Patel
iTextSharp is very poor with designs using css, images etc. Instead wkhtmltopdfis the best.
iTextSharp 在使用 css、图像等的设计方面很差。相反,wkhtmltopdf是最好的。

