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

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

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

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

请注意,我不是从文件中获取 html,而是从数据库中获取...</p>

4

1 回答 1

-1

它具有将 HTML 文件转换为 pdf 的能力。

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

转换所需的命名空间是:

using iTextSharp.text;
using iTextSharp.text.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();

尝试将所有css内联

于 2015-05-31T08:27:38.213 回答