英文:
PDFGeneration in VB.Net using iText: Trailer not found (iText.Kernel.PdfException)
问题
以下是翻译好的部分:
' 生成信函
Dim oMemory_Document_Writer As New System.IO.MemoryStream()
Dim oPDF_Document_Writer As PdfWriter = New PdfWriter(oMemory_Document_Writer)
If sLang = "D" Then
sTemplate_Based_On_Language = cTemplate_D
Else
sTemplate_Based_On_Language = cTemplate_F
End If
Dim oPDF_Reader_Form_With_Fields As PdfReader = New PdfReader(sTemplate_Based_On_Language)
Dim oPDF_Document_Form_Filled_Out As PdfDocument = New PdfDocument(oPDF_Reader_Form_With_Fields, oPDF_Document_Writer)
Dim oForm_Letter As iText.Forms.PdfAcroForm = iText.Forms.PdfAcroForm.GetAcroForm(oPDF_Document_Form_Filled_Out, True)
Dim oForm_Fields As IDictionary(Of String, iText.Forms.Fields.PdfFormField)
oForm_Fields = oForm_Letter.GetFormFields()
oForm_Fields.Item(PDF_Variables.Letter.Nom).SetValue(oClient.Name)
oForm_Fields.Item(PDF_Variables.Letter.Adresse).SetValue(oClient.Adresse)
sBarcode = sCode
oForm_Fields.Item(PDF_Variables.Letter.BarCode.Bar).SetValue(GED_Code128b(sBarcode))
oForm_Fields.Item(PDF_Variables.Letter.BarCode.Text).SetValue("Code " & sBarcode)
oForm_Letter.FlattenFields()
oPDF_Reader_Form_With_Fields.Close() ' 下一行中引发的异常
oPDF_Document_Form_Filled_Out = New PdfDocument(New PdfReader(New System.IO.MemoryStream(oMemory_Document_Writer.ToArray())))
oPDF_Document_Form_Filled_Out.CopyPagesTo(1, oPDF_Document_Form_Filled_Out.GetNumberOfPages(), oPDF_Document_Result)
oPDF_Document_Form_Filled_Out.Close()
如果需要进一步解决错误,请提供完整的错误堆栈信息。
英文:
I have inherited the following code to generate a PDF:
' Generate letter
Dim oMemory_Document_Writer As New System.IO.MemoryStream()
Dim oPDF_Document_Writer As PdfWriter = New PdfWriter(oMemory_Document_Writer)
If sLang = "D" Then
sTemplate_Based_On_Language = cTemplate_D
Else
sTemplate_Based_On_Language = cTemplate_F
End If
Dim oPDF_Reader_Form_With_Fields As PdfReader = New PdfReader(sTemplate_Based_On_Language)
Dim oPDF_Document_Form_Filled_Out As PdfDocument = New PdfDocument(oPDF_Reader_Form_With_Fields, oPDF_Document_Writer)
Dim oForm_Letter As iText.Forms.PdfAcroForm = iText.Forms.PdfAcroForm.GetAcroForm(oPDF_Document_Form_Filled_Out, True)
Dim oForm_Fields As IDictionary(Of String, iText.Forms.Fields.PdfFormField)
oForm_Fields = oForm_Letter.GetFormFields()
oForm_Fields.Item(PDF_Variables.Letter.Nom).SetValue(oClient.Name)
oForm_Fields.Item(PDF_Variables.Letter.Adresse).SetValue(oClient.Adresse)
sBarcode = sCode
oForm_Fields.Item(PDF_Variables.Letter.BarCode.Bar).SetValue(GED_Code128b(sBarcode))
oForm_Fields.Item(PDF_Variables.Letter.BarCode.Text).SetValue("Code " & sBarcode )
oForm_Letter.FlattenFields()
oPDF_Reader_Form_With_Fields.Close()
' V Exception thrown in next line V
oPDF_Document_Form_Filled_Out = New PdfDocument(New PdfReader(New System.IO.MemoryStream(oMemory_Document_Writer.ToArray())))
oPDF_Document_Form_Filled_Out.CopyPagesTo(1, oPDF_Document_Form_Filled_Out.GetNumberOfPages(), oPDF_Document_Result)
oPDF_Document_Form_Filled_Out.Close()
It throws an exception in the indicated line
I read several threads here related to this issue, but none of the solutions have worked for me so far.
How do I fix this error?
Here is the full StackTrace
If I call oPDF_Document_Form_Filled_Out.Close() before (as suggested by mkl and other posts, I get this exception : "System.ObjectDisposedException: 'Cannot access a closed file." (Stacktrace)
答案1
得分: 1
在你的代码中,你首先在oPDF_Document_Form_Filled_Out
中创建了一个PdfDocument
,然后你将其输出用于进一步处理。
然而,要使用第一个PdfDocument
的输出,必须将其关闭,否则它将是不完整的。你在代码中忘记了这一点。
你确实关闭了为之构建PdfDocument
的PdfReader
。在关闭PdfDocument
时,可能需要访问其PdfReader
。因此,在关闭PdfReader
之前,你必须先关闭PdfDocument
。
英文:
In your code you first create a PdfDocument
in oPDF_Document_Form_Filled_Out
and thereafter you use its output for further processing.
To use the output of the first PdfDocument
, though, it must be closed. Otherwise it's incomplete. You forgot that in your code.
You do close the PdfReader
that PdfDocument
has been constructed for. When closing the PdfDocument
, it may require access to its PdfReader
. Thus, you must close the PdfDocument
before closing the PdfReader
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论