C# 使用 ITextsharp 将 Html 导出为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18378458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:05:23  来源:igfitidea点击:

Export Html to PDF using ITextsharp

c#asp.net.netitexthtml-to-pdf

提问by Qaisar Shabbir Awan

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

我已经尝试了下面的代码,我也遇到了错误。我正在使用最新的 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.

字典中不存在给定的键。

回答by MSTdev

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

让我们试试下面的代码,它将帮助您将 HTML 转换为 PDF 文件。

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;
     }
 }

回答by Kapil Khandelwal

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();