5

I need to create single pdf file from multiple images. For example, I have 12 images then pdf will generate 3 pages with consist of 4 image in single page 2 images in a row.

So, is there any dll, sample I can use to generate pdf from images?enter image description here

k-s
  • 2,192
  • 11
  • 39
  • 73
  • 1
    google for iTextSharp, its very useful, can be used to create pdf's and put images in there –  Feb 04 '13 at 12:16
  • Have you checked e.g. http://stackoverflow.com/questions/1273242/third-party-library-to-convert-image-into-pdf-and-eps-format-on-the-fly – mybrave Feb 04 '13 at 12:25

3 Answers3

5

There are multiple libraries that have support for this:

  1. iTextSharp - working with images tutorial:
  2. pdfSharp - Working with images tutorial
  3. PDF Clown
Kevin R.
  • 3,571
  • 1
  • 19
  • 29
Petrutiu Mihai
  • 565
  • 4
  • 14
  • yeah, its good example. How can I add 4 images on page..any settings to apply on code. – k-s Feb 04 '13 at 12:59
  • You have an example on iTextSharp link I sent you how to add multiple images. If you want them to be displayed differently, just add them on a table instead of paragraph. I didn't try it, but it should work. Or if that is not enough you can merge them in c#. How to merge images in c#: http://stackoverflow.com/questions/465172/merging-two-images-in-c-net – Petrutiu Mihai Feb 04 '13 at 13:13
3

Thanks, I have used table to create 6 images on one page in pdf.

Public Function CreatePDF(images As System.Collections.Generic.List(Of Byte())) As String
        Dim PDFGeneratePath = Server.MapPath("../images/pdfimages/")
        Dim FileName = "attachmentpdf-" & DateTime.Now.Ticks & ".pdf"

        If images.Count >= 1 Then
            Dim document As New Document(PageSize.LETTER)
            Try
                ' Create pdfimages directory in images folder.
                If (Not Directory.Exists(PDFGeneratePath)) Then
                    Directory.CreateDirectory(PDFGeneratePath)
                End If

                ' we create a writer that listens to the document
                ' and directs a PDF-stream to a file
                PdfWriter.GetInstance(document, New FileStream(PDFGeneratePath & FileName, FileMode.Create))

                ' opens up the document
                document.Open()
                ' Add metadata to the document.  This information is visible when viewing the

                ' Set images in table
                Dim imageTable As New PdfPTable(2)
                imageTable.DefaultCell.Border = Rectangle.NO_BORDER
                imageTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER

                For ImageIndex As Integer = 0 To images.Count - 1
                    If (images(ImageIndex) IsNot Nothing) AndAlso (images(ImageIndex).Length > 0) Then
                        Dim pic As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(SRS.Utility.Utils.ByteArrayToImage(images(ImageIndex)), System.Drawing.Imaging.ImageFormat.Jpeg)

                        ' Setting image resolution
                        If pic.Height > pic.Width Then
                            Dim percentage As Single = 0.0F
                            percentage = 400 / pic.Height
                            pic.ScalePercent(percentage * 100)
                        Else
                            Dim percentage As Single = 0.0F
                            percentage = 240 / pic.Width
                            pic.ScalePercent(percentage * 100)
                        End If

                        pic.Border = iTextSharp.text.Rectangle.BOX
                        pic.BorderColor = iTextSharp.text.BaseColor.BLACK
                        pic.BorderWidth = 3.0F

                        imageTable.AddCell(pic)
                    End If
                    If ((ImageIndex + 1) Mod 6 = 0) Then
                        document.Add(imageTable)
                        document.NewPage()

                        imageTable = New PdfPTable(2)
                        imageTable.DefaultCell.Border = Rectangle.NO_BORDER
                        imageTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER
                    End If
                    If (ImageIndex = (images.Count - 1)) Then
                        imageTable.AddCell(String.Empty)
                        document.Add(imageTable)
                        document.NewPage()
                    End If
                Next
            Catch ex As Exception
                Throw ex
            Finally
                ' Close the document object
                ' Clean up
                document.Close()
                document = Nothing
            End Try
        End If

        Return PDFGeneratePath & FileName
    End Function
k-s
  • 2,192
  • 11
  • 39
  • 73
2

Have a look at the book "iText in Action", this more or less also covers iTextSharp, which is a .NET version of the iText PDF library. That is, the C# you must write is almost identical to the Java code samples.

You can download the samples from http://itextpdf.com/book/examples.php. A particularly interesting example (code in Java) is the sample on how to add an image. The corresponding C# examples can be found on SourceForge.

Good luck!

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76