Combining PDFs with PDFSharp losing form fields
Asked Answered
S

1

6

I am attempting to concatenate two created PDF files to a new PDF using PDFSharp and this code (which I found here):

        // Open the output document
        PdfDocument outputDocument = new PdfDocument();
        // Iterate files
        foreach (string file in files)
        {
            // Open the document to import pages from it.
            PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

            // Iterate pages
            int count = inputDocument.PageCount;
            for (int idx = 0; idx < count; idx++)
            {
                // Get the page from the external document...
                PdfPage page = inputDocument.Pages[idx];
                // ...and add it to the output document.
                outputDocument.AddPage(page);
            }
        }
        // Save the document...
        string filename = Path.Combine(this.tempFolder, "MyPDF.pdf");
        outputDocument.Save(filename);

The second PDF has form fields which I fill out, also using PDFSharp. The issue I am running into is that when combined into a new PDF, the form fields show up blank.

I have opened up the second PDF after it is created and saved, and the form fields show up with the text just fine.

Am I missing something, or does PDFSharp have some sort of bug in regards to this issue? It seems to me that if I can open and view the PDF just fine, there shouldn't be any problems with combining them.

Thanks in advance for your help!

Sammie answered 19/2, 2013 at 18:56 Comment(1)
I have not found a great solution for this. However what finally worked after many many attempts was to make sure that both pdfdocuments had an acroform property and also make sure that "/NeedApperances" was set to true. returnDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true). You cannot just add an acroform either. You may have to initialize your pdfdocument from and existing pdf with a form.Chine
E
1

PDFsharp does not fully support form fields. I didn't examine this, but there could be a bug when combining PDF files with filled form fields. We continue to maintain and improve PDFsharp, but there are no plans to improve the handling of form fields.

Maybe it'll work if you try it a different way: open the second PDF for modification, open the first for import and add the pages of the first file at the beginning of the second file (this may not work if both files contain filled form fields).
Create a copy of the second file before you do that if you have to keep the original file.

Experiential answered 21/2, 2013 at 9:10 Comment(1)
Thank you for your response. I had already tried your suggestion of adding the first page to the beginning of the second, but didn't have any luck there. It seems to be an issue with the version of PDF: we ended up resolving the issue using something from iTextSharp (see this link). However, I am now struggling with the same issue with a different PDF, where this workaround does not seem to resolve the issue... =SMahdi

© 2022 - 2024 — McMap. All rights reserved.