将 GridView 导出为 PDF 后如何更改 iTextSharp 中的默认字体大小?
我使用以下链接中的 iTextSharp 方法将 GridView 导出到 PDF 文档:
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
代码是这样的:
protected void btnExportPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
除了 PDF 中的字体大小之外,这一切都很完美。我猜 iTextSharp 的默认字体是 Arial 和 12pt。
有什么方法可以全局更改整个 PDF 的默认字体及其大小(至少是其大小)?
谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确实有。
(但我最初的建议不是...... Font.DEFFAULTSIZE 是“静态最终”,所以...... Derp)。
呃...我认为 HTMLWorker 的样式表代码不会让您设置整体默认字体大小,虽然我还没有使用它那么多。我可能是错的。您可以通过类或标签来设置它,但这可能是相当多的工作......嘿!
我认为你可以设置“body”的样式,这将影响其中的所有内容。除非您正在处理 HTML 片段,否则这应该可以解决问题:
我的方法是更改源 (com.itextpdf.text.Font.java),以便声明
Font.DEFAULTSIZE
是符合我的喜好,但我维护自己的分支......我就是这样奇怪。There is indeed.
(but my initial suggestion isn't it... Font.DEFFAULTSIZE is "static final", so... Derp).
Err... I don't think HTMLWorker's stylesheet code will let you set an overall default font size, thought I haven't worked with it all that much. I could be wrong. You can set it by class or tag, but that could be quite a bit of work... hey!
I think you can set the style for "body" which will then affect everything within it. Unless you're working on HTML fragments, this should do the trick:
The way I did it was Change The Source (com.itextpdf.text.Font.java) so that the declaration of
Font.DEFAULTSIZE
was to my liking, but I maintain my own branch... I'm weird like that.C#
C#