How to change default font size in iTextSharp after exporting GridView to PDF?

I use the iTextSharp method in the following link to export a GridView to a PDF:

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

The code is as follows:

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 fine except for the font size in PDF. I assume that the default values ​​for iTextSharp are Arial and 12pt.

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

Thank!

+5
source share
3 answers

Really.

( ... Font.DEFFAULTSIZE " ", ... Derp).

Err... , HTMLWorker , , . . , ... !

, "", , . 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);
0

#

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

All Articles