create a pdf file from a number of jpg file on a folder

Wednesday 7 January 2015

create a pdf file from a number of jpg file on a folder

 create a pdf file from a number of jpg file on a folder

  • write the following code to create a pdf file 
 Public Function ConvertJpgoPdf(ByVal file_name As String, ByVal file_path As String) As String
        Dim err As String

        Try


            ' Create the document with a certain size and certain margins 
            Dim document As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 50, 50, 50)
            ' creation of output pdf file
            Dim writer As PdfWriter = PdfWriter.GetInstance(document, New System.IO.FileStream(file_path& "\" & file_name& ".pdf", System.IO.FileMode.Create))
            ' load the tiff image and count the total number of pages
            document.Open()
            Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(GetTempFolder("OutPut_img")) '''***** OutPut_img is the path where jpg file will ''''store with name Fil1.jpg,fil2.jpg......
            Dim count As Int32 = dir.GetFiles().Length
            For k As Int32 = 1 To count


                Dim cb As PdfContentByte = writer.DirectContent
                Dim filename As String = GetTempFolder("OutPut_img") & "\Fil" & k & ".jpg"
           
                Dim bm As Bitmap = New System.Drawing.Bitmap(filename)
                bm.SetResolution(50, 50)
                Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Jpeg)
''' for windows 7 write the following code
                 img.ScalePercent(70.0F / img.DpiX * 90, 70.0F / img.DpiY * 90)
                 img.SetAbsolutePosition(35, 70.0F)
               ''' for windows 8 write the following code
                  img.ScalePercent(70.0F / img.DpiX * 25, 70.0F / img.DpiY * 25)
                  img.SetAbsolutePosition(0, 0)
                  img.Border = iTextSharp.text.Rectangle.BOX
                 img.BorderColor = iTextSharp.text.BaseColor.GRAY
                 img.BorderWidth = 0.5F

                document.Add(img)
                document.NewPage()
                bm.Dispose()


            Next

            document.Close()
            ' document.Dispose()
            writer.Close()
            ' writer.Dispose()
            Dim s() As String = Directory.GetFiles(GetTempFolder("OutPut_img"))

            For l = 0 To s.Length - 1

                File.Delete(s(l))

            Next
            Directory.Delete(GetTempFolder("OutPut_img"))
            Return "01"
        Catch ex As Exception
            err = "Error From Client .Net Dll(Convert Multipage Tiff to Pdf)"
            Return err
            Throw ex
        End Try
    End Function

    Private Function GetTempFolder(ByVal folder_NM As String) As String
        Dim folder As String = Path.Combine(Path.GetTempPath, folder_NM)
        Directory.CreateDirectory(folder)

        If (Directory.Exists(folder)) Then
            Return folder
        End If

    End Function