问题描述
我正在尝试以PDF格式导出数据,现在我尝试使用itext sharp在PDF中添加图像我试试这个但是这显示错误..我尝试下面的代码但这显示错误当我在PDF文件中添加图像我成功添加文本
I am trying to export data in PDF and now i try to add image in PDF using itext sharp i try this but this shows an error ..Is try below code but this shows an error when i add image in PDF file i successfully add text
Private Sub ExportGridToPDF()
Dim headerText As String = "file"
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New iTextSharp.text.Document(iTextSharp.text.PageSize.A1, 10.0F, 10.0F, 10.0F, 0.0F)
Dim beginning As New iTextSharp.text.Chunk(headerText)
Dim p1 As New iTextSharp.text.Phrase(beginning)
Dim p2 As New iTextSharp.text.Phrase()
p2.Add(p1)
Dim p As New iTextSharp.text.Paragraph()
p.Add(p2)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
pdfDoc.Add(p)
Dim im As Image = iTextSharp.text.Image.GetInstance(imagepath + "/mikesdotnetting.tif")
pdfDoc.Add(im)
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
此部分
Dim im As Image = iTextSharp.text.Image.GetInstance(imagepath + "/mikesdotnetting.tif")
pdfDoc.Add(im)
这显示此行中的错误
Dim jpg As Image = iTextSharp.text.Image.GetInstance(New Uri(url))
错误
**Value of type 'iTextSharp.text.Image' cannot be converted to 'System.Web.UI.WebControls.Image'.**
任何解决方案?
推荐答案
您在同一代码中使用来自不同包/命名空间的两个名为 Image
的类。这是模棱两可的。你应该使用完全限定的名字。
You are using two classes named Image
from a different package / namespace in the same code. That is ambiguous. You should use fully qualified names.
我不是.Net开发人员,但如果这是Java,你会做这样的事情:
I'm not a .Net developer, but if this were Java, you'd do something like this:
Dim im As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath + "/mikesdotnetting.tif")
您的问题是由您创建变量 jpg
引起的类型 System.Web.UI.WebControls.Image
,但是您要分配类型为 iTextSharp.text.Image 到那个变量。很明显,这不起作用。
Your problem is caused by the fact that you create a variable
jpg
that is of type System.Web.UI.WebControls.Image
, but you are assigning an object of type iTextSharp.text.Image
to that variable. It is obvious that this doesn't work.
当你有两个同名的类(
Image
)时在两个不同的命名空间 System.Web.UI.WebControls
和 iTextSharp.text
中,你应该避免引入含糊之处。
When you have two classes with the same name (
Image
) in two different namespaces System.Web.UI.WebControls
and iTextSharp.text
, you should avoid introducing ambiguities.
阅读了解更多信息信息。
Read Resolving an ambiguous reference for more info.
这篇关于使用itextsharp添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!