HTML编辑器ASP.NET中的HTML到PDF [英] HTML to PDF from HTML Editor ASP.NET

查看:61
本文介绍了HTML编辑器ASP.NET中的HTML到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个由多个页面组成的PDF。整个内容需要在HTMLEditor中显示,以便可以编辑并保存为PDF格式。



我使用的是Visual Studio 2012和C#。



我面临的问题是HTMLEditor存在浏览器兼容性问题。用户将通过HTMLEditor更新HTML的内容,并将其保存为PDF。由于用户更新了内容,我无法使用iTextSharp。任何解决方案都会有所帮助。



问候,

Bala

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

推荐答案

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


这篇关于HTML编辑器ASP.NET中的HTML到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆