我需要从多个图像创建一个PDF文件。例如,我有12张图片,那么pdf将生成3页,其中包含4张图片,一张页面中的2张图片。从多个图像生成单个PDF
那么,有没有任何DLL,我可以用来从图像生成PDF的样本?
我需要从多个图像创建一个PDF文件。例如,我有12张图片,那么pdf将生成3页,其中包含4张图片,一张页面中的2张图片。从多个图像生成单个PDF
那么,有没有任何DLL,我可以用来从图像生成PDF的样本?
谢谢,我已经使用表创建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
看看这本书"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 – Ika
。它有这种支持多个库:
是的,它的好例子。如何在页面上添加4个图像。任何设置应用于代码。 –
你有一个iTextSharp链接的例子,我发给你如何添加多个图像。如果您希望以不同方式显示它们,只需将它们添加到表格中而不是段落中即可。我没有尝试,但它应该工作。或者,如果这还不够,你可以在c#中合并它们。如何在C#中合并图像:http://stackoverflow.com/questions/465172/merging-two-images-in-c-net –
谷歌的iTextSharp的,它是非常有用的,可以用来创建PDF文件,并把图片在那里 – RhysW
你有例如检验http://stackoverflow.com/questions/1273242/third-party-library-to-convert-image-into-pdf-and-eps-format-on-the-fly – mybrave
Thx,我试过搜索,但不是确切的snipplet我有的代码。 –