2015-05-29 26 views
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这个代码在这里。方法HTML从数据库转换为渲染的格式为PDF与iTextSharp的

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

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

请注意,我不是从文件中获取的HTML,它与数据库的...

+1

您可以使用其他类型的数据流的不仅仅是文件流。例如,您可以将内存中的HTML加载到MemoryStream中,也可以直接从数据库字段获取流表示。 [有关使用带有内存流的'PdfWriter.GetInstance'的相关问题](http://stackoverflow.com/questions/2815761/create-pdf-in-memory-instead-of-physical-file)。 – GolezTrol

+0

嗨Golez,让我把它弄出来...... – kabue7

回答

-1

它具有HTML文件转换成PDF格式,以能力。

它已经回答here。我在这里做一些chages。

所需的命名空间转换为:

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内嵌

+0

既然你没有做任何改变的代码,除了删除一点点,这可能会更好,因为评论指出[这是从原来的答案复制] (http://stackoverflow.com/a/25164258/231316) –

+0

好吧@Chris Hass,将照顾现在的事情。 ☺。我只是将该响应标题添加为pdf。 –