PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Aug 21, 2018 11:31 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Mar 13, 2018 12:02 am 
Offline

Joined: Mon Mar 12, 2018 11:50 pm
Posts: 5
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 13, 2018 9:39 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2849
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 13, 2018 10:24 am 
Offline

Joined: Mon Mar 12, 2018 11:50 pm
Posts: 5
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 13, 2018 10:38 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2849
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 15, 2018 9:33 pm 
Offline

Joined: Mon Mar 12, 2018 11:50 pm
Posts: 5
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?


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 16, 2018 6:42 am 
Offline
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 389
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 16, 2018 11:11 am 
Offline

Joined: Mon Mar 12, 2018 11:50 pm
Posts: 5
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 1413 times ]
pdfsharp_compared_part2.png
pdfsharp_compared_part2.png [ 247.91 KiB | Viewed 1413 times ]
pdfsharp_compared_part1.png
pdfsharp_compared_part1.png [ 234.5 KiB | Viewed 1413 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 19, 2018 12:18 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2849
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 20, 2018 2:32 pm 
Offline

Joined: Mon Mar 12, 2018 11:50 pm
Posts: 5
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");
    }
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group