2017-03-21 109 views
0

我在寫一個「combine」.pdf文件應用程序。我使用下面的代碼生成的.pdf文件導致無法由Adobe打開的.pdf文件。這是我執行的代碼:iTextSharp創建一個損壞的.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 

我重視,我作爲輸入,以及所產生的.pdf文件2 .pdf文件(甚至通過Adobe表示,它無法打開)。

任何幫助,非常感謝。

謝謝 喬納森

+0

雙源文件可以在這裏找到: https://www.accurecord-direct.com/pdffiles /confirms/8462_C05i8y456t2i0k0z0p0u1sevke0nwdqvkudg20170321113741.pdf 和https://www.accurecord-direct.com/PDFFILES/Confirms/VSTA_C05s7q717a7x707x7a7o7pvety4lzxw3ucog20170321125034.pdf –

+0

和生成的文件可以在這裏找到:https://www.accurecord-direct.com/PDFFILES /確認/確認_2017_03_21.pdf –

+1

我沒有看到你最終關閉文檔。你只是忘了複製和粘貼?或者你真的不關閉它? – mkl

回答

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! 
+0

你說得對。我忘記關閉文件。不幸的是,在添加該行代碼後,生成的.pdf文件仍然無效。 –

+0

@Jonathan請分享創建的結果文件。 – mkl

+0

以下鏈接指向生成的文件:accurecord-direct.com/PDFFILES/Confirms/confirms_2017_03_21. pdf –