Click here to Skip to main content
14,386,319 members
Rate this:
Please Sign up or sign in to vote.
See more:
I exporting div tag data into pdf format in asp.net using C# but the default font size never changes..
Plz give me solution for this asap.

my aspx code is..
protected void btnPdf_Click(object sender, EventArgs e)
   {
       Response.ContentType = "application/pdf";
       Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);
       Tbldata.RenderControl(hw);
       StringReader sr = new StringReader(sw.ToString());
       Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
       HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
       PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
       pdfDoc.Open();
       htmlparser.Parse(sr);
       pdfDoc.Close();
       Response.Write(pdfDoc);
       Response.End();
   }
Posted
Updated 25-May-19 5:06am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

try placing this

BaseFont bfChronicleFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\ChronicleDisp-Black.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
               Font ChronicleFont = new Font(bfChronicleFont, 16f);
               BaseFont bfBrandonFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\Brandon_reg.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
               Font BrandonFont = new Font(bfBrandonFont, 14f, Font.NORMAL);
   
Comments
Member 10513529 12-Mar-14 7:28am
   
Bt how to assign font for the document
Member 10513529 12-Mar-14 7:29am
   
I am getting error C:\inetpub\wwwroot\Practice\Practice\Fonts\ChronicleDisp-Black.otf not found as file or resource.
nandakishoreroyal 12-Mar-14 7:31am
   
that is the location of font,
Member 10513529 12-Mar-14 7:32am
   
Actually I want to change the font size for my pdfdocument ie pdfdoc in my code
nandakishoreroyal 12-Mar-14 7:32am
   
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Member 10513529 12-Mar-14 7:54am
   
Still not solve my problem..
In my .aspx file have code

<div id="Tbldata" runat="server" ></div>

and i want to converti this Tbldata into pdf at runtime & want to change font-size for this pdfdocument
nandakishoreroyal 12-Mar-14 8:05am
   
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font ChronicleFont = new Font(bfTimes , 16f);


16f means 16px, u can increase your font size.
Rate this:
Please Sign up or sign in to vote.

Solution 2

private void HTMLtoPDF()
      {
          try
          {

              string sFilePath = "C:/Users/NandaKishore.K/Desktop/";
              FileStream fs = File.Create(sFilePath + "SamplePDF.pdf");

              Document document = new Document();

              BaseFont bfR = iTextSharp.text.pdf.BaseFont.CreateFont(BaseFont.TIMES_ROMAN, iTextSharp.text.pdf.BaseFont.CP1257, iTextSharp.text.pdf.BaseFont.EMBEDDED);
              PdfWriter writer = PdfWriter.GetInstance(document, fs);



              PdfWriter.GetInstance(document, Response.OutputStream);

              document.Open();

              document.Add(jpg);

              //iTextSharp.text.Font HeaderOne = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16f, iTextSharp.text.Font.BOLD);
              //Font Chronicle = FontFactory.GetFont("Chronicle Display Black");
              //Chronicle.Size = 16;
              //Chronicle.IsBold();
              BaseFont bfChronicleFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\ChronicleDisp-Black.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
              Font ChronicleFont = new Font(bfChronicleFont, 16f);
              BaseFont bfBrandonFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\Brandon_reg.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
              Font BrandonFont = new Font(bfBrandonFont, 14f, Font.NORMAL);


              PdfPTable table1 = new PdfPTable(1);
              PdfPCell pdfCell;

              pdfCell = new PdfPCell(new Phrase("BELLEVUE SQUARE", ChronicleFont));
              pdfCell.Border = 0;
              pdfCell.PaddingTop = 40;
              pdfCell.PaddingLeft = 100;
              table1.AddCell(pdfCell);

              pdfCell = new PdfPCell(new Phrase("Use our west entrance on two.", BrandonFont));
              pdfCell.Border = 0;
              pdfCell.PaddingTop = 10;
              pdfCell.PaddingLeft = 100;
              table1.AddCell(pdfCell);

              pdfCell = new PdfPCell(new Phrase("Thursday, November 29, 7:30 p.m.", BrandonFont));
              pdfCell.Border = 0;
              pdfCell.PaddingLeft = 100;
              table1.AddCell(pdfCell);

              pdfCell = new PdfPCell(new Phrase(string.Empty));
              pdfCell.Border = 0;
              pdfCell.FixedHeight = 60;
              table1.AddCell(pdfCell);

              document.Add(table1);

              PdfPTable table2 = new PdfPTable(5);
              PdfPCell pdfCell2;
              pdfCell2 = new PdfPCell(new Phrase("Name:", BrandonFont));
              pdfCell2.PaddingLeft = 100;
              pdfCell2.Border = 0;
              pdfCell2.Colspan = 3;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase("Name", BrandonFont));
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase(string.Empty));
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase("Total Number of Guests:", BrandonFont));
              pdfCell2.PaddingLeft = 110;
              pdfCell2.PaddingTop = 15;
              pdfCell2.Colspan = 3;
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase("Name", BrandonFont));
              pdfCell2.PaddingTop = 15;
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase(string.Empty));
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase("Confirmation Number:", BrandonFont));
              pdfCell2.PaddingTop = 15;
              pdfCell2.Colspan = 3;
              pdfCell2.PaddingLeft = 100;
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase("Name", BrandonFont));
              pdfCell2.PaddingTop = 15;
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase(string.Empty));
              pdfCell2.Border = 0;
              table2.AddCell(pdfCell2);

              pdfCell2 = new PdfPCell(new Phrase(string.Empty));
              pdfCell2.Colspan = 5;
              pdfCell2.Border = 0;
              pdfCell2.FixedHeight = 80;
              table2.AddCell(pdfCell2);

              document.Add(table2);

              document.Close();
              fs.Close();

              Response.AddHeader("Content-Disposition", "attachment;filename=SamplePDF.pdf");
              Response.ContentType = "application/pdf";
              Response.TransmitFile("C:\\Users\\NandaKishore.K\\Desktop\\SamplePDF.pdf");
              Response.End();

          }

          catch (Exception ex)
          {

          }
      }
   
Comments
Member 10513529 12-Mar-14 8:22am
   
protected void btnPdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font ChronicleFont = new Font(bfTimes, 26f);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Tbldata.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}

here is my code i have done changes bt not get any difference in output
nandakishoreroyal 13-Mar-14 0:39am
   
remove Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

and place Document pdfDoc = new Document();
Rate this:
Please Sign up or sign in to vote.

Solution 3

if (!Directory.Exists("C:\\Downloads\\GSOvertimeReport"))
           {
               Directory.CreateDirectory("C:\\Downloads\\GSOvertimeReport");
           }


               filename = filename.Replace('/', '-');

           DataTable dataTable = SelectDT(str);
           var pdfDocument = new Document();
           var pdfFile = string.Format("{0}{1}.pdf", "C:\\Downloads\\GSOvertimeReport\\", filename);

           if (File.Exists(pdfFile))
           {
               File.Delete(pdfFile);
           }

           var pdfWriter =PdfWriter.GetInstance(pdfDocument,new FileStream(pdfFile,FileMode.Create));
           pdfDocument.Open();
           BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

           Font Mtimes = new Font(bfTimes, 14, Font.BOLD, BaseColor.BLACK);
           DataTable dtComp = new DataTable();
           dtComp = SelectDT("select * from GSIT_CompanyMaster");
           Paragraph head = new Paragraph(dtComp.Rows[0][1].ToString()+" - Time Sheet ",Mtimes);
           head.Alignment = Element.ALIGN_CENTER;
           pdfDocument.Add(head);
           pdfDocument.Add(new Paragraph("-------------------------------------------------------------------------------------------------------------------------------- "));
           pdfDocument.Add(new Paragraph());

           var pdfTable = new PdfPTable(dataTable.Columns.Count);
           pdfTable.WidthPercentage = 100;


           Font Htimes = new Font(bfTimes, 10, Font.BOLD, BaseColor.WHITE);
           for (int k = 0; k < dataTable.Columns.Count; k++)
           {
               var pdfCell = new PdfPCell(new Phrase(dataTable.Columns[k].ColumnName,Htimes));
               pdfCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
               pdfCell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
               pdfCell.BackgroundColor = new BaseColor(51,102,102);
               pdfTable.AddCell(pdfCell);
           }

           Font times = new Font(bfTimes, 7, Font.NORMAL, BaseColor.BLACK);

           for (int row = 0; row < dataTable.Rows.Count; row++)
           {

               for (int col = 0; col < dataTable.Columns.Count; col++)
               {

                   var cell = new PdfPCell(new Phrase (dataTable.Rows[row][col].ToString(),times));
                   cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                   cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
                   pdfTable.AddCell(cell);
               }
           }

           pdfDocument.Add(pdfTable);
           pdfDocument.Close();
       }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100