2013-02-04 127 views
5

我需要從多個圖像創建一個PDF文件。例如,我有12張圖片,那麼pdf將生成3頁,其中包含4張圖片,一張頁面中的2張圖片。從多個圖像生成單個PDF

那麼,有沒有任何DLL,我可以用來從圖像生成PDF的樣本? enter image description here

+1

谷歌的iTextSharp的,它是非常有用的,可以用來創建PDF文件,並把圖片在那裏 – RhysW

+0

你有例如檢驗http://stackoverflow.com/questions/1273242/third-party-library-to-convert-image-into-pdf-and-eps-format-on-the-fly – mybrave

+0

Thx,我試過搜索,但不是確切的snipplet我有的代碼。 –

回答

0

謝謝,我已經使用表創建6張圖片在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 
4

。它有這種支持多個庫:

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

是的,它的好例子。如何在頁面上添加4個圖像。任何設置應用於代碼。 –

+0

你有一個iTextSharp鏈接的例子,我發給你如何添加多個圖像。如果您希望以不同方式顯示它們,只需將它們添加到表格中而不是段落中即可。我沒有嘗試,但它應該工作。或者,如果這還不夠,你可以在c#中合併它們。如何在C#中合併圖像:http://stackoverflow.com/questions/465172/merging-two-images-in-c-net –