【问题标题】:hiqpdf - asp.net - how to amend code to capture divhiqpdf - asp.net - 如何修改代码以捕获 div
【发布时间】:2017-02-26 10:40:00
【问题描述】:

这是我为 hiqpdf 下载的 C#,但我不确定如何修改它以使其适用于我的 html?我的 asp.net c# sheet for textBoxUrl 出现错误,但我不确定我应该使用什么命名空间来获取这个或是否需要替换这个文本?

C#代码:

using HiQPdf;

protected void Print_Button_Click(object sender, EventArgs e)
{

    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    // select the HTML element to be converted to PDF
    htmlToPdfConverter.ConvertedHtmlElementSelector =
                                    textBoxConvertedHtmlElementSelector.Text;

    // convert URL to a PDF memory buffer
    string url = textBoxUrl.Text;

    byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);

    // inform the browser about the binary data format
    HttpContext.Current.Response.AddHeader("Content-Type",application/pdf");

    // let the browser know how to open the PDF document
    HttpContext.Current.Response.AddHeader("Content-Disposition",
                String.Format("attachment; filename=ConvertHtmlPart.pdf;

                        size ={ 0}
    ",
        pdfBuffer.Length.ToString()));

    // write the PDF buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(pdfBuffer);

    // call End() method of HTTP response 
    // to stop ASP.NET page processing
     HttpContext.Current.Response.End();

}

【问题讨论】:

    标签: c# html css asp.net hiqpdf


    【解决方案1】:

    textBoxUrl 是一个TextBox 控件。您应该将其替换为您的源 URL。

    例如对于带有“#page”选择器的 bbc 网站。

    using HiQPdf;
    
    protected void Print_Button_Click(object sender, EventArgs e)
    {
    
    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
    
    // select the HTML element to be converted to PDF
    htmlToPdfConverter.ConvertedHtmlElementSelector = "#page"                               
    
    // convert URL to a PDF memory buffer
    string url = "http://www.bbc.com/";
    
    byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
    
    // inform the browser about the binary data format
    HttpContext.Current.Response.AddHeader("Content-Type",application/pdf");
    
    // let the browser know how to open the PDF document
    HttpContext.Current.Response.AddHeader("Content-Disposition",
                String.Format("attachment; filename=ConvertHtmlPart.pdf;
    
                        size ={ 0}
    ",
        pdfBuffer.Length.ToString()));
    
    // write the PDF buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(pdfBuffer);
    
    // call End() method of HTTP response 
    // to stop ASP.NET page processing
     HttpContext.Current.Response.End();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-09
      • 2020-08-29
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2014-01-26
      相关资源
      最近更新 更多