C# 方法将HTML从数据库转换为带有iTextSharp的PDF中的可渲染格式

C# 方法将HTML从数据库转换为带有iTextSharp的PDF中的可渲染格式,c#,pdf,asp.net-web-api,itextsharp,xmlworker,C#,Pdf,Asp.net Web Api,Itextsharp,Xmlworker,我将上述代码与最新的itextsharp一起使用,以生成pdf列表。到目前为止,它工作得非常出色,直到我想将每个itextsharp列表从html转换为至少纯文本,或者最好是带图像的格式化文本(如果可能)。请有人帮助我将q.Message转换为pdf格式的纯文本,以便使用XMLWorker和此代码呈现为pdf文件 var paper = _repo.VeryLatestPaper().Result; List list = new List(List.ORDERED); paper.Quest

我将上述代码与最新的itextsharp一起使用,以生成pdf列表。到目前为止,它工作得非常出色,直到我想将每个
itextsharp
列表从html转换为至少纯文本,或者最好是带图像的格式化文本(如果可能)。请有人帮助我将
q.Message
转换为pdf格式的纯文本,以便使用
XMLWorker
和此代码呈现为pdf文件

var paper =  _repo.VeryLatestPaper().Result;
List list = new List(List.ORDERED);
paper.Questions.ForEach(q => list.Add(q.Message));

var doc1 = new Document(); 

string path = "B:\\Test\\PDF";
PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
writer.PageEvent = new PDFWriterEvents("This is a Test");


doc1.Open();

//new XMLParser().Parse(new StringReader(text));

//XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc1, new StringReader(text));

doc1.Add(list);

doc1.Close();

请注意,我不是从文件中获取html,而是从数据库中获取…

它能够将html文件转换为pdf

它已经回答了。我在这里做一些改变

转换所需的命名空间为:

//new XMLParser().Parse(new StringReader(text));

//XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc1, new StringReader(text));
以及转换和下载文件:

using iTextSharp.text;
using iTextSharp.text.pdf;

尝试将所有css内联

您可以使用其他类型的流,而不仅仅是文件流。例如,您可以在内存中加载HTML并传递MemoryStream,或者您也可以直接从数据库字段获取流表示。嗨,戈雷斯,让我来检查一下……既然你除了删除一点代码之外没有对代码做任何更改,那么这可能会更好,因为指向Ok@Chris Hass的注释现在就可以解决这些问题了。☺. 我只添加了一个pdf格式的响应标题。
//Create a byte array that will eventually hold our final PDF
            Byte[] bytes;

            //Boilerplate iTextSharp setup here
            //Create a stream that we can write to, in this case a MemoryStream
            using (var ms = new MemoryStream())
            {

                //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
                using (var doc = new Document())
                {

                    //Create a writer that's bound to our PDF abstraction and our stream
                    using (var writer = PdfWriter.GetInstance(doc, ms))

                    {

                        //Open the document for writing
                        doc.Open();


                        string finalHtml = string.Empty;
                       // read your html by database or from a html file here and store it into finalHtml e.g. a string



                        //XMLWorker also reads from a TextReader and not directly from a string
                        using (var srHtml = new StringReader(finalHtml))
                        {

                            //Parse the HTML
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        }



                        doc.Close();
                    }
                }

                //After all of the PDF "stuff" above is done and closed but **before** we
                //close the MemoryStream, grab all of the active bytes from the stream
                bytes = ms.ToArray();
            }





        //clear the response
            Response.Clear();
            MemoryStream mstream = new MemoryStream(bytes);
        //define response content type
            Response.ContentType = "application/pdf";
        //give the name of file of pdf and add in to header
            Response.AddHeader("content-disposition", "attachment;filename=invoice.pdf");
            Response.Buffer = true;
            mstream.WriteTo(Response.OutputStream);
            Response.End();