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;
}