【问题标题】:How to Change Default Font Size in iTextSharp After Exporting GridView to PDF?将 GridView 导出为 PDF 后如何更改 iTextSharp 中的默认字体大小?
【发布时间】:2011-09-15 01:03:48
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: asp.net pdf gridview export itextsharp


    【解决方案1】:

    确实有。

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

    Err... 我认为 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 的声明符合我的喜好,但我维护自己的分支......我很奇怪那个。

    【讨论】:

    • 不,它是 Java。 iTextSharp 源自 iText。您应该会发现它很容易翻译。
    【解决方案2】:

    C#

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

    【讨论】:

      【解决方案3】:
      BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
      iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont
      (iTextSharp.text.FontFactory.HELVETICA,20);
      

      【讨论】:

      • 奇怪的是您“无法完全理解”链接页面,但您已多次链接到该博客。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 2012-06-12
      • 2016-03-04
      相关资源
      最近更新 更多