Monday, July 25, 2011

Convert TIFF image to PDF


Here's the code: I have used this on Button click event. You can Call the below code as your need.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim doc As Document = New Document(PageSize.A4, 0, 0, 0, 0)

Dim fname As String = TextBox2.Text & "\" & "abc.pdf"

Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New System.IO.FileStream(fname, System.IO.FileMode.Create, System.IO.FileAccess.Write))

Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(TextBox1.Text)

Dim noofpages As Integer = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)

doc.Open()

Dim cb As PdfContentByte = writer.DirectContent

Dim page As Integer

For page = 0 To noofpages - 1

bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page)

Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()

bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif)

Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.ToArray())

stream.Close()

img.ScaleToFit(595.0F, 842.0F)

img.SetAbsolutePosition(0, 0)

cb.AddImage(img)

doc.NewPage()

Next

doc.Close()

End Sub




Hope this code will be usefull.


No comments:

Post a Comment