如何使用C#的.aspx转换成PDF? [英] How to convert .aspx to pdf using C#?

查看:182
本文介绍了如何使用C#的.aspx转换成PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

presently我需要转换的pdf.The第一页的.aspx页面包含images.I've用下面的code,但它给error.I've在我的项目添加iTextsharp.dll。

Presently I need to convert one .aspx page in pdf.The page contains images.I've used the following code,but it's giving error.I've added iTextsharp.dll in my project..

protected void btnConvertToPDF_Click(object sender, EventArgs e)
{
    Uri strurl = Request.Url;             
    string url = strurl.ToString();             
    string text = GetPageText(url);             
    string filepath = Server.MapPath("test.htm");
    StreamWriter writer = new StreamWriter(filepath);             
    writer.Write(text);             
    writer.Close();             
    htmltopdf(text);
}

public string GetPageText(string url)             
{
    string htmlText = string.Empty;             
    string FILE_NAME = Server.MapPath("test.xml"); //"c:\\test.xml";            
    try
    {
        HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);             
        requestIP.Timeout = 10000;
        using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())             
        {
            using (Stream streamIP = responseIP.GetResponseStream())             
            {
                using (StreamReader readerText = new StreamReader(streamIP))             
                {
                    htmlText = readerText.ReadToEnd();             
                    string text = htmlText;             
                    StreamWriter writer = new StreamWriter(FILE_NAME);             
                    writer.Write(text);             
                    writer.Close();             
                }
            }
        }
    }
    finally
    {
    }
    return htmlText;             
}

public void htmltopdf(string strHtml)
{
    Document doc = new Document();
    PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("test.pdf"), System.IO.FileMode.Create));
    HTMLParser.Parse(doc, Server.MapPath("test.htm"));
    if (File.Exists(Server.MapPath("test.htm")))        
        File.Delete(Server.MapPath("test.htm"));             
    if (File.Exists(Server.MapPath("test.xml")))             
        File.Delete(Server.MapPath("test.xml")); 
}

显示就行了HTMLparser.Parse为的HTMLParser不会在目前的情况下存在即使运行code。若之前我评论这行,然后运行code错误,它创建一个PDF文件与作为它不能open.It已损坏,不正确的版本等等等等。请谁能告诉我什么是错了吗?有没有用做任务开源的错误?我已经做通过购买任何成分编码不工作。

The error showing on the line HTMLparser.Parse as "HTMLPARSER does not exist in the current context" even before running the code.If I comment that line and run the code,it's creating one pdf file with error as "It can't be open.It has been damaged,not correct version etc etc".Please anyone can tell me what is wrong?Is there any open source to use to do the task?I've to do the job by coding not by buying any component..

推荐答案

您可以使用 WkHtmltoPdf 了解coverting页面为PDF。请参见这个帖子了解更多详情

You can use WkHtmltoPdf for coverting the page to pdf. See this post for more details

编辑:

使用此code到url直接转换为PDF。您需要将您的项目的bin文件夹

Use this code to convert the url directly to pdf. You need to place the wkhtmltopdf.exe in bin folder of your project

string url= @"http://www.google.com";

try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = Server.MapPath("~/bin/") + "wkhtmltopdf.exe";
process.StartInfo.Arguments = "\""+ url+ " " + Server.MapPath("~/PDFFiles/") + "test.pdf\"";

process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.WaitForExit();

}
catch (Exception ee)
            {
        //logging
            }

这篇关于如何使用C#的.aspx转换成PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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