合并 Jpg 文件到 Pdf Stream

Merging Jpg file to Pdf Stream

这是我的问题:我正在尝试从一个文件夹中读取 JPG 文件并将它们转换为一个 PDF 文件,例如如果我的文件夹中有 1).Hello.jpg 2)。 World.jpg 我想抓取这些文件并将其合并为一个 PDF 文件,这样结果就是 newPDF.pdf

我正在从文件夹中正确读取图像并将它们添加到文档中,但它没有在文件夹中创建新的 PDF 文件。我该如何解决这个问题??

这是我的代码:

'!=Orginally after setting all the files in the folder we need to read the path from the session file.
'!= After reading the path we need to read each file from the folder and generate one pdf file.
Dim attachmentsFolder As String = "E:/IRAttachments/PSC/2013/2/IR-7264"
Dim fileName As String = String.Concat("IR_7264(", DateTime.Now.ToString("yyyyMMddHHmmssfff").ToString(), ").pdf")
Dim finalPathName As String = String.Concat(attachmentsFolder, "/", fileName)
'!= Step 2). read the pdf/images from folder and merge them to a one pdf file.
Dim files As New List(Of String)()
Dim readerList As New List(Of PdfReader)()
m_HashTableIRAttachments = New Hashtable
m_DictionaryEntryIRAttachments = New DictionaryEntry
Dim fileExtentionType As String = String.Empty
Dim doc As Document = New Document

For Each filePath As String In Directory.GetFiles(attachmentsFolder)
    fileExtentionType = filePath.Substring(filePath.LastIndexOf("."))

    If fileExtentionType = ".jpg" Then '# Get the extension type 
        Dim document As New Document()
        Using stream = New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
            PdfWriter.GetInstance(document, stream)
            document.Open()
            Using imageStream = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                Dim image__1 = Image.GetInstance(imageStream)
                document.Add(image__1)
                Dim pdfFile As String = finalPathName
            End Using
            document.Close()
        End Using

        'PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + fileName, FileMode.Create))
        'doc.Open()
        'doc.Add(New Paragraph("Hello World"))
        'Dim myDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
        'Dim pdfFile As String = finalPathName
        'Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, New FileStream(pdfFile, FileMode.Create))
        'myDoc.Open()
        'Dim para As New Paragraph("Let's write some text before inserting image.")
        'Dim myImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(filePath)
        'myImage.ScaleToFit(300.0F, 250.0F)
        'myImage.SpacingBefore = 50.0F
        'myImage.SpacingAfter = 10.0F
        'myImage.Alignment = Element.ALIGN_CENTER
        'myDoc.Add(para)
        'myDoc.Add(myImage)
        'myDoc.Close()
        'doc.Close()

    Else
        '# Means it's a pdf and not a jpg file.
        Dim pdfReader1 As New PdfReader(filePath)
        readerList.Add(pdfReader1)
    End If
Next

当您为 PDF 文件创建 Stream 时,您使用的是 fileName 变量,它只是名称,而不是完整路径。很可能正在创建 PDF - 只是不在您期望的位置。您可能想改用 finalPathName

Using stream = New FileStream(finalPathName, FileMode.Create, FileAccess.Write, FileShare.None)

我还建议您查看 System.IO.Path class 上可用的方法,并在构建文件路径和获取文件扩展名时使用它们,例如

Dim finalPathName As String = Path.Combine(attachmentsFolder, fileName)
'...
fileExtentionType = Path.GetExtension(filePath)
' etc.

编辑

您似乎也在覆盖每个图像文件的 PDF 文件,而我想您希望所有图像都在一个 PDF 文件中。您的图像循环可能应该在 Using stream = ... 块内(例如在 document.Open()document.Close() 之间)。