首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从多个图像生成单个PDF

从多个图像生成单个PDF
EN

Stack Overflow用户
提问于 2013-02-04 20:14:30
回答 3查看 20.4K关注 0票数 5

我需要从多个图像创建单个pdf文件。例如,我有12个图像,那么pdf将生成3个页面,其中4个图像包含在单页2个图像中。

那么,有没有什么dll,示例可以用来从图片中生成pdf呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-05 19:19:02

谢谢,我已经使用表格在pdf格式的一页上创建了6张图像。

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2013-02-04 20:42:56

有多个库支持这一点:

  • pdfSharp - Working with images tutorial

  1. iTextSharp -working with images tutorial
票数 5
EN

Stack Overflow用户

发布于 2013-02-04 20:32:36

看看"iText in Action"这本书,这本书或多或少也涵盖了iTextSharp,它是iText PDF库的.NET版本。也就是说,您必须编写的C#几乎与Java代码示例完全相同。

您可以从http://itextpdf.com/book/examples.php下载示例。一个特别有趣的示例(用Java语言编写的代码)是how to add an image上的示例。可以在SourceForge上找到相应的C#示例。

祝好运!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14686581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档