Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ASP.NET网页转换为PDF_C#_.net_Asp.net_Itextsharp - Fatal编程技术网

C# 将ASP.NET网页转换为PDF

C# 将ASP.NET网页转换为PDF,c#,.net,asp.net,itextsharp,C#,.net,Asp.net,Itextsharp,我正在寻找使用iTextSharp将当前查看的asp.net网页导出为PDF文档的最简单方法-它可以是它的屏幕截图,也可以传递url以生成文档。样本代码将不胜感激。非常感谢 补充了达林在评论中所说的内容 您可以尝试使用生成PDF文件。它以URL作为输入。这就是我在SO应用程序中使用的内容。补充了Darin在评论中所说的内容 您可以尝试使用生成PDF文件。它以URL作为输入。这就是我在SO应用程序中使用的内容。这并不是那么容易(或者我认为是这样),我遇到了同样的问题,我必须编写代码以生成pdf格式

我正在寻找使用iTextSharp将当前查看的asp.net网页导出为PDF文档的最简单方法-它可以是它的屏幕截图,也可以传递url以生成文档。样本代码将不胜感激。非常感谢

补充了达林在评论中所说的内容


您可以尝试使用生成PDF文件。它以URL作为输入。这就是我在SO应用程序中使用的内容。

补充了Darin在评论中所说的内容

您可以尝试使用生成PDF文件。它以URL作为输入。这就是我在SO应用程序中使用的内容。

这并不是那么容易(或者我认为是这样),我遇到了同样的问题,我必须编写代码以生成pdf格式的精确页面。这取决于页面和使用的样式等,所以我创建每个元素的绘图

对于一些项目,我使用了Winnovative HTML到PDF转换器,但它不是免费的。

这不是那么容易(或者我认为是这样),我也遇到了同样的问题,我必须编写代码以生成PDF格式的确切页面。这取决于页面和使用的样式等,所以我创建每个元素的绘图

对于某些项目,我使用了Winnovative HTML到PDF转换器,但它不是免费的。

尝试一下PDFSharp(在运行时生成PDF文件),它是一个开源库:

替代解决方案:

尝试一下PDFSharp(在运行时生成PDF文件),它是一个开源库:


替代解决方案:

在过去的项目中,我们使用 做一些你需要做的事情,效果很好。你基本上给它一个url,它把HTML页面处理成PDF

不利的一面是,它是一个许可软件,成本与之相关,我们在同时导出大量大型PDF时遇到了一些性能问题


希望这有帮助

在过去的一个项目中,我们使用 做一些你需要做的事情,效果很好。你基本上给它一个url,它把HTML页面处理成PDF

不利的一面是,它是一个许可软件,成本与之相关,我们在同时导出大量大型PDF时遇到了一些性能问题

希望这有帮助

WInnovative网站上有一个例子就是这样做的。将当前查看的asp.net网页转换为PDF文档的相关C#代码为:

// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
    convertToPdf = true;
}

protected override void Render(HtmlTextWriter writer)
{
    if (convertToPdf)
    {
        // Get the current page HTML string by rendering into a TextWriter object
        TextWriter outTextWriter = new StringWriter();
        HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
        base.Render(outHtmlTextWriter);

        // Obtain the current page HTML string
        string currentPageHtmlString = outTextWriter.ToString();

        // Create a HTML to PDF converter object with default settings
        HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

        // Set license key received after purchase to use the converter in licensed mode
        // Leave it not set to use the converter in demo mode
        htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

        // Use the current page URL as base URL
        string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

        // Convert the current page HTML string a PDF document in a memory buffer
        byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    else
    {
        base.Render(writer);
    }
}
在WInnovative网站上有一个例子,就是这样做的。将当前查看的asp.net网页转换为PDF文档的相关C#代码为:

// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
    convertToPdf = true;
}

protected override void Render(HtmlTextWriter writer)
{
    if (convertToPdf)
    {
        // Get the current page HTML string by rendering into a TextWriter object
        TextWriter outTextWriter = new StringWriter();
        HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
        base.Render(outHtmlTextWriter);

        // Obtain the current page HTML string
        string currentPageHtmlString = outTextWriter.ToString();

        // Create a HTML to PDF converter object with default settings
        HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

        // Set license key received after purchase to use the converter in licensed mode
        // Leave it not set to use the converter in demo mode
        htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

        // Use the current page URL as base URL
        string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

        // Convert the current page HTML string a PDF document in a memory buffer
        byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    else
    {
        base.Render(writer);
    }
}

你为什么要用iTextSharp做这个?iTextSharp无法将ASP.NET页面转换为PDF,也无法将HTML转换为PDF。这不是它的目的。它是用来从头开始创建PDF的,你知道,你从一个新文档开始,添加文本、图像等等。。。因此,一种技术可能包括捕获页面的屏幕截图(再问一个关于如何执行此操作的问题,由于重复的数量太多,可能会关闭)然后使用iTextSharp生成PDF,其中包含表示页面渲染输出的.jpeg图像。为什么要使用iTextSharp执行此操作?iTextSharp无法将ASP.NET页面转换为PDF,也无法将HTML转换为PDF。这不是它的目的。它是用来从头开始创建PDF的,你知道,你从一个新文档开始,添加文本、图像等等。。。因此,一种技术可以包括捕获页面的屏幕截图(询问另一个如何操作的问题,该问题可能会因为重复的数量过多而关闭),然后使用iTextSharp生成PDF,其中包含表示页面渲染输出的.jpeg图像。