如何更改iTextSharp的默认字体大小导出GridView控件PDF后? [英] How to Change Default Font Size in iTextSharp After Exporting GridView to PDF?

查看:230
本文介绍了如何更改iTextSharp的默认字体大小导出GridView控件PDF后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的链接iTextSharp的方法到GridView导出为PDF文档:

<一个href=\"http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx\">http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

在code是这样的:

 保护无效btnExportPDF_Click(对象发件人,EventArgs的发送)
{
    Response.ContentType =应用程序/ PDF
    Response.AddHeader(内容处置,附件;文件名= GridViewExport.pdf);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);    StringWriter的SW =新的StringWriter();
    HtmlTextWriter的HW =新的HtmlTextWriter(SW);    GridView1.AllowPaging = FALSE;
    GridView1.DataBind();
    GridView1.RenderControl(HW);    StringReader SR =新StringReader(sw.ToString());
    文档pdfDoc =新的文档(PageSize.A4,10F,10F,10F,0F);
    HTMLWorker的HTMLParser =新HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc,Response.OutputStream);    pdfDoc.Open();
    htmlparser.Parse(SR);
    pdfDoc.Close();    的Response.Write(pdfDoc);
    到Response.End();
}

这完美的作品除了在PDF的字体大小。我猜测iTextSharp的默认设置是Arial和12磅。

有什么办法来全局更改此默认字体和它的大小(至少它的大小)整个PDF?

感谢您!


解决方案

  BASEFONT bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252,FALSE);
iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont
(iTextSharp.text.FontFactory.HELVETICA,20);

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!

解决方案

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont
(iTextSharp.text.FontFactory.HELVETICA,20);

这篇关于如何更改iTextSharp的默认字体大小导出GridView控件PDF后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆