iTextSharp在WPF中生成损坏的PDF
我不熟悉用iTextSharp编程。我有一个用PDF文件编写的应用程序。文件以word编写,然后以PDF格式保存(共2页)。我的问题是,当我想在PDF上写入时,我看到了写入的数据,但输出的PDF已损坏。我不明白为什么PDF会被破坏iTextSharp在WPF中生成损坏的PDF,wpf,pdf,itextsharp,Wpf,Pdf,Itextsharp,我不熟悉用iTextSharp编程。我有一个用PDF文件编写的应用程序。文件以word编写,然后以PDF格式保存(共2页)。我的问题是,当我想在PDF上写入时,我看到了写入的数据,但输出的PDF已损坏。我不明白为什么PDF会被破坏 Public Sub WriteForm(TemplateFilePath As String, NewFilePath As String, data As FormData) 'input output files Dim oldFile
Public Sub WriteForm(TemplateFilePath As String, NewFilePath As String, data As FormData)
'input output files
Dim oldFile As String = TemplateFilePath
Dim newFile As String = NewFilePath
'if file already exists then delete it
If System.IO.File.Exists(newFile) Then
System.IO.File.Delete(newFile)
End If
' open the reader
Dim reader As New PdfReader(oldFile)
Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
Dim document As New Document(size)
' open the writer
Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
document.Open()
'write data to first page
Me.WriteData(reader, writer, data)
'write second page
Me.AppendCopiedPage(document, writer, reader, 2)
' close the streams
document.Close()
fs.Close()
writer.Close()
reader.Close()
End Sub
'写入数据的代码
Private Sub WriteData(reader As PdfReader, writer As PdfWriter, data As FormData)`
Dim cb As PdfContentByte = writer.DirectContent
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED)
cb.SetColorFill(BaseColor.BLACK)
cb.SetFontAndSize(bf, 10)
' create the first page and add it to the pdf
Dim page1 As PdfImportedPage = writer.GetImportedPage(reader, 1)
cb.AddTemplate(page1, 0, 0)
Me.WriteText(cb, data.SID, 110, 653)
...
End Sub
在writer之后关闭fs。不幸的是,它不起作用:(我想我可能有问题。WriteData--私有子WriteData(reader作为PdfReader,writer作为PdfWriter,data作为FormData)Dim cb作为PdfContentByte=writer.DirectContent Dim bf作为BaseFont=BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1250,BaseFont.EMBEDDED)Dim page1作为PDFIImportedPage=writer.GetImportedPage(reader,1)cb.AddTemplate(page1,0,0)“…结束子窗口是否可以使用在上窗口中添加的已写入数据代码更新问题请同时提供说明问题的示例PDF。