2

I have two fillable pdf files and did the code to merge those pdfs into one single pdf. Below is my code for that.

public void PDFSplit()
{
    List<string> files=new List<string>();
    files.Add(Server.MapPath("~/Template/sample_pdf.pdf"));
    files.Add(Server.MapPath("~/Template/temp/sample_pdf.pdf"));
    //call method
    Merge(files, Server.MapPath("~/Template/sample_pdf_123.pdf"));
}

//Merge pdf
public  void Merge(List<String> InFiles, String OutFile)
{
    using (FileStream stream = new FileStream(OutFile, FileMode.Create))
    using (iTextSharp.text.Document doc = new iTextSharp.text.Document())
    using (PdfCopy pdf = new PdfCopy(doc, stream))
    {
        doc.Open();

        PdfReader reader = null;
        PdfImportedPage page = null;

        InFiles.ForEach(file =>
        {
            reader = new PdfReader(file);

            for (int i = 0; i < reader.NumberOfPages; i++)
            {
                page = pdf.GetImportedPage(reader, i + 1);
                pdf.AddPage(page);
            }

            pdf.FreeReader(reader);
            reader.Close();
        });
    }
}

The code is working fine, but the problem is when I am trying to read that new generated merged file, it's not showing fields using AcroFields.

//To read pdf data
PdfReader reader = null;

reader = new PdfReader(Server.MapPath("~/Template/sample_pdf_123.pdf"));

AcroFields pdfFormFields = reader.AcroFields;
mkl
  • 90,588
  • 15
  • 125
  • 265
Dhiraj Mane
  • 73
  • 3
  • 10
  • Which iTextSharp version exactly do you use? In early versions you had to use `PdfCopyFields` instead of `PdfCopy` to include fields in the copy. – mkl Apr 13 '18 at 15:11
  • I'm not a native English speaker. You're not a native speaker. However, when I learn a technology, I read tutorials, and that's how I learn English words. I would never say "marge fallible PDF files" because I'd know that this would significantly lower the chances at getting an answer. When I read the subject of your question, I thought you were talking about a margin. In any case: **YOU NEED TO UPGRADE TO iText 7.** Stop using old iText versions!!! – Bruno Lowagie Apr 14 '18 at 12:22
  • @mkl : I am using itextsharp 5.5.13 version – Dhiraj Mane Apr 15 '18 at 11:20
  • Why are you using a *maintenance* release instead of the newest release? A maintenance release means that we fix bugs in an old iText version to help out paying customers who aren't ready to move to iText 7 yet. Are you a paying user in that situation? If not, please upgrade to iText 7 and take a look at my answer. – Bruno Lowagie Apr 16 '18 at 06:55
  • Possible duplicate of [Split PDF form into pages preserving fields](https://stackoverflow.com/questions/31219403/split-pdf-form-into-pages-preserving-fields) – mkl Apr 16 '18 at 11:12
  • As has been exercised in the [duplicate candidate](https://stackoverflow.com/a/31222039/1729265): You have to **a** call `pdf.SetMergeFields();` before opening the document to make the iText 5.5.x `PdfCopy` class copy form field structures, and **b** use `pdf.AddDocument` instead of iterating the pages with `pdf.GetImportedPage` and `pdf.AddPage` whenever using `SetMergeFields`. Or even better, update to iText 7. – mkl Apr 16 '18 at 11:17
  • related: https://stackoverflow.com/a/21723187/1729265, https://stackoverflow.com/a/22348593/1729265, https://stackoverflow.com/a/28567177/1729265 – mkl Apr 16 '18 at 11:26

1 Answers1

0

You are unable to marge fallible PDF files because you are using an old version of iText. Please upgrade to iText 7 for .NET and read the iText 7 jump-start tutorial, more specifically chapter 6 where it says:

Merging forms

This is how it's done:

PdfDocument destPdfDocument = new PdfDocument(new PdfWriter(dest));
PdfDocument[] sources = new PdfDocument[] {
    new PdfDocument(new PdfReader(SRC1)),
    new PdfDocument(new PdfReader(SRC2)) };
PdfPageFormCopier formCopier = new PdfPageFormCopier();
foreach (PdfDocument sourcePdfDocument in sources) {
    sourcePdfDocument.CopyPagesTo(1,
        sourcePdfDocument.GetNumberOfPages(), destPdfDocument, formCopier);
    sourcePdfDocument.Close();
}
destPdfDocument.Close();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno, I tried code given by you to merge pdf, but I am facing new issue that after merging 2 pdf. Fields are getting disappear for second page like "Native language" checkbox. I was trying with this [pdf](https://git.itextsupport.com/projects/I7JS/repos/jumpstart/browse/cmpfiles/chapter04/cmp_job_application.pdf) – Dhiraj Mane Apr 17 '18 at 13:31