保存页面时下载网页/ asp.net c# [英] downloading webpage while saving the page/asp.net c#

查看:61
本文介绍了保存页面时下载网页/ asp.net c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i开发了一个网页,允许用户像注册表一样输入值。保存数据我需要提供一个选项以pdf格式下载页面(我已输入值)。



我做了什么?

*我尝试使用itextsharp dll。问题是我没有得到pdf文件中的所有内容。示例图像和样式未正确转换。

*我尝试了一些第三方工具,如(SautinSoft的PdfVision,ConvertApi,pdfcrowd)程序集。所有的dll都将url作为参数并转换为pdf。以这种方式,服务器端的html代码转换为pdf。我输入的值不在pdf中。





示例代码我从网上获取(sautinsoft)。

Hi all,

i have developed a webpage which allow the user to enter the values just like a registration form.while saving the data i need to give an option for downloading the page as pdf format(which i have entered values).

what i did?
* i tried using itextsharp dll. the problem is i don''t get all the content in the pdf files. example images and styles are not converted properly.
* i tried some third party tools like( SautinSoft''s PdfVision,ConvertApi,pdfcrowd)assemblies. all the dll are getting the url as a parameter and converting to pdf. in that way the server side html code is converted into pdf.the values i have entered is not in the pdf.


sample code i got from net for (sautinsoft).

SautinSoft.PdfVision v = new SautinSoft.PdfVision();
       v. PageStyle.PageSize.Letter();
       byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://en.wikipedia.org/wiki/Main_Page");
      
       if (pdf != null)
       {
           Response.Buffer = true;
           Response.Clear();
           Response.ContentType = "application/PDF";
           Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf");
           Response.BinaryWrite(pdf);
           Response.Flush();
           Response.End();
}





for(itextsharp)

protected override void Render(HtmlTextWriter writer)

{

MemoryStream mem = new MemoryStream();

StreamWriter twr = new StreamWriter(mem);

HtmlTextWriter myWriter = new HtmlTextWriter(twr);

base.Render(myWriter);

myWriter.Flush();

myWriter.Dispose();

StreamReader strmRdr = new StreamReader(mem);

strmRdr.BaseStream.Position = 0;

string pageContent = strmRdr.ReadToEnd();

strmRdr.Dispose();

mem.Dispose();

writer.Write(pageContent);

CreatePDFDocument(pageContent);





}

public void CreatePDFDocument(string strHtml)

{



string strFileName = HttpContext.Current.Server.MapPath(te st.pdf);

//第1步:创建文档对象

文档文档=新文档();

/ /步骤2:

//我们创建一个侦听文档的编写器

PdfWriter.GetInstance(document,new FileStream(strFileName,FileMode.Create));

StringReader se = new StringReader(strHtml);

HTMLWorker obj = new HTMLWorker(document);

document.Open();

obj.Parse(se);

document.Close();

ShowPdf(strFileName);







}

public void ShowPdf(string strFileName)

{

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader(Content-Disposition,inline; filename =+ strFileName) ;

Response.ContentType =application / pdf;

Response.Write文件(strFileName);

Response.Flush();

Response.Clear();

}



注意:我已从多个论坛中获取代码。



如果你有更好的解决方案,请告诉我。



谢谢。

dev



for (itextsharp)
protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem = new MemoryStream();
StreamWriter twr = new StreamWriter(mem);
HtmlTextWriter myWriter = new HtmlTextWriter(twr);
base.Render(myWriter);
myWriter.Flush();
myWriter.Dispose();
StreamReader strmRdr = new StreamReader(mem);
strmRdr.BaseStream.Position = 0;
string pageContent = strmRdr.ReadToEnd();
strmRdr.Dispose();
mem.Dispose();
writer.Write(pageContent);
CreatePDFDocument(pageContent);


}
public void CreatePDFDocument(string strHtml)
{

string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
document.Close();
ShowPdf(strFileName);



}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}

note: i have taken code from several forums.

please let me know if u have better solution.

thanks.
dev

推荐答案

如果将输入的值保存到数据库中例如,尝试发送带有保存的id的查询字符串,然后只检查页面加载是否有查询字符串,如果有,则加载保存的数据并设置文本框值。这样,当itextsharp生成pdf时,页面将加载值。



只记得你这样做

If you save the values you have entered to a database then try sending a query string with the saved id for instance, then just check on the page load if there is a query string , if there is , load the saved data and set the textbox values. In that way the page will be loaded with the values when itextsharp generates the pdf.

just remember when you do this
byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://en.wikipedia.org/wiki/Main_Page");



就像打开一个新的浏览器窗口并重新加载页面一样。没有输入

这就是为什么你需要先写一个方法来加载值



希望它有帮助


its like opening up a new browser window and reloading the page. Nothing is entered
thats why you need to write a method to load the values first

Hope it helps


在转换为字节之前我们必须加载网页。这样就完成了技巧。





谢谢

Dev。
before converting to bytes we have to load the webpage.that done the tricks.


Thanks
Dev.


这篇关于保存页面时下载网页/ asp.net c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆