生成多个图像单一的PDF [英] Generate single PDF from multiple images

查看:282
本文介绍了生成多个图像单一的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建多个映像单一PDF文件。例如,我有12个图片然后PDF将产生3页带有包括在单页4影像的2个图像在一排。

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.

那么,有没有任何DLL,样品我可以用它来从图像生成PDF?

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

推荐答案

谢谢,我已经使用表中的PDF创建一个页面上6幅图像。

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

这篇关于生成多个图像单一的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆