0

I am using below given c# code to extract page from a pdf document using itextsharp. The page is extracted fine however it is messing up the acro fields in some way. I am able to see and enter data on extracted page acro fields but when I check the count of acro fields from code it is coming up as "0"

Code to extract:

public void ExtractPage(string sourcePdfPath, string outputPdfPath, int pageNumber)
{
    PdfReader reader = null;
    Document document = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage = null;

    try
    {
        // Intialize a new PdfReader instance with the contents of the source Pdf file:
        reader = new PdfReader(sourcePdfPath);

        // Capture the correct size and orientation for the page:
        document = new Document(reader.GetPageSizeWithRotation(pageNumber));

        // Initialize an instance of the PdfCopyClass with the source 
        // document and an output file stream:
        pdfCopyProvider = new PdfCopy(document, 
            new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

        document.Open();

        // Extract the desired page number:
        importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
        pdfCopyProvider.AddPage(importedPage);
        document.Close();
        reader.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Code to check Acro fields:

private static int GetNumberOfSignatures(string FileName)
{
    int best = 0; if (Path.GetExtension(FileName) != ".pdf" || !File.Exists(FileName)) return   0;
    var form = new PdfReader(FileName).AcroFields;
    var formFields = form.Fields;
    foreach (var cur in formFields)
    {
        if (cur.Key.ToLower().StartsWith("signature_placeholder_"))
        {
            int val = 0;
            if (!int.TryParse(cur.Key.Substring(22), out val)) val = 0;
            if (val > best) best = val;
        }
        if (cur.Key.ToLower() == "signature_placeholder" && best < 1) best = 1;
    }
    return best;
}
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
DevXR
  • 3
  • 2

1 Answers1

0

You are using the wrong code to extract pages. You should use PdfStamper instead of PdfCopy. For instance: if you want to create a new PDF containing nothing but page 4 of an existing document, you should use the following code:

PdfReader reader = new PdfReader(sourcePdfPath);
reader.SelectPages("4");
PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite);
stamper.Close();

For an overview of the parameter of the SelectPages() method, see my answer to PDF Page re-ordering using itext

Note that extracting a page from a signed PDF document will always invalidate the signature. The opposite would be in violation with the specifications regarding digital signatures.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165