Cannot sign PDF

I am using below code segment to sign a PDF,
using Aspose.Pdf;
using Aspose.Pdf.Facades;
using Aspose.Pdf.Forms;

const string certificateAsBase64String = “BASE64 encoded bytes”;
var certificateBytes = Convert.FromBase64String(certificateAsBase64String);
var ms = new MemoryStream(certificateBytes);
using var document = new Document(“Invoice.pdf”);
using var signature = new PdfFileSignature(document);
var pkcs = new PKCS7(ms, “”); //
signature.Sign(1, false, new System.Drawing.Rectangle(300, 100, 400, 200), pkcs);
// Save output PDF file
signature.Save(“Invoice_signed.pdf”);

But the last line threw error
System.ArgumentOutOfRangeException: ‘Index was out of range. Must be non-negative and less than the size of the collection. (Parameter ‘index’)’

with stacktrace
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at #=z6sIGnsbNuXgiMkU431jdzQY21Nt2nNO6Sg==.#=zUeDtL0E=.#=zqra2huo=(Byte[] #=zxqN3OtE=, #=zHj89bs272vK26nh0wvJL1qmVRVUm214pYkN8sKo= #=zR7U5Rv4=, OcspSettings #=zQxNNSwj7GlU4, TimestampSettings #=zPUhhl2Wp622w, Boolean #=zvTW$kRg=, X509Certificate2 #=zp4lvb9qKU_uI)
at #=z6sIGnsbNuXgiMkU431jdzQY21Nt2nNO6Sg==.#=zUeDtL0E=.#=z46PVKiU=(Byte[] #=zxqN3OtE=, #=zHj89bs272vK26nh0wvJL1qmVRVUm214pYkN8sKo= #=zR7U5Rv4=, OcspSettings #=zQxNNSwj7GlU4, TimestampSettings #=zPUhhl2Wp622w, Boolean #=zvTW$kRg=)
at #=z6sIGnsbNuXgiMkU431jdzQY21Nt2nNO6Sg==.#=zUeDtL0E=.#=z46PVKiU=(Byte[] #=zxqN3OtE=, Stream #=ze1IU5_68dbjU, String #=zcrWuHS8=, OcspSettings #=zQxNNSwj7GlU4, TimestampSettings #=zPUhhl2Wp622w, Boolean #=zvTW$kRg=)
at #=z6sIGnsbNuXgiMkU431jdzQY21Nt2nNO6Sg==.#=zUeDtL0E=.#=z46PVKiU=(Byte[] #=zxqN3OtE=, Stream #=ze1IU5_68dbjU, String #=zcrWuHS8=, OcspSettings #=zQxNNSwj7GlU4, TimestampSettings #=zPUhhl2Wp622w)
at #=z6sIGnsbNuXgiMkU431jdzQY21Nt2nNO6Sg==.#=zUeDtL0E=.#=z46PVKiU=(Byte[] #=zxqN3OtE=, Stream #=ze1IU5_68dbjU, String #=zcrWuHS8=)
at #=zG5zgmaqMf_NWUoyosfDYJne5jola.#=z46PVKiU=(#=z8Zx2Kam9l1Eg7OVBOp3KCKdPsxVqDo6fy5i8O0wJuzSU #=z5f6gCx771HUm, Boolean #=zQYOuPd60eW7t, #=zBEAE4JBpHv2_Bzlcmaubjuz$YIUzEFbf6g== #=z9tWE$yM=, #=zkqpQ9FJ7o4qUFyIM4D2cw$E9uDixUwagVw== #=zT6VdQfM=, #=z1W7nKJ5zFZgjgI1ZKrOIEH7ORsidx61fVg== #=zY2M1LYQ=, Stream #=zR7U5Rv4=, String #=zDQ5F57M=, X509Certificate2 #=zp4lvb9qKU_uI, String& #=zyvC97EI=, Int32& #=zqHgIU$06tzCw)
at #=zG5zgmaqMf_NWUoyosfDYJne5jola.#=z46PVKiU=(String #=zntnlMWI=, #=zkqpQ9FJ7o4qUFyIM4D2cw$E9uDixUwagVw== #=zT6VdQfM=, #=z1W7nKJ5zFZgjgI1ZKrOIEH7ORsidx61fVg== #=zY2M1LYQ=, Stream #=zR7U5Rv4=, String #=zDQ5F57M=, X509Certificate2 #=zp4lvb9qKU_uI)
at #=zG5zgmaqMf_NWUoyosfDYJne5jola.#=z46PVKiU=(String #=zntnlMWI=, #=zkqpQ9FJ7o4qUFyIM4D2cw$E9uDixUwagVw== #=zT6VdQfM=, #=z1W7nKJ5zFZgjgI1ZKrOIEH7ORsidx61fVg== #=zY2M1LYQ=, Stream #=zR7U5Rv4=, String #=zDQ5F57M=)
at Aspose.Pdf.Forms.Signature.#=z46PVKiU=(String #=zntnlMWI=, Stream #=zR7U5Rv4=, String #=zDQ5F57M=)
at Aspose.Pdf.Forms.SignatureField.Sign(Signature signature, Stream pfx, String pass)
at Aspose.Pdf.Forms.SignatureField.Sign(Signature signature)
at Aspose.Pdf.Facades.PdfFileSignature.#=z2EJ2KGVf9sP_(Stream #=zmR5a98iRp0A5, Stream #=z5xnxeAY6wTGR, String #=zcrWuHS8=)
at Aspose.Pdf.Facades.PdfFileSignature.Save(Stream outputStream)
at Aspose.Pdf.Facades.PdfFileSignature.Save(String outputFile)
at Program.$(String[] args) in E:\Learn\Testy\AsposePdfTrial\Program.cs:line 16

Can someone help me?

@imgen

Would you please share the source base64 string as well as sample PDF document for our reference? We will test the scenario in our environment and address it accordingly.

Hi Asad,
Ok. The base64 string is
MIIGBzCCBO+gAwIBAgIMRzG5xachtuqdVBsCMA0GCSqGSIb3DQEBDAUAMFIxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMSgwJgYDVQQDEx9HbG9iYWxTaWduIEdDQyBSNDUgQUFUTCBDQSAyMDIwMB4XDTIzMDgwODE2NDYxNFoXDTI1MTAwNzAxMjcxOVowgY4xCzAJBgNVBAYTAkhLMRAwDgYDVQQIEwdLb3dsb29uMRUwEwYDVQQHEwxXb25nIFRhaSBTaW4xHzAdBgNVBAoTFlRvcG8gU29sdXRpb25zIExpbWl0ZWQxFzAVBgNVBAMTDlRvcG8gU29sdXRpb25zMRwwGgYJKoZIhvcNAQkBFg1hZG1pbkB0b3BvLmNjMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1tKLfMhYPXwQfJy09MBojtMaSqZgLTex2QnvW4+8ASY840TUJKjIbtR1frZygRPR+ehqyxuRUstFCdxtEuv8wQvf7CRD9voAIldoK0/GEcgmGD0gS9ybbeRuOFPNGOJ/AKAU9KSccUr9ONF8XiczxQbFnBlezRbinwR1enwP7t2nyY2+Az87SVSeeIEiVkxNVYy6PpBeOs4daKY2jSP+LtuLLidC2gD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jFOHVoSEQ+aXV6IjHH94Yw0oe2BtqfPeMOZWdhvpVzYpoV46xmesUsGlRQu6mLqBWTDInFAMBdrfjQDD6eF5U9gZU/Djm1JggHrwJy5jhvpmDWSU5mEab1e7b5SA6lR12MJ0CPzhgUIrJ7adoiEiYHVFSqNxWkQ6F382jqE4EwURYEKmPFPkAUHvAkneJ0yBC8G4pGXlQeomlX9lc2WWxaIgpm3aiKcUE7q5RZFh20e6DWgbblF8KK1yqqziTwXoz+T3tONeBHx7WHYjdeQO2QL7zcgXVtvAJ3cPYACHa8jzeam/jooKgYwp18Yzt//M6OJCFXk1E0vQGSFSLw=

The PDF file is just one simple PDF. No special. Invoice.pdf (5.7 KB)

@imgen

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-55302

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.