Question

How do I hide a button in a PDF? When I try the code below I get an error that says the Field Name can't contain a '#'. The error occurs on the line that uses RegenerateField.

The field name is "form1[0].#pageSet[0].Page1[0].PrintButton1[0]"

Thanks in advance.

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
Was it helpful?

Solution

I found the answer. The SetFieldProperty command was using PdfFormField.FLAGS_HIDDEN and it should be using PDfAnnotation and "setflags' instead.

So

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

Becomes

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

And I don't need the RegenerateField command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top