Asp.net iTextSharp代码有什么问题

Asp.net iTextSharp代码有什么问题,asp.net,pdf,itextsharp,Asp.net,Pdf,Itextsharp,我想将我的网格视图导出为PDF,但遇到了麻烦。我尝试了两种不同的方法,但都有错误。在第一种情况下,VS无法识别以下代码: Image PNG= Image.GetInstance(imagepath + "/test.PNG"); doc.Add(PNG); 我应该提到,我的PDF包含一些图片 // #1 protected void Button1_Click(object sender, EventArgs e) { string pdfpath = Server.MapPath(

我想将我的网格视图导出为PDF,但遇到了麻烦。我尝试了两种不同的方法,但都有错误。在第一种情况下,VS无法识别以下代码:

Image PNG= Image.GetInstance(imagepath + "/test.PNG");
doc.Add(PNG);
我应该提到,我的PDF包含一些图片

// #1
protected void Button1_Click(object sender, EventArgs e)
{
    string pdfpath = Server.MapPath("PDFs");
    string imagepath = Server.MapPath("Images");
    Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

    try
    {
        PdfWriter.GetInstance(doc, new FileStream(pdfpath + "Certificates/Dormaj-Certificates.pdf", FileMode.Create));
        doc.Open();
        doc.Add(new Paragraph("GIF"));

        Image gif = Image.GetInstance(imagepath + "/mikesdotnetting.gif");
        doc.Add(gif);
    }
    catch (Exception ex)
    {
        //Log error;
    }
    finally
    {
        doc.Close();
    }
}
然后我尝试了这个代码,它给了我关于图像的错误, 它说它找不到图像路径

// #2
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
var appDataPath = Server.MapPath("~/Certificates/");
if (!Directory.Exists(appDataPath))
{
    //Directory.Create(appDataPath);
}
var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".pdf");
using (var output = File.Create(filePath))
{
    // Do stuff here
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

我发现了问题!我已经纠正了第一条路! 这都是因为我的图像地址!因为我在网格视图中使用了图像,所以出现了这个问题!要解决这个问题,唯一需要做的就是给我们的图像提供一个直接地址。e、 g~/image/test.jpg应更改为http..yoursite/image/test.jpg。
就是这样

HTMLWorker已被放弃,取而代之的是[XML Worker][1]。请不要期待官方对这个问题的回答。[1] :能给我解释一下吗?呃。。。你所拥有的是Oops意味着出了问题。您需要的是查看生成的PDF:找到图像。这些示例中没有一个使用HTMLWorker类,因为它的作者不再支持它,因为我是作者之一。