简体   繁体   中英

Export Html to PDF using ITextsharp

I have tried the code below, I am also facing an error. I am using latest DLL.

String strSelectUserListBuilder = @"<html><body>
                                <h1>My First Heading</h1>
                                <p>My first paragraph.</p>
                            </body>
                        </html>";

String htmlText = strSelectUserListBuilder.ToString();

List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

I got this error:

The given key was not present in the dictionary.

Try this:

Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw = 
             new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();

lets try below code it will help you convert HTML to PDF file.

String strSelectUserListBuilder = @"<html><body>
                                <h1>My First Heading</h1>
                                <p>My first paragraph.</p>
                            </body>
                        </html>";

String htmlText = strSelectUserListBuilder.ToString();
CreatePDFFromHTMLFile(htmlText , pdfFileName);


public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
 {
     try
     {
         object TargetFile = FileName;
         string ModifiedFileName = string.Empty;
         string FinalFileName = string.Empty;

         /* To add a Password to PDF -test */
         TestPDF.HtmlToPdfBuilder builder = new TestPDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
         TestPDF.HtmlPdfPage first = builder.AddPage();
         first.AppendHtml(HtmlStream);
         byte[] file = builder.RenderPdf();
         File.WriteAllBytes(TargetFile.ToString(), file);

         iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
         ModifiedFileName = TargetFile.ToString();
         ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

         string password = "password";
         iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, password, "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);ss
         reader.Close();
         if (File.Exists(TargetFile.ToString()))
             File.Delete(TargetFile.ToString());
         FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
         File.Copy(ModifiedFileName, FinalFileName);
         if (File.Exists(ModifiedFileName))
             File.Delete(ModifiedFileName);

     }
     catch (Exception ex)
     {
         throw ex;
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM