无法在使用itextsharp创建的pdf中获取图像

时间:2012-05-01 20:14:14

标签: asp.net pdf itextsharp

我没有用pdf获取图像我只得到文字,请帮助我, 我正在使用来自http://sourceforge.net/projects/itextsharp/

的iteshsharp 5.2.1

默认代码.aspx.cs

Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
savetopdf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

default.aspx的代码

<form id="form1" runat="server">
<div id="savetopdf" runat="server">
    <asp:Label ID="lbl" runat="server" Text=" Lorem Ipsum, Dolor"></asp:Label>
    <img src="http://localhost:5583/WebSite1/images/Penguins.jpg" alt="penguins" />
    <img src="http://localhost:5583/WebSite1/images/Tulips.jpg" alt="tulips" />
</div>
</form>

2 个答案:

答案 0 :(得分:1)

HTMLWorker是错误的,并已停止使用XMLWorker。

http://sourceforge.net/projects/itextsharp/files/xmlworker/

演示:

http://demo.itextsupport.com/xmlworker/

即使文档引用了Java API,对C#的适应也应该是直截了当的:

http://demo.itextsupport.com/xmlworker/itextdoc/index.html

答案 1 :(得分:0)

尝试这样:

string ImagePath = Server.MapPath("/Images/Printbanner1.jpg");
Document document = new Document();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=xxx.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
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);
htmlTable = htmlTable.ToString().Replace("'", "\"");
htmlTable = htmlTable.Replace("px", "");
StringReader sr = new StringReader(htmlTable.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ImagePath);
pdfDoc.Add(image);
htmlparser.Parse(sr);

pdfDoc.Close();