我使用visual basic和iTextSharp填充PDF表单。
除了其中一个表单字段的长度太短之外,一切都很棒。
根据我的判断,我需要从字典中删除该字段的MAXLEN值...但是如果我能找到如何使用VB和iTextSharp来解决这个问题,那我就开始了。
该字段本身被称为" internalP"并且当前设置为4个字符的长度。我需要它设置为10个字符。
我确实假设我可以以某种方式编辑字段,但花了几个小时环顾四周,我认为解决方案只是删除MAXLEN属性,它只是我找不到任何示例代码。
有人可以帮忙吗?
到目前为止我运行的代码是:
嗨,我的代码如下:
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pdfTemplate As String = "z:\shared\LP1F.pdf"
Dim newFile As String = "z:\shared\Final.pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
' set form pdfFormFields
'''
'this was my first attempt but is not working
'I receive a compilation error saying that I can't use nul'
'''
pdfFormFields.SetFieldProperty("internalP", "FieldMaxLength", 10, null)
pdfFormFields.RegenerateField("internalP")
pdfFormFields.SetField("internalP", "1234567890")
'''
' therefore I started with this code, but then got stuck!!
'''
Dim item As AcroFields.Item
item = pdfFormFields.GetFieldItem("internalP")
Dim pdfDictionary As PdfDictionary = item.GetWidget(0)
pdfDictionary.Remove(PdfName.MAXLEN)
MessageBox.Show("Finished")
' flatten the form to remove editting options, set it to false
' to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = False
' close the pdf
pdfStamper.Close()
End Sub
End Class
答案 0 :(得分:1)
Bruno's answer here让您深入了解正在发生的事情,我建议您仔细阅读。但是,它适用于不同的属性,因此以下代码应该适合您。
''//Get the form item
Dim fi = pdfFormFields.GetFieldItem("internalP")
''//Get the merged propertoes
Dim props = fi.GetMerged(0)
''//Set a new value
props.Put(PdfName.MAXLEN, New PdfNumber(10))
我建议在那里投掷一些空的和绑定的支票,但这应该让你去。