问题描述
我有一个应用程序,我在一个aspx页面中有一个链接按钮,打开另一个aspx页面,它基本上是一个报告但是预定义的措辞,除了一些数字值来自点击链接按钮的页面。
I have an application where I have a link button in one aspx page which opens another aspx page which is basically a report but predefined wordings, except some numeric values which is coming from the page where the link button is clicked.
string script = "detailedresults=window.open('processdocs/" + doc_file + "?id=" + id + "')";
ScriptManager.RegisterStartupScript(this, this.GetType(), "jsCall", script, true);
我需要像这样改变它。当我点击链接时,它应该打开aspx页面作为pdf报告,用户可以下载并保存为pdf到任何路径。意味着aspx页面应该基于id列写入,但必须是pdf
请告知
I need to change it like this. When i click on the link , it should open the aspx page as pdf report which user can download and save as pdf to any path. Means the aspx page should be writen based on the id column but it must be to pdf
please advise
推荐答案
protected void writetoPDF()
{
MyPage tmpPage = new MyPage();
HtmlForm form = new HtmlForm();
form.Controls.Add(form1);
tmpPage.Controls.Add(form);
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(htmlWriter);
string htmlContent = sw.ToString();
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
var fileName = HttpContext.Current.ApplicationInstance.Server.MapPath("");
fileName = fileName + @"\Report.pdf";
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add a paragraph to the document
//document.Add(new Paragraph(htmlContent.ToString()));
System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));
// HtmlParser.Parse(document, _xmlr);
using (TextReader sReader = new StringReader(htmlContent.ToString()))
{
List<IElement> list = HTMLWorker.ParseToList(sReader, new StyleSheet());
foreach (IElement elm in list)
{
document.Add(elm);
}
}
// step 5: we close the document
document.Close();
ShowPdf(fileName);
if (File.Exists(fileName))
File.Delete(fileName);
}
private void ShowPdf(string s)
{
string saveas = qms_control_nbr + ".pdf";
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + saveas);
Response.ContentType = "application/pdf";
Response.WriteFile(s);
Response.Flush();
Response.Clear();
}
这是有效的,但是为aspx页面定义的所有格式都没了。
任何人可以提出建议吗?
this is working but all the formating defined for the aspx page gone.
Can anyoone advice?
这篇关于asp页面到pdf报告链接按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!