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

问题描述 投票:5回答:3

我在以下链接中使用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
3个回答
0
投票

的确有。

(但我最初的建议不是它...... 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的声明是我喜欢的,但我维护自己的分支......我很奇怪。


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

-1
投票

C#

Hashtable props = new Hashtable();
props.Add(ElementTags.SIZE, "8");
© www.soinside.com 2019 - 2024. All rights reserved.