这是我为hiqpdf下载的C#,但是我不确定如何修改它,以便它可以与我的html一起使用?我的asp.net c#表单中的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();

}

最佳答案

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();

}

09-20 07:36