将 GridView 导出为 PDF 后如何更改 iTextSharp 中的默认字体大小?

发布于 2024-11-15 10:54:04 字数 1252 浏览 1 评论 0 原文

我使用以下链接中的 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 的默认字体及其大小(至少是其大小)?

谢谢你!

I am using the iTextSharp method in the following link to export a GridView to a PDF document:

http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

The code is like this:

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();  
}

This works perfect except the font size in the PDF. I guess the defaults for iTextSharp are Arial and 12pt.

Is there any way to change this default font and its size (at least its size) globally for the whole PDF?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网白 2024-11-22 10:54:04
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont
(iTextSharp.text.FontFactory.HELVETICA,20);
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont
(iTextSharp.text.FontFactory.HELVETICA,20);
讽刺将军 2024-11-22 10:54:04

确实有。

(但我最初的建议不是...... Font.DEFFAULTSIZE 是“静态最终”,所以...... Derp)。

呃...我认为 HTMLWorker 的样式表代码不会让您设置整体默认字体大小,虽然我还没有使用它那么多。我可能是错的。您可以通过类或标签来设置它,但这可能是相当多的工作......嘿!

我认为你可以设置“body”的样式,这将影响其中的所有内容。除非您正在处理 HTML 片段,否则这应该可以解决问题:

StyleSheet bodySize = new StyleSheet();
Map<String,String> props = new HashMap<String, String>();
props.put(ElementTags.SIZE, "8"); 
bodySize.applyStyle("body", props);

htmlparser.setStyleSheet(bodySize);

我的方法是更改​​源 (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:

StyleSheet bodySize = new StyleSheet();
Map<String,String> props = new HashMap<String, String>();
props.put(ElementTags.SIZE, "8"); 
bodySize.applyStyle("body", props);

htmlparser.setStyleSheet(bodySize);

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.

永不分离 2024-11-22 10:54:04

C#

Hashtable props = new Hashtable();
props.Add(ElementTags.SIZE, "8");

C#

Hashtable props = new Hashtable();
props.Add(ElementTags.SIZE, "8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文