将WebControl Image转换为iText.text.image

时间:2013-07-09 05:17:31

标签: asp.net vb.net pdf itextsharp tiff

这是我的代码

 Dim doc As iTextSharp.text.Document = New iTextSharp.text.Document()
 Dim path As String = Request.MapPath("Sample PDFs") & "\Sample.tif"
PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + _
                                 "render\1.pdf", FileMode.Create))

Dim data As clsData = New clsData(GetConnection, enumEgswFetchType.DataTable)
 Dim int As Integer = data.GetPageCount(path)
doc.Open()

For ctr As Integer = 0 To int - 1
 Dim img As iTextSharp.text.Image
Dim image As New System.Web.UI.WebControls.Image
image.ImageUrl = "ctrls/ViewTif.aspx?path=" + path.ToString + "&page=" + ctr.ToString

img = iTextSharp.text.Image.GetInstance(image, System.Web.UI.WebControls.Image)

doc.Add(image)
Next

doc.Close()
Response.Redirect("~/1.pdf")

有什么方法可以将webcontrol图像添加到itext中吗?图像变量是动态图像,源页面从tif文件返回jpeg图像。或者有什么方法可以直接将tif作为pdf直接放到itext中?

1 个答案:

答案 0 :(得分:0)

您是否需要使用System.Web.UI.WebControls.ImageiTextSharp.text.Image.GetInstance()的一个重载接受了一个网址:

Dim TestFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
Using FS As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None)
    Using Doc As New Document()
        Using Writer = PdfWriter.GetInstance(Doc, FS)
            Doc.Open()

            Doc.Add(New Paragraph("Hello World"))

            ''//Get the image from a URL
            Dim Img = iTextSharp.text.Image.GetInstance("http://www.google.com/images/srpr/logo4w.png")
            ''//Optionally adjust it
            Img.ScaleAbsolute(100, 200)
            ''//Add it to the document
            Doc.Add(Img)

            Doc.Close()
        End Using
    End Using
End Using