0

美好的一天,我刚刚开始使用 itextsharp 生成 pdf。当我尝试这样做时,我不断收到错误。但是,当我在渲染方法的覆盖中生成 pdf 时,它似乎可以工作..

这是按钮单击的代码:

protected void btnExport_Click(object sender, EventArgs e)
{
    string htmlMarkup = createPdf.GetHtmlPage(HttpContext.Current.Request.Url.AbsoluteUri);

    try
    {
        createPdf.GeneratePDF(htmlMarkup);
    }
    catch (Exception ex)
    {
        lblmessage.Text = ex.Message;
        throw;
    }
}

以及生成类中的代码:

    public void GeneratePDF(string pdfBody)
    {


        var document = new Document();

        // TODO : Write to Memory Stream
        var writer = PdfWriter.GetInstance(document, memStream);

        document.Open();
        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);

        //Why is this line not working??
        hw.Parse(new StringReader(pdfBody));

        document.Close();

         ShowPdf();
    }

    public string GetHtmlPage(string strURL)
    {

        String strResult;
        WebResponse objResponse;
        WebRequest objRequest = HttpWebRequest.Create(strURL);

        objRequest.UseDefaultCredentials = true;
        objRequest.PreAuthenticate = true;
        objRequest.Credentials = CredentialCache.DefaultCredentials;

        objResponse = objRequest.GetResponse();
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
        {
            strResult = sr.ReadToEnd();
            sr.Close();
        }
        return strResult;
    }

错误是在注释行生成的。错误是“找不到网络路径。\r\n”我该如何解决这个问题?!请帮助提前谢谢

4

0 回答 0