2010-08-02 18 views
0

我需要建立從哪個是從編輯存儲數據庫提供的HTML中的PDF文件創建從存儲在數據庫中,從一串HTML PDF ...如何使用iTextSharp的

我必須拿出相應的HTML與在PDF中的所有樣式......請幫助

我使用iTextSharp的方法,但我沒有得到我時,我將其轉換爲PDF格式編輯器提供了正確的內容,我用

代碼

string content = "<the html content from database>"; 

    StringReader reader = new StringReader(content); 
    MemoryStream ms = new MemoryStream(); 
    Document doc = new Document(PageSize.A4,50,50,30,30); 
    HTMLWorker parser = new HTMLWorker(doc); 
    PdfWriter.GetInstance(doc, ms); 
    doc.Open(); 
    try 
    { 
     parser.Parse(reader); 
    } 
    catch(Exception ex) 
    { 
     Paragraph paragraph = new Paragraph("Error! " + ex.Message); 
     paragraph.SetAlignment("center"); 
     Chunk text = paragraph.Chunks[0] as Chunk; 
     doc.Add(paragraph); 
    } 
    finally 
    { 
     doc.Close(); 
    } 
    Byte[] buffer = ms.GetBuffer(); 
    if (buffer != null) 
    { 
     Response.ContentType = "application/pdf"; 
     Response.AddHeader("content-length", buffer.Length.ToString()); 
     Response.BinaryWrite(buffer); 
    } 

有什麼不對的地方,請幫忙用代碼從html創建pdf

回答

0

這是爲我工作的代碼。

 using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using iTextSharp.text; 
    using iTextSharp.text.pdf; 
    using System.IO; 
    using System.Collections; 
    using System.Text; 
    using iTextSharp.text.xml; 
    using iTextSharp.text.html; 

    public partial class Default2 : System.Web.UI.Page 
    { 
      protected void Page_Load(object sender, EventArgs e) 
      { 
      //create document 
      Response.Write(Server.MapPath(".")); 
      Document document = new Document(); 
      try 
      { 
       //writer - have our own path!!! 
       PdfWriter.GetInstance(document, new FileStream(Server.MapPath(".") +   
        "parsetest.pdf", FileMode.Create)); 
     document.Open(); 
     //html -text - kan be from database or editor too 
     String htmlText = "<font " + 
     " color=\"#0000FF\"><b><i>Title One</i></b></font><font " + 
     " color=\"black\"><br><br>Some text here<br><br><br><font " + 
     " color=\"#0000FF\"><b><i>Another title here " + 
     " </i></b></font><font " + 
     " color=\"black\"><br><br>Text1<br>Text2<br><OL><LI>hi</LI><LI>how are u</LI> 
     </OL>"; 
     //make an arraylist ....with STRINGREADER since its no IO reading file... 
     List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null); 

     //add the collection to the document 
     for (int k = 0; k < htmlarraylist.Count; k++) 
     { 
      document.Add((IElement)htmlarraylist[k]); 
     } 
     document.Add(new Paragraph("And the same with indentation....")); 
     // or add the collection to an paragraph 
     // if you add it to an existing non emtpy paragraph it will insert it from 
     //the point youwrite - 
     Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder" 
     mypara.IndentationLeft = 36; 
     mypara.InsertRange(0, htmlarraylist); 
     document.Add(mypara); 
     document.Close(); 

    } 
    catch (Exception exx) 
    { 
     Console.Error.WriteLine(exx.StackTrace); 
     Console.Error.WriteLine(exx.Message); 
    } 
} 
} 
+0

即時得到例外,在這條線...名單 非泛型類型「iTextSharp.text.List」不能與類型參數 列表 htmlarraylist = iTextSharp.text.html.simpleparser使用.HTMLWorker.ParseToList(new StringReader(htmlText),null); – deepu 2010-08-02 07:52:45

+0

當我用數組代替列表 我得到異常 無法隱式轉換類型「system.collection,generic.list到system.collections.arraylist – deepu 2010-08-02 07:54:26

+0

我糾正這些問題,但如果出現在HTML的任何樣式,然後伊茨沒有進入pdf – deepu 2010-08-02 10:42:03