Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create a PDF consisting of Multiple pages. The entire content need to be displayed in HTMLEditor so that it can be edited and saved as PDF.

I am using Visual Studio 2012 and C#.

The problem I am facing is HTMLEditor has browser compatability problem. The user will update the contents of HTML via HTMLEditor and will save it as PDF. Since the user updates the content I am not able to go for iTextSharp. Any solution will be helpful.

Regards,
Bala
Posted
Updated 10-Mar-14 2:07am
v2
Comments
Kornfeld Eliyahu Peter 10-Mar-14 7:38am    
Read about iTextSharp...
So, what is the issue?
rbala006 10-Mar-14 7:44am    
HTML Content has tables and styles, which varies in each browser. Total pages varies from 7 to 10. All the contents are dynamic.
Tejas Vaishnav 10-Mar-14 8:15am    
Read the HTML text after edited, from your HTML editor and pass that HTML text to itextsharp.

1 solution

C#
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);

      string imageFilePath = "C:\\inetpub\\wwwroot\\Practice\\Practice\\Images\\FLH_EventTicket_Background.jpg";

      iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

      //Resize image depend upon your need
      //For give the size to image
      jpg.ScaleToFit(3000, 770);

      //If you want to choose image as background then,
      jpg.Alignment = iTextSharp.text.Image.UNDERLYING;

      //If you want to give absolute/specified fix position to image.
      jpg.SetAbsolutePosition(7, 69);

      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)
   {
   }
}


code block corrected and tabulation reduced
 
Share this answer
 
v3
Comments
Nelek 10-Mar-14 8:21am    
When posting long code snippet, please check the answer once posted and correct the format if something is not showing correctly. I have corrected your code blocks and reduced a bit the tabulation to avoid broken lines as much as possible.

Thanks for helping :)
nandakishoreroyal 10-Mar-14 8:56am    
ok:)

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