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