Click here to Skip to main content
15,884,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..
C#
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 4:06am
v3

try placing this

C#
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);
 
Share this answer
 
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);
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)
          {

          }
      }
 
Share this answer
 
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();
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();
       }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900