我遇到了iTextSharp和PDF表格(特别是表单字段)的问题,我已经花了将近两天的时间,因为我非常希望有人能够得到答案。
我有一个PDF表单,当我以用户身份打开它时,我可以在表单字段中输入&符号,并且很好。但是,当我使用iTextSharp使用.SetField填充表单字段值时,&符号会消失。我试过用& (这实际上导致字段中的所有文本显示为空白),&的unicode表示,不平整表单,展平表单等等都无济于事。我不确定问题是什么,因为我提到表单字段当然可以接受使用它的默认编码的逗号和&符号。
有什么东西我不见了吗?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Using iText 4.1.2.0
GeneratePDF2()
End Sub
Private Sub GeneratePDF2()
''//The directory to output files to
Dim WorkingFolder = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim FormFileName = Path.Combine(WorkingFolder, "testfile.pdf")
Dim FinalFileName = Path.Combine(WorkingFolder, "Final.pdf")
''//The name of the form field that we are going to create
Dim TextFieldName = "form1[0].#subform[0].Table3[0].Row2[0].Line2_FullName_and_AddressofEmployer[0]"
Dim FieldValue As String = "Jonathan & Chris & Mark" ' Does Not Work
'Dim FieldValue As String = "Jonathan and Chris and Mark" ' Works
Dim Letter As RandomAccessFileOrArray
'Create a PDF reader object based on the PDF template
Dim PDFReader As PdfReader
'Dim BAOS1 As MemoryStream
Dim Stamper As PdfStamper
Dim BAOS As MemoryStream = New MemoryStream()
Dim Copy As PdfCopyFields = New PdfCopyFields(BAOS)
Dim FormFilePath As String = FormFileName
Letter = New RandomAccessFileOrArray(FormFilePath)
'Create a PDF reader object based on the PDF template
PDFReader = New PdfReader(Letter, Nothing)
Dim BAOS1 As MemoryStream = New MemoryStream()
Stamper = New PdfStamper(PDFReader, BAOS1)
Dim FormFields As AcroFields = Stamper.AcroFields
'Set field value
FormFields.SetField(TextFieldName, FieldValue)
'Rename field after setting value
Dim RenamedFormFieldName As String
RenamedFormFieldName = String.Concat(Guid.NewGuid().ToString, "_", Guid.NewGuid().ToString)
FormFields.RenameField(TextFieldName, RenamedFormFieldName)
' flatten the form to remove editting options, set it to false
' to leave the form open to subsequent manual edits
Stamper.FormFlattening = True
' close the pdf
Stamper.Close()
'This could be the correct location
Copy.AddDocument(New PdfReader(BAOS1.ToArray))
Copy.Writer.CloseStream = False
Copy.Close()
PDFReader = New PdfReader(BAOS.ToArray())
Stamper = New PdfStamper(PDFReader, New FileStream(FinalFileName, FileMode.Create))
Stamper.FormFlattening = True
Stamper.Close()
End Sub
答案 0 :(得分:0)
我无法重现您的问题,我使用的是5.1.1.0版。下面是创建PDF的示例代码,向其添加字段,然后将字段的值设置为This & that
。 (它分三步,因为我不知道如何在初始PDF创建过程中添加字段。)我也尝试在Acrobat中手动创建PDF,我也可以将字段设置为&符号。您是在iTextSharp或其他程序中创建表单字段吗?你可以在某处发布PDF吗?我们可以看一下吗?
Option Explicit On
Option Strict On
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''//The directory to output files to
Dim WorkingFolder = My.Computer.FileSystem.SpecialDirectories.Desktop
''//This sample code creates a base PDF, then adds a text field to it and finally sets the field value.
''//These filenames represent those three actions
Dim BaseFileName = Path.Combine(WorkingFolder, "Base.pdf")
Dim FormFileName = Path.Combine(WorkingFolder, "Form.pdf")
Dim FinalFileName = Path.Combine(WorkingFolder, "Final.pdf")
''//The name of the form field that we are going to create
Dim TextFieldName = "Text1"
''//Create our base PDF
Using FS As New FileStream(BaseFileName, FileMode.Create, FileAccess.Write, FileShare.Read)
Using Doc As New Document(PageSize.LETTER)
Using W = PdfWriter.GetInstance(Doc, FS)
Doc.Open()
Doc.NewPage()
Doc.Add(New Paragraph("This is my form"))
Doc.Close()
End Using
End Using
End Using
''//Add our form field
Using FS As New FileStream(FormFileName, FileMode.Create, FileAccess.Write, FileShare.Read)
Dim R1 = New PdfReader(BaseFileName)
Using S As New PdfStamper(R1, FS)
Dim F As New TextField(S.Writer, New Rectangle(50, 50, 500, 100), TextFieldName)
S.AddAnnotation(F.GetTextField(), 1)
S.Close()
End Using
End Using
''//Set the field value to text with an ampersand
Using FS As New FileStream(FinalFileName, FileMode.Create, FileAccess.Write, FileShare.Read)
Dim R2 = New PdfReader(FormFileName)
Using S As New PdfStamper(R2, FS)
S.AcroFields.SetField(TextFieldName, "This & that")
S.Close()
End Using
End Using
Me.Close()
End Sub
End Class
修改强>
我刚试了一下您发送的PDF文件,它的工作对我来说很合适。下面是我运行的完整代码。 Here's the PDF it made。你确定你没有对PDF做其他事情(我不知道是什么。)如果你创建一个全新的Windows应用程序并使用下面的代码对5.1.1.0它是否适合你? / p>
Option Explicit On
Option Strict On
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''//The directory to output files to
Dim WorkingFolder = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim FormFileName = Path.Combine(WorkingFolder, "testfile.pdf")
Dim FinalFileName = Path.Combine(WorkingFolder, "Final.pdf")
''//The name of the form field that we are going to create
Dim TextFieldName = "form1[0].#subform[0].Table3[0].Row2[0].Line2_FullName_and_AddressofEmployer[0]"
''//Set the field value to text with an ampersand
Using FS As New FileStream(FinalFileName, FileMode.Create, FileAccess.Write, FileShare.Read)
Dim R2 = New PdfReader(FormFileName)
Using S As New PdfStamper(R2, FS)
S.AcroFields.SetField(TextFieldName, "Chris & Mark")
S.FormFlattening = True
S.Close()
End Using
End Using
Me.Close()
End Sub
End Class
答案 1 :(得分:0)
有类似的情况,用户在应用程序中输入的德语变音符号未显示在PDF中。原来是一个字体问题。 不得不使用应用程序发布我们自己的字体(解放包以获得跨平台的Arial风格)并执行此操作(它是Java):
BaseFont baseFont = BaseFont.createFont(FONT_FILE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
acroFields.setFieldProperty(fieldName, "textfont", baseFont, null);