PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

Acrobat Reader always prompts "Do you want to save"

Open leonard0guerra opened this issue 3 years ago • 5 comments

I am trying to open and modify a pdf template with pdfSharp which contains some acrofields. I want to fill the acrofields with some content, make them readonly afterwards and save the pdf document.

All seems to work fine, but when I open the generated document with acrobat reader and close it again, it always prompts me if I want to save the changes.

using (PdfDocument document = PdfReader.Open(filePdfPath, PdfDocumentOpenMode.Modify))
            {
                SetApparence(document, true);

                PdfAcroFieldCollection? fields = document?.AcroForm.Fields;
                string[] names = fields?.DescendantNames ?? Array.Empty<string>();

                foreach (string name in names)
                {
                    PdfAcroField field = fields[name];
                    // Fai delle cose in base al tipo

                    switch (field)
                    {
                         case PdfTextField text:
        
                            text.Value = new PdfString("string value");
                            text.ReadOnly = true;
                            break;
                        // Other Case...
                    }
                }

            }
        }

        private static void SetApparence(PdfDocument document, bool apparence)
        {
            //The populated fields are not visible by default
            if (document.AcroForm.Elements.ContainsKey("/NeedAppearances"))
            {
                document.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(apparence);
            }
            else
            {
                document.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(apparence));
            }
        }

When saving the document all acrofields should be set to readonly, so there is no reason for asking the user if he wants to save changes.

So I want to disable this prompt for the generated document. Can someone help me?

leonard0guerra avatar Jul 21 '22 14:07 leonard0guerra

Looks almost like a copy of this topic in the PdfSharp-Forum.

By setting /NeedAppearances to true, you're instructing Acrobat Reader to generate new Appearances (i.e. Visuals) for the AcroFields. This of course will change the document which in turn causes the save-prompt.

To disable the save-prompt, don't set /NeedAppearances (or set it to false) But then the contents of the fields may not be displayed correctly; the solution to that is to create the Appearances by yourself. (which is not an easy task)

Or, you can ignore the prompt and live with it...

packdat avatar Jul 21 '22 17:07 packdat

You mean something like this. Is there any documentation or resources about it?

leonard0guerra avatar Jul 21 '22 18:07 leonard0guerra

You mean something like this.

Exactly.
However note that the current code is not 100% correct, as is handles only a single annotation per AcroField. A single AcroField may have multiple annotations (e.g. consider a contract-document where a contract-number is repeated on every page, this can be realized by specifying multiple annotations for that field)

Is there any documentation or resources about it?

I'm not aware of any documentation regarding this apart from the PDF-reference manuals published by Adobe. I'd start by reading the PDF-1.7 specification found here or here. Chapters of interest include

  • Interactive Forms (chapter 12.7)
  • Annotations (chapter 12.5), esp. Widget-Annotations (12.5.6.19)
  • Text (chapter 9)
  • Graphics (chapter 8)

packdat avatar Jul 25 '22 16:07 packdat

Any solution to this?

angelru avatar Jan 05 '23 06:01 angelru

Instead of text.Value = new PdfString("string value"); Do text.Text = value;

Finally, do not set /NeedAppearances and then it should be good.

griniak25 avatar Oct 26 '23 15:10 griniak25