PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Acrobat Reader always prompts "Do you want to save"
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3741
Page 1 of 1

Author:  hoebd [ Tue Mar 13, 2018 12:02 am ]
Post subject:  Acrobat Reader always prompts "Do you want to save"

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.

This is my code:

Code:
public void generateTestDocument()
{
    PdfDocument pdfDocument = PdfReader.Open("Template.pdf", PdfDocumentOpenMode.Modify);
           
    if(pdfDocument != null)
    {
        PdfAcroForm acroForm = pdfDocument.AcroForm;
        if (acroForm != null)
        {
            PdfAcroField.PdfAcroFieldCollection fields = acroForm.Fields;
            string[] names = fields.Names;

            for (int i = 0; i < names.Length; i++)
            {
                string fqName = names[i];
                PdfAcroField field = fields[fqName];
                PdfTextField txtField;

                if ((txtField = field as PdfTextField) != null)
                {                           
                    txtField.Value = new PdfString(fqName);
                    txtField.ReadOnly = true;                           
                }
            }
        }

        pdfDocument.Save("GeneratedDocument.pdf");
    }
}




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?

Author:  Thomas Hoevel [ Tue Mar 13, 2018 9:39 am ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

Hi!

You only set text fields to read only. Maybe you should do that for all form fields.

What are the differences between the PDF file generated by PDFsharp and the PDF file saved by Adobe Reader?

See also:
viewtopic.php?p=10610#p10610
I think the Flatten() method is included with the latest versions of PDFsharp.

Author:  hoebd [ Tue Mar 13, 2018 10:24 am ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

Hi Thomas,

thank you very much for your fast reply. I tested to set all fields to readonly, but that didn't solve the problem.

I am not sure what the difference between the documents is, that's what I want to find out. When I click on "save changes" in acrobat reader and open/close the document the second time, then it closes without asking to save changes. So it seems like pdfsharp is setting some parameter that there are unsaved changes in the document.

Is there already a nuget packet for the latest version that I need for the flatten() method?

Author:  Thomas Hoevel [ Tue Mar 13, 2018 10:38 am ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

hoebd wrote:
Is there already a nuget packet for the latest version that I need for the flatten() method?
Should be included in any package from the last 6 months.
The latest and recommended packages have version 1.50.4790-beta5a.

hoebd wrote:
I am not sure what the difference between the documents is, that's what I want to find out.
Compare the PDF file from PDFsharp and the file from Adobe Reader with a Diff tool.
It can help to make a minimal PDF with just one page and one textfield.

Author:  hoebd [ Thu Mar 15, 2018 9:33 pm ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

I have installed PDFsharp version 1.32.2062 with a nuget package, but I can not find the flatten() method there.

So I created a new test project and imported the dll files for version 1.5 beta. Here I can call the pdfDocument.flatten() method before saving the document, but acrobat reader still asks me to save the changes.

Can you recommend me any diff tool to compare pdf files?

Author:  TH-Soft [ Fri Mar 16, 2018 6:42 am ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

hoebd wrote:
I have installed PDFsharp version 1.32.2062 with a nuget package, but I can not find the flatten() method there.
You cannot expect to find the latest features in a four-year-old NuGet package.
Click "include prerelease" to see the current RC1.


hoebd wrote:
Can you recommend me any diff tool to compare pdf files?
Anything that works for source code files.
Or just open both PDF files with WordPad and compare what you have.
Keep the PDF file small and simple.

Author:  hoebd [ Fri Mar 16, 2018 11:11 am ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

I have created a simple pdf template with one textfield, then I filled this textfield with pdfsharp 1.5 and used the flatten() method before saving it.

Afterwards I opened a copy of the generated document, saved it with acrobat reader and compared it to the original generated document with notepad++.

The results are completely different, as you can see in the attached screenshots.

Attachments:
pdfsharp_compared_part3.png
pdfsharp_compared_part3.png [ 97.33 KiB | Viewed 3135 times ]
pdfsharp_compared_part2.png
pdfsharp_compared_part2.png [ 247.91 KiB | Viewed 3135 times ]
pdfsharp_compared_part1.png
pdfsharp_compared_part1.png [ 234.5 KiB | Viewed 3135 times ]

Author:  Thomas Hoevel [ Mon Mar 19, 2018 12:18 pm ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

I use Adobe Reader XI on my computer.
When I open a file marked for "PDF 1.3" or "PDF 1.4" with Adobe Reader and select File => Save As then the new file has the same PDF version as the old one.
I have Adobe Reader, not Adobe Acrobat.

Your file was changed from 1.4 to 1.6 and the 1.6 version used some new optimizations and therefore the files are completely different. This makes the file incompatible with older Reader versions.

It works for me. Sorry it does not work for you.

Author:  hoebd [ Tue Mar 20, 2018 2:32 pm ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

I have noticed that the problem is already in the pdf template. When I open the pdf template with acrobat reader and close it there is already the saving-prompt.
So what I did is to click on yes and saved the template.

When I use this saved template with pdfsharp and generate a document, then the generated document does not have a saving-prompt anymore.
So this problem seemed to be resolved, but when I do it this way, the acroform fields of the generated document does not show the value I set with pdfsharp anymore.

So I googled and found this solution:

Code:
if (pdfDocument.AcroForm != null)
{
    if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
    {
        pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
    }
    else
    {
        pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
    }
}



With this code the text will be shown in the acroform textfields again, but the the saving-prompt appears again too.

So I think the problem is connected to this parameter. But how can I reach both, no saving-prompt and the correct value in the acroform textfields?


Here you can see my complete test code:

Code:
public void generateTestDocument()
{
    PdfDocument pdfDocument = PdfReader.Open("Template_saved.pdf", PdfDocumentOpenMode.Modify);

    if (pdfDocument != null)
    {
        PdfAcroForm acroForm = pdfDocument.AcroForm;


        if (pdfDocument.AcroForm != null)
        {
            if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
            {
                pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
            }
            else
            {
                pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
            }
        }


        if (acroForm != null)
        {
            PdfAcroField.PdfAcroFieldCollection fields = acroForm.Fields;
            string[] names = fields.Names;

            for (int i = 0; i < names.Length; i++)
            {
                string fqName = names[i];
                PdfAcroField field = fields[fqName];
                PdfTextField txtField;
                field.ReadOnly = false;

                if ((txtField = field as PdfTextField) != null)
                {
                    string text = "[test]";
                    txtField.Value = new PdfString(text);
                }                       

                field.ReadOnly = true;
            }
        }

        pdfDocument.Flatten();
        pdfDocument.Save("GeneratedDocument.pdf");
    }
}

Author:  Stackarn [ Wed Mar 20, 2019 4:18 pm ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

hoebd wrote:
I have noticed that the problem is already in the pdf template. When I open the pdf template with acrobat reader and close it there is already the saving-prompt.
So what I did is to click on yes and saved the template.

When I use this saved template with pdfsharp and generate a document, then the generated document does not have a saving-prompt anymore.
So this problem seemed to be resolved, but when I do it this way, the acroform fields of the generated document does not show the value I set with pdfsharp anymore.

So I googled and found this solution:

Code:
if (pdfDocument.AcroForm != null)
{
    if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
    {
        pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
    }
    else
    {
        pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
    }
}



With this code the text will be shown in the acroform textfields again, but the the saving-prompt appears again too.

So I think the problem is connected to this parameter. But how can I reach both, no saving-prompt and the correct value in the acroform textfields?


Here you can see my complete test code:

Code:
public void generateTestDocument()
{
    PdfDocument pdfDocument = PdfReader.Open("Template_saved.pdf", PdfDocumentOpenMode.Modify);

    if (pdfDocument != null)
    {
        PdfAcroForm acroForm = pdfDocument.AcroForm;


        if (pdfDocument.AcroForm != null)
        {
            if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
            {
                pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
            }
            else
            {
                pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
            }
        }


        if (acroForm != null)
        {
            PdfAcroField.PdfAcroFieldCollection fields = acroForm.Fields;
            string[] names = fields.Names;

            for (int i = 0; i < names.Length; i++)
            {
                string fqName = names[i];
                PdfAcroField field = fields[fqName];
                PdfTextField txtField;
                field.ReadOnly = false;

                if ((txtField = field as PdfTextField) != null)
                {
                    string text = "[test]";
                    txtField.Value = new PdfString(text);
                }                       

                field.ReadOnly = true;
            }
        }

        pdfDocument.Flatten();
        pdfDocument.Save("GeneratedDocument.pdf");
    }
}



Hi!

Did you find a solution for this?
I'm trying to find out whats wrong with my code. I have a template that i fill using PDFSharp (and powershell).

When I use the "AcroForm.Fields" to fill it and then save the file, the text only shows in acrobat programs when I click the field (but if I open the PDF in eg. a browser, the fields are filled).
It works fine if I add "AcroForm.Elements["/NeedAppearances"]" but then Acrobat software promts me to save changes when the new file is opened.

Anybody got an idea whay I get this problem?

Author:  (void) [ Thu Mar 21, 2019 6:43 pm ]
Post subject:  Re: Acrobat Reader always prompts "Do you want to save"

Quote:
Anybody got an idea whay I get this problem?

What i THINK what happens:

You have a template containing AcroFields which do not have appearances (at least one of them).
(An appearance describes, how to display the field in the PdfReader)
So when you fill a field, save the document and open it in a PdfReader, the field may appear empty, because there is no information on how to display it.

From the PdfReference about NeedAppearances:
A flag specifying whether to construct appearance streams and appearance dictionaries for all widget annotations in the document

So when you set the flag, a PdfReader will look through all widgets (which AcroFields are) and if it finds, that the widget does not have an appearance, it will create one based on the information in the AcroField dictionaries.
In essence you tell the reader to change the document, which causes the save-prompt.

The solution ?
Create the appearances by yourself.

I also tried that in a forked version of PdfSharp
Have a look at the PdfTextField class and its RenderAppearance method.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/