2013-02-04 16 views
5

Tôi cần tạo một tệp pdf từ nhiều hình ảnh. Ví dụ, tôi có 12 hình ảnh sau đó pdf sẽ tạo ra 3 trang với 4 hình ảnh trong một trang 2 hình ảnh liên tiếp.Tạo một PDF từ nhiều hình ảnh

Vì vậy, có bất kỳ dll, mẫu nào tôi có thể sử dụng để tạo pdf từ hình ảnh không? enter image description here

+1

google for iTextSharp, rất hữu ích, có thể được sử dụng để tạo pdf và đặt hình ảnh trong đó – RhysW

+0

Bạn đã kiểm tra ví dụ: http://stackoverflow.com/questions/1273242/third-party-library-to-convert-image-into-pdf-and-eps-format-on-the-fly – mybrave

+0

Thx, tôi đã thử tìm kiếm nhưng không phải là đoạn mã chính xác mã tôi có. –

Trả lời

0

Cảm ơn, tôi đã sử dụng bảng để tạo ra 6 hình ảnh trên một trang bằng 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

Hãy xem sách "iText in Action", điều này ít nhiều cũng bao gồm iTextSharp, phiên bản .NET của thư viện PDF iText. Nghĩa là, C# bạn phải viết gần giống với các mẫu mã Java.

Bạn có thể tải xuống các mẫu từ http://itextpdf.com/book/examples.php. Một ví dụ đặc biệt thú vị (mã trong Java) là mẫu trên how to add an image. Các ví dụ C# tương ứng có thể được tìm thấy trên SourceForge.

Chúc may mắn!

+0

Lưu ý: itext là thương mại hoặc AGPL – Ika

4

Có nhiều thư viện có hỗ trợ cho việc này:

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

vâng, ví dụ điển hình của nó. Làm thế nào tôi có thể thêm 4 hình ảnh trên trang ... mọi cài đặt để áp dụng trên mã. –

+0

Bạn có một ví dụ về liên kết iTextSharp tôi đã gửi cho bạn cách thêm nhiều hình ảnh. Nếu bạn muốn chúng được hiển thị khác nhau, chỉ cần thêm chúng vào một bảng thay vì đoạn văn. Tôi đã không thử nó, nhưng nó sẽ hoạt động. Hoặc nếu đó là không đủ, bạn có thể hợp nhất chúng trong C#. Cách hợp nhất hình ảnh trong C#: http://stackoverflow.com/questions/465172/merging-two-images-in-c-net –