Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Default Font Size in iTextSharp After Exporting GridView to 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!

like image 944
ncakmak Avatar asked Nov 26 '22 12:11

ncakmak


1 Answers

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont
(iTextSharp.text.FontFactory.HELVETICA,20);
like image 135
user1657913 Avatar answered Dec 04 '22 01:12

user1657913