Sign or Certify error on modified file Invalid index: index should be in the range [1..n] where n equals to the pages count

When trying to sign or certify a pdf to which a page has been added you receive the error:
Invalid index: index should be in the range [1…n] where n equals to the pages count.
when trying to save. Code snippet and zipped test project. Copy data directory contents to c:\temp or change paths in code.

using (Aspose.Pdf.Document orig = new Aspose.Pdf.Document(“c:\temp\input.pdf”))
{
Aspose.Pdf.Page signaturePage = orig.Pages.Add();
//Create text fragment
TextFragment textFragment = new TextFragment(“Signature Text”);
textFragment.Position = new Position(100, 600);
//Set text properties
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont(“TimesNewRoman”);
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
//Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(signaturePage);
//Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);
using (PdfFileSignature signature = new PdfFileSignature(orig))
{
PKCS7 pkcs = new PKCS7(“c:\Temp\TestCert.pfx”, “TestCert”);
DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
signature.Certify(signature.Document.Pages.Count, “Signature Reason”, “Contact”, “Location”, true, rect, docMdpSignature);
//signature.Sign(orig.Pages.Count, “Signature Reason”, “Contact”, “Location”, true, rect, pkcs);
signature.Save(“c:\Temp\Signed_output.pdf”);
}
}

Hi Mark,


Thanks for contacting support.

In order to resolve your problem, please update following code line.

signature.Certify(signature.Document.Pages.Count-1,
“Signature
Reason”
, “Contact”, “Location”, true, rect, docMdpSignature);<o:p></o:p>


In the event of any further query, please feel free to contact.

That makes the error go away, but it is still not working. The appended page is removed and the signature is then on the wrong page.

Hi Mark,


Thanks for your feedback. Please note as you are adding a new page for signature. As pdf is created dynamically and
in order to get real page in PDF document you should render its dynamic
model by saving it. So please amend you code as following to accomplish the task. Hopefully it will help you to accomplish the task.

using (Aspose.Pdf.Document
orig = new Aspose.Pdf.Document(“c:\temp\input.pdf”))<o:p></o:p>

{

Aspose.Pdf.Page signaturePage = orig.Pages.Add();

//Create text fragment

TextFragment textFragment = new TextFragment("Signature Text");

textFragment.Position = new Position(100, 600);

//Set text properties

textFragment.TextState.FontSize = 12;

textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");

textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);

textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

//Create TextBuilder object

TextBuilder textBuilder = new TextBuilder(signaturePage);

//Append the text fragment to the PDF page

textBuilder.AppendText(textFragment);

MemoryStream ms=new MemoryStream();

orig.Save(ms);

Document neworig = new Document(ms);

using (PdfFileSignature signature = new PdfFileSignature(neworig))

{

PKCS7 pkcs = new PKCS7("c:\\Temp\\TestCert.pfx", "TestCert");

DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);

System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);

signature.Certify(signature.Document.Pages.Count, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);

//signature.Sign(orig.Pages.Count, "Signature Reason", "Contact", "Location", true, rect, pkcs);

signature.Save("c:\\Temp\\Signed_output.pdf");

}

}


Please feel free to contact us for any further assistance.


Best Regards,