2

Hy, I'm stuck with merging several PDF form in a single One. I've attemp to do it with PDFSharp, but it can't open a readonly pdf form. I've test with pdfsharp and his unethicalreading paramameter and it does some of the work. It merge PDF but erase the form content... (saddly I've lost my time to fill it...)

here are some parts of my code, perhaps its just a missing parameter that I don't think about...

private void MergePDFs(string outPutFilePath, params string[] filesPath)
        {
            List<PdfReader> readerList = new List<PdfReader>();
            foreach (string filePath in filesPath)
            {
                PdfReader pdfReader = new PdfReader(filePath);
                PdfReader.unethicalreading = true;
                readerList.Add(pdfReader);
            }

            Document document = new Document(PageSize.A4, 0, 0, 0, 0);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outPutFilePath, FileMode.Create));
            document.Open();

            foreach (PdfReader reader in readerList)
            {
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    document.Add(iTextSharp.text.Image.GetInstance(page));
                }
            }
            document.Close();
        }

So if someone has an idea about how merging "modification protected" PDF forms, I'm ready for testing it!

EDIT: After some other tests on non protected PDF forms it doesn't seems to be a protection issue. My MergePDFs doesn't use the form content.

So the Task is still "Merging several PDF forms on C#"

EDIT2: here are 2 files that I want to merge. One is modif protected, one is not. All two are filled. https://drop.infini.fr/r/XFt_Sd8gFc#7thQxyjeGdo8uIkXN6oys82KLB8VE0AHmflb0uYRkVM= https://drop.infini.fr/r/NUa2n2No0R#FaOAGyzk0SrTDtB6CiXUhF7WEKw0EHGHHM7gaKz20o4=

Alex Jud
  • 85
  • 1
  • 9
  • Maybe the reason you can't read it is because it's protected and therefore you shouldn't be allowed to do what you are doing? You are basically trying to forge a protected file, so in some ways I'm glad that it doesn't work. – Neil Sep 08 '20 at 18:52
  • Are you sure the pdf is just "read only" and not somehow drm protected? – Nyerguds Sep 08 '20 at 18:52
  • The PDF is protected for modification, not for filling the form. I can fill the form manually and with itextsharp and save it without issues. When I try to merge this one with and another the protected content is merging, but not the form content. – Alex Jud Sep 08 '20 at 18:59
  • After some other test on a non protected form, the content isn't comming with the merge. So it is'nt a protection issue. – Alex Jud Sep 08 '20 at 19:05
  • PDFSharp or iText .NET, formerly known as iTextSharp? – Amedee Van Gasse Sep 08 '20 at 20:17
  • Probably your pdf is a hybrid AcroForm/XFA form, and you filled in only the XFA form while only the AcroForm form was merged? Without sharing the pdf in question, this actually is merely a wild guessing event... – mkl Sep 08 '20 at 21:18
  • I've add two links. Perhaps you can test your guess! – Alex Jud Sep 09 '20 at 11:08
  • 1
    Your PDFs are not XFA forms. But I just took a closer look at the code you supplied and saw that you use a `PdfWriter`/`GetImportedPage`/`Image.GetInstance` based approach. Such an approach usually is recommended against by iText developers because it explicitly ignores everything but the static content of the page, in particular it drops annotations and your form field widgets are annotations. You should use a `PdfCopy` based solution instead, see [this answer](https://stackoverflow.com/a/15945467/1729265) (the links therein may have become inactive but the code still presents the idea). – mkl Sep 10 '20 at 10:52
  • @mkl Thank you so much!! Your solution work great. I post a corrected code in Answer ! – Alex Jud Sep 10 '20 at 15:03

1 Answers1

0

Thanks to @mkl in comments, I've figured out a solution.

   private void Merge2PDFs(string outPutFilePath, params string[] filesPath)
        {
            byte[] mergedPdf = null;
            using (FileStream fs = new FileStream(outPutFilePath, FileMode.OpenOrCreate))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (Document PDFdocument = new Document())
                    {
                        using (PdfCopy copy = new PdfCopy(PDFdocument, ms))
                        {
                            PDFdocument.Open();

                            foreach (string filePath in filesPath)
                            {
                                PdfReader reader = new PdfReader(filePath);
                                PdfReader.unethicalreading = true;
                                // loop over the pages in that document
                                int n = reader.NumberOfPages;
                                for (int page = 0; page < n;)
                                {
                                    copy.AddPage(copy.GetImportedPage(reader, ++page));
                                }
                            }
                        }
                    }
                    mergedPdf = ms.ToArray();
                    fs.Write(mergedPdf, 0, mergedPdf.Length);

                    fs.Close();
                }
            }
        }

Effectivly, my first approch only copy the Base document and not the form content.

this post for more information

Alex Jud
  • 85
  • 1
  • 9