2013-02-04 14 views
5

Ho bisogno di creare un singolo file pdf da più immagini. Ad esempio, ho 12 immagini, quindi il pdf genererà 3 pagine con 4 immagini in una sola pagina 2 immagini in una riga.Genera un singolo PDF da più immagini

Quindi, c'è qualche DLL, esempio che posso utilizzare per generare il pdf dalle immagini? enter image description here

+1

Google per iTextSharp, la sua molto utile, può essere utilizzato per creare PDF e mettere le immagini in là – RhysW

+0

Hai controllato per esempio http://stackoverflow.com/questions/1273242/third-party-library-to-convert-image-into-pdf-and-eps-format-on-the-fly – mybrave

+0

Thx, ho provato a cercare, ma non lo snipplet esatto di codice che ho. –

risposta

0

Grazie, ho usato tavolo per creare 6 immagini su una pagina 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 
2

Dai uno sguardo al libro "iText in Action", questo più o meno copre anche iTextSharp, che è una versione .NET della libreria PDF iText. Cioè, il C# che devi scrivere è quasi identico agli esempi di codice Java.

È possibile scaricare i campioni da http://itextpdf.com/book/examples.php. Un esempio particolarmente interessante (codice in Java) è l'esempio su how to add an image. Gli esempi C# corrispondenti possono essere trovati su SourceForge.

Buona fortuna!

+0

Nota: itext è commerciale o AGPL – Ika

4

Non ci sono più librerie che hanno il supporto per questo:

  1. iTextSharp - working with images tutorial:
  2. pdfSharp-Working with images tutorial
  3. PDF Clown
+0

sì, è un buon esempio. Come posso aggiungere 4 immagini sulla pagina ... tutte le impostazioni da applicare sul codice. –

+0

Hai un esempio sul collegamento iTextSharp Ti ho inviato come aggiungere più immagini. Se vuoi che vengano visualizzati in modo diverso, aggiungili a un tavolo anziché a un paragrafo. Non l'ho provato, ma dovrebbe funzionare. Oppure, se ciò non bastasse, puoi unirli in C#. Come unire le immagini in C#: http://stackoverflow.com/questions/465172/merging-two-images-in-c-net –

Problemi correlati