【问题标题】:Generate single PDF from multiple images从多个图像生成单个 PDF
【发布时间】:2013-01-19 03:26:11
【问题描述】:

我需要从多个图像创建单个 pdf 文件。例如,我有 12 张图片,然后 pdf 将生成 3 页,其中包含 4 张图片在单页中连续 2 张图片。

那么,有没有什么 dll,我可以用来从图像生成 pdf 的示例?

【问题讨论】:

标签: c# asp.net pdf pdf-generation


【解决方案1】:

有多个库对此提供支持:

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

【讨论】:

  • 是的,这是一个很好的例子。如何在页面上添加 4 张图片。任何设置都可以应用于代码。
  • 您在 iTextSharp 链接上有一个示例,我向您发送了如何添加多个图像。如果您希望它们以不同的方式显示,只需将它们添加到表格而不是段落中。我没有尝试过,但它应该可以工作。或者,如果这还不够,您可以将它们合并到 c# 中。 c#中如何合并图片:stackoverflow.com/questions/465172/merging-two-images-in-c-net
【解决方案2】:

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

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

【讨论】:

  • 什么是 PdfWriter ?
  • @Kiquenet 它来自 itextSharp。全名是“iTextSharp.text.pdf.PdfWriter”
【解决方案3】:

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

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

祝你好运!

【讨论】:

  • 注意:itext 是商业或 AGPL
猜你喜欢
  • 2015-03-11
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
相关资源
最近更新 更多