【问题标题】:Merging Jpg file to Pdf Stream将 Jpg 文件合并到 Pdf 流
【发布时间】:2015-10-18 02:36:28
【问题描述】:

这是我的问题:我正在尝试从文件夹中读取 JPG 文件并将它们转换为一个 PDF 文件,例如,如果我的文件夹中有 1).Hello.jpg 2)。 World.jpg 我想获取这些文件并将其合并为一个 PDF 文件,因此结果将是 新PDF.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

【问题讨论】:

    标签: asp.net vb.net pdf itextsharp


    【解决方案1】:

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

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

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

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

    编辑

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2021-02-01
      • 2011-06-18
      • 2019-12-25
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多