How to Change Default Font Size in iTextSharp After Exporting GridView to PDF?












5















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!










share|improve this question



























    5















    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!










    share|improve this question

























      5












      5








      5








      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!










      share|improve this question














      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!







      asp.net pdf gridview export itextsharp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 14 '11 at 20:42









      ncakmakncakmak

      1,72941517




      1,72941517
























          3 Answers
          3






          active

          oldest

          votes


















          0














          There is indeed.



          (but my initial suggestion isn't it... Font.DEFFAULTSIZE is "static final", so... Derp).



          Err... I don't think HTMLWorker's stylesheet code will let you set an overall default font size, thought I haven't worked with it all that much. I could be wrong. You can set it by class or tag, but that could be quite a bit of work... hey!



          I think you can set the style for "body" which will then affect everything within it. Unless you're working on HTML fragments, this should do the trick:



          StyleSheet bodySize = new StyleSheet();
          Map<String,String> props = new HashMap<String, String>();
          props.put(ElementTags.SIZE, "8");
          bodySize.applyStyle("body", props);

          htmlparser.setStyleSheet(bodySize);


          The way I did it was Change The Source (com.itextpdf.text.Font.java) so that the declaration of Font.DEFAULTSIZE was to my liking, but I maintain my own branch... I'm weird like that.






          share|improve this answer





















          • 1





            No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

            – Mark Storer
            Jun 16 '11 at 17:03



















          0














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





          share|improve this answer





















          • 1





            Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

            – Andrew Barber
            Sep 21 '12 at 18:36



















          -1














          C#



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





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6349847%2fhow-to-change-default-font-size-in-itextsharp-after-exporting-gridview-to-pdf%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            There is indeed.



            (but my initial suggestion isn't it... Font.DEFFAULTSIZE is "static final", so... Derp).



            Err... I don't think HTMLWorker's stylesheet code will let you set an overall default font size, thought I haven't worked with it all that much. I could be wrong. You can set it by class or tag, but that could be quite a bit of work... hey!



            I think you can set the style for "body" which will then affect everything within it. Unless you're working on HTML fragments, this should do the trick:



            StyleSheet bodySize = new StyleSheet();
            Map<String,String> props = new HashMap<String, String>();
            props.put(ElementTags.SIZE, "8");
            bodySize.applyStyle("body", props);

            htmlparser.setStyleSheet(bodySize);


            The way I did it was Change The Source (com.itextpdf.text.Font.java) so that the declaration of Font.DEFAULTSIZE was to my liking, but I maintain my own branch... I'm weird like that.






            share|improve this answer





















            • 1





              No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

              – Mark Storer
              Jun 16 '11 at 17:03
















            0














            There is indeed.



            (but my initial suggestion isn't it... Font.DEFFAULTSIZE is "static final", so... Derp).



            Err... I don't think HTMLWorker's stylesheet code will let you set an overall default font size, thought I haven't worked with it all that much. I could be wrong. You can set it by class or tag, but that could be quite a bit of work... hey!



            I think you can set the style for "body" which will then affect everything within it. Unless you're working on HTML fragments, this should do the trick:



            StyleSheet bodySize = new StyleSheet();
            Map<String,String> props = new HashMap<String, String>();
            props.put(ElementTags.SIZE, "8");
            bodySize.applyStyle("body", props);

            htmlparser.setStyleSheet(bodySize);


            The way I did it was Change The Source (com.itextpdf.text.Font.java) so that the declaration of Font.DEFAULTSIZE was to my liking, but I maintain my own branch... I'm weird like that.






            share|improve this answer





















            • 1





              No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

              – Mark Storer
              Jun 16 '11 at 17:03














            0












            0








            0







            There is indeed.



            (but my initial suggestion isn't it... Font.DEFFAULTSIZE is "static final", so... Derp).



            Err... I don't think HTMLWorker's stylesheet code will let you set an overall default font size, thought I haven't worked with it all that much. I could be wrong. You can set it by class or tag, but that could be quite a bit of work... hey!



            I think you can set the style for "body" which will then affect everything within it. Unless you're working on HTML fragments, this should do the trick:



            StyleSheet bodySize = new StyleSheet();
            Map<String,String> props = new HashMap<String, String>();
            props.put(ElementTags.SIZE, "8");
            bodySize.applyStyle("body", props);

            htmlparser.setStyleSheet(bodySize);


            The way I did it was Change The Source (com.itextpdf.text.Font.java) so that the declaration of Font.DEFAULTSIZE was to my liking, but I maintain my own branch... I'm weird like that.






            share|improve this answer















            There is indeed.



            (but my initial suggestion isn't it... Font.DEFFAULTSIZE is "static final", so... Derp).



            Err... I don't think HTMLWorker's stylesheet code will let you set an overall default font size, thought I haven't worked with it all that much. I could be wrong. You can set it by class or tag, but that could be quite a bit of work... hey!



            I think you can set the style for "body" which will then affect everything within it. Unless you're working on HTML fragments, this should do the trick:



            StyleSheet bodySize = new StyleSheet();
            Map<String,String> props = new HashMap<String, String>();
            props.put(ElementTags.SIZE, "8");
            bodySize.applyStyle("body", props);

            htmlparser.setStyleSheet(bodySize);


            The way I did it was Change The Source (com.itextpdf.text.Font.java) so that the declaration of Font.DEFAULTSIZE was to my liking, but I maintain my own branch... I'm weird like that.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 15 '11 at 16:27

























            answered Jun 15 '11 at 16:01









            Mark StorerMark Storer

            13.5k13064




            13.5k13064








            • 1





              No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

              – Mark Storer
              Jun 16 '11 at 17:03














            • 1





              No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

              – Mark Storer
              Jun 16 '11 at 17:03








            1




            1





            No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

            – Mark Storer
            Jun 16 '11 at 17:03





            No, it's Java. iTextSharp is derived from iText. You should find it easy to translate.

            – Mark Storer
            Jun 16 '11 at 17:03













            0














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





            share|improve this answer





















            • 1





              Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

              – Andrew Barber
              Sep 21 '12 at 18:36
















            0














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





            share|improve this answer





















            • 1





              Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

              – Andrew Barber
              Sep 21 '12 at 18:36














            0












            0








            0







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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 19 '12 at 12:10









            Andrew Barber

            33.6k1476107




            33.6k1476107










            answered Sep 9 '12 at 9:48









            user1657913user1657913

            51




            51








            • 1





              Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

              – Andrew Barber
              Sep 21 '12 at 18:36














            • 1





              Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

              – Andrew Barber
              Sep 21 '12 at 18:36








            1




            1





            Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

            – Andrew Barber
            Sep 21 '12 at 18:36





            Odd that you "can't understand fully" the linked page, yet you've linked to that blog multiple times.

            – Andrew Barber
            Sep 21 '12 at 18:36











            -1














            C#



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





            share|improve this answer






























              -1














              C#



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





              share|improve this answer




























                -1












                -1








                -1







                C#



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





                share|improve this answer















                C#



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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 6 '12 at 20:29









                j0k

                20.2k136775




                20.2k136775










                answered Nov 15 '11 at 18:50









                johnjohn

                1




                1






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6349847%2fhow-to-change-default-font-size-in-itextsharp-after-exporting-gridview-to-pdf%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python