的點擊一個PDF我有一個新的要求,在我的網頁中的按鈕,點擊後,它會顯示該網頁爲PDF。在研究中,我發現iTextSharp
用於此目的。 但我在我的網頁上也有很多Telerik
和ASP控件。我是否也可以使用iTextSharp
在PDF中獲得?顯示Web頁面的一個按鈕
如果是,那麼任何人都可以,請給我提供一個基礎環節,因爲我還沒有碰到過這個工具來又開始使用iTextSharp
。
據@ahaliav我用下面的代碼試圖答案。但是,我沒有掌握pdf。
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(HtmlContent);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
HTMLWorker worker = new HTMLWorker(document);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
// step 3: we create a worker parse the document
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
//Response.Write(document);
//Response.End();
}
protected override void Render(HtmlTextWriter writer)
{
// setup a TextWriter to capture the markup
TextWriter tw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
// render the markup into our surrogate TextWriter
base.Render(htw);
// get the captured markup as a string
string pageSource = tw.ToString();
// render the markup into the output stream verbatim
writer.Write(pageSource);
// remove the viewstate field from the captured markup
HtmlContent = Regex.Replace(pageSource,
"<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
"", RegexOptions.IgnoreCase);
// the page source, without the viewstate field, is in viewStateRemoved
// do what you like with it
}
請幫忙。 我還有其他的選擇,那就是當我點擊一個按鈕時顯示整個網頁的圖像。任何人都可以引導我對這兩個問題的任何可能的方向。
我終於改變了我的代碼:
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
MemoryStream msOutput = new MemoryStream();
有了這個,我能夠得到一個包含所有我的控制MSWORD文件,但我也得到了一些不必要的文本,我怎麼能刪除,如果有人遇到同樣的問題?
我使用許可[wkhtmltopdf(http://code.google.com/p/wkhtmltopdf/)我的一個項目是基於網頁生成PDF。 –
我可以得到telerik控件也在那個pdf –
不知道,但我的猜測是否定的。 –