嗨,我想通過使用asp.net應用程序獲取當前頁面源代碼。我發現了一段代碼將HTML轉換爲PDF,但爲了將我的頁面轉換爲PDF,我需要獲取頁面的HTML代碼。我怎樣才能得到這些字符串?我簡單的代碼是這樣的:如何從asp.net獲取當前頁面源代碼頁面
string sPathToWritePdfTo = Server.MapPath("") + "/pdf_dosya_adi.pdf";
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
sbHtml.Append("<html>");
sbHtml.Append("<body>");
sbHtml.Append("<font size='14'>HTML den PDF çevirme Test</font>");
sbHtml.Append("<br />");
sbHtml.Append("Body kısmında yazacak yazı");
sbHtml.Append("</body>");
sbHtml.Append("</html>");
using (System.IO.Stream stream = new System.IO.FileStream
(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
{
Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
htmlToPdf.Open(stream);
htmlToPdf.Run(sbHtml.ToString());
htmlToPdf.Close();
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "friendlypdfname.pdf"));
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.WriteFile(sPathToWritePdfTo);
HttpContext.Current.Response.End();
如果我能得到HTML代碼關閉我的asp.net頁面,我把我的網頁上的所有行成 sbHtml.Append(「」); 代碼通過使用for循環,這將解決我的問題在我看來。
這可以爲mycurrent頁面嗎?這是工作本地主機 – EmreAltun 2012-03-12 13:43:15
@EmreAltun,如果你想使用WebClient你需要指定網頁的完整地址。 – 2012-03-12 13:45:05
我使用這段代碼,但它總是再次調用我的當前頁面,並導致循環。 – EmreAltun 2012-03-12 13:57:00