将Jpg文件合并到Pdf Stream

时间:2015-07-27 18:54:19

标签: asp.net vb.net pdf itextsharp

这是我的问题:我正在尝试从文件夹中读取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

1 个答案:

答案 0 :(得分: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()之间。)