2012-05-18 144 views
1

我嘗試使用itextsharp從html頁面視圖創建pdf文件。從asp.net頁面視圖創建pdf

此代碼塊的作品,但是,我從url獲取html代碼字符串,我想給這個字符串進行轉換。所以我可以通過提供url的功能將任何站點視圖轉換爲pdf。謝謝..

Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf"); 
Response.Cache.SetCacheability(HttpCacheability.NoCache); 
StringWriter sw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(sw); 

WebClient wc = new WebClient(); 
string htmlText = wc.DownloadString("http://"); 

this.Page.RenderControl(hw); 
StringReader sr = new StringReader(sw.ToString()); 
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); 
HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
pdfDoc.Open(); 
htmlparser.Parse(sr); 
pdfDoc.Close(); 
Response.Write(pdfDoc); 
Response.End(); 

回答