iTextSharp会创建一个损坏的.pdf文件

时间:2017-03-21 15:46:47

标签: pdf itext

我正在写一个"结合" .pdf文件应用程序。我使用下面的代码生成的.pdf文件会生成一个无法由Adobe打开的.pdf文件。这是我正在执行的代码:

 Dim doc_fs = CreateObject("Scripting.FileSystemObject")
 Dim document_path = "C:\pdffilesfolder\"
 Dim document_folder = doc_fs.GetFolder(document_path)

 Dim dateArray() As String
 dateArray = lblDateToPrint.Text.Split("/")   'lblDateToPrint.Text contains "3/21/2017"
 If Val(dateArray(0)) < 10 Then
    dateArray(0) = "0" & dateArray(0)
 End If
 If Val(dateArray(1)) < 10 Then
    dateArray(1) = "0" & dateArray(1)
 End If

 Dim outFile as string = document_path & "\confirms_" & dateArray(2) & "_" & dateArray(0) & "_" & dateArray(1) & ".pdf"     
 Dim document = New Document
 Dim writer As PdfCopy = New PdfCopy(document, New FileStream(outFile, FileMode.Create))
 document.Open()


 Dim fileOnServer As String = ""
 Dim fileOnServerDate As String = ""

 For Each item In document_folder.Files
      fileOnServer = item.path
      Dim reader As PdfReader = New PdfReader(fileOnServer)
      writer.AddDocument(reader)
      reader.Close()
 Next

我已经附加了我正在使用的2 .pdf文件作为输入以及生成的.pdf文件(即使通过Adobe说它无法打开)。

非常感谢任何帮助。

谢谢你, 乔纳森

1 个答案:

答案 0 :(得分:2)

您忘了最终关闭Document document

Dim document = New Document
Dim writer As PdfCopy = New PdfCopy(document, New FileStream(outFile, FileMode.Create))
document.Open()

Dim fileOnServer As String = ""
Dim fileOnServerDate As String = ""

For Each item In document_folder.Files
    fileOnServer = item.path
    Dim reader As PdfReader = New PdfReader(fileOnServer)
    writer.AddDocument(reader)
    reader.Close()
Next

document.Close() ' Don't forget to close the document!