views:

46

answers:

0

Hi All,

I have created a PNG image that is 200 DPI, and perfectly sized for a landscape A4 page size. I needed to convert this to a PDF document, so I've used the iTextSharp library with the code below.

This all works, however the image quality has degraded. Any suggestions as to how I might improve this?

Public Sub ConvertPNGtoPDF(ByVal inputFile As String, ByVal outputFile As String)
        Using fs As New FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
            Dim document As New Document(PageSize.A4.Rotate, 0, 0, 0, 0)

            Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)

            document.Open()

            Dim cb As PdfContentByte = writer.DirectContent

            Using bm As New Bitmap(inputFile)
                Dim total As Integer = bm.GetFrameCount(FrameDimension.Page)

                For k As Integer = 0 To total - 1
                    bm.SelectActiveFrame(FrameDimension.Page, k)
                    Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bm, Nothing, False)

                    img.SetDpi(200, 200)
                    img.ScalePercent(72.0F / 200.0F * 100)
                    img.SetAbsolutePosition(0, 0)

                    cb.AddImage(img)
                    document.NewPage()
                Next
            End Using

            document.Close()
            writer.Close()
        End Using

    End Sub