使用RegenerateField的Itextsharp错误:字段名称不能包含'#'

时间:2014-05-13 19:07:24

标签: vb.net itextsharp

如何隐藏PDF中的按钮?当我尝试下面的代码时,我收到一个错误,指出字段名称不能包含“#”。在使用RegenerateField的行上发生错误。

字段名称为“form1 [0]。#pageSet [0] .Page1 [0] .PrintButton1 [0]”

提前致谢。

Using outStream As New MemoryStream
        Dim pdfReader As New PdfReader(pdfBinaryFile)
        Dim stamper As New PdfStamper(pdfReader, outStream)
        Dim form As AcroFields = stamper.AcroFields

        stamper.AddViewerPreference(PdfName.HIDETOOLBAR, New PdfBoolean(True))
        stamper.AddViewerPreference(PdfName.FITWINDOW, New PdfBoolean(True))
        stamper.FormFlattening = False

        Dim keyStringPrintButton As String = stamper.AcroFields.Fields.First(Function(item) item.Key.ToString().ToUpper().Contains("PRINT")).Key.ToString()
        Dim keyValuePrintButton As AcroFields.Item = stamper.AcroFields.Fields.First(Function(item) item.Key.ToString().ToUpper().Contains("PRINT")).Value
        Dim dictionaryEntryPrintButton As Dictionary(Of String, AcroFields.Item)

        dictionaryEntryPrintButton = New Dictionary(Of String, AcroFields.Item)
        dictionaryEntryPrintButton.Add(keyStringPrintButton, keyValuePrintButton)
        form.SetFieldProperty(dictionaryEntryPrintButton.Keys(0), "setfflags", PdfFormField.FLAGS_HIDDEN, Nothing)
        form.RegenerateField(dictionaryEntryPrintButton.Keys(0))

        stamper.Close()
        pdfReader.Close()
End Using

1 个答案:

答案 0 :(得分:0)

我找到了答案。 SetFieldProperty命令使用的是PdfFormField.FLAGS_HIDDEN,它应该使用PDfAnnotation和“setflags”。

所以

form.SetFieldProperty(dictionaryEntryPrintButton.Keys(0),“setfflags”,PdfFormField.FLAGS_HIDDEN,Nothing)

变为

form.SetFieldProperty(dictionaryEntryPrintButton.Keys(0),“setflags”,PdfAnnotation.FLAGS_HIDDEN,Nothing)

我不需要RegenerateField命令。