为什么我不能将包含 PdfFormField
的 PdfPTable
写入现有PDF? iTextSharp告诉我
在此上下文中不受支持 . 使用PdfStamper.addAnnotation() .
Private Sub RR(PdfFileName As String)
Dim reader As New PdfReader(PdfFileName)
Dim RandomFileName As String = System.IO.Path.GetRandomFileName
Dim OutputPdf As String = System.IO.Path.GetDirectoryName(PdfFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(RandomFileName) & ".pdf"
Dim stamper As New PdfStamper(reader, New FileStream(OutputPdf, FileMode.Create))
Dim table As New PdfPTable(1)
Dim PageMargin As Single = 20
Dim tbCell As New PdfPCell()
table.TotalWidth = (reader.GetPageSize(1).Right - reader.GetPageSize(1).Left) - 20
Dim PDfFormField As PdfFormField = CreateTextField(stamper.Writer, "TestField", 0, 0)
tbCell = New PdfPCell With {.CellEvent = New iTextSharp.text.pdf.events.FieldPositioningEvents(stamper.Writer, PDfFormField), .MinimumHeight = 10, .BorderWidth = 0.1}
With table
.AddCell(tbCell)
End With
table.WriteSelectedRows(0, -1, PageMargin, Bottom + table.TotalHeight + PageMargin, stamper.GetOverContent(1))
stamper.Close()
reader.Close()
End Sub
1 回答
问题
iTextSharp类
FieldPositioningEvents
只能在将PdfWriter
与PdfDocument
实例结合使用时从头开始创建新PDF,因为它使用了冲压上下文中不支持的writer.addAnnotation(field)
/fieldWriter.addAnnotation(cellField)
调用 .解决方案
要解决此限制,只需创建自己的单元格事件侦听器,该侦听器使用
addAnnotation
重载,并在PdfStamper
中显示页码 .(遗憾的是,我在本世纪尚未完成任何VB编码;但我希望,C#中的示例也可以提供帮助 . )
例如 . 以下是从原始
FieldPositioningEvents
代码派生的事件监听器,但仅限于单元格事件并适用于PdfStamper
:它的使用方式类似于OP的原始代码: