Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Aug 20, 2024 4:59 pm

I need to add multiple (up to 4) signatures to a document. When I add more than one, the generated PDF tells me that the "Certification is invalid". For testing, I have applied the same signature 4 times in 4 locations on the PDF. Will the problem go away if all 4 signatures are from different sources?

Here's the code:
Code: Select all
// Quality Engineer
PdfCertificate cert1 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "");
PdfSignature signature1 = new PdfSignature(pdf, page, cert1, "Signature1");
signature1.Certificated = true;
signature1.SignImageSource = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signature1.GraphicsMode = GraphicMode.SignImageOnly;
signature1.Bounds = new RectangleF(81, 631, 122, 30);

// Supplier Quality Engineer
PdfCertificate cert2 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "");
PdfSignature signature2 = new PdfSignature(pdf, page, cert2, "Signature2");
signature2.Certificated = true;
signature2.SignImageSource = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signature2.GraphicsMode = GraphicMode.SignImageOnly;
signature2.Bounds = new RectangleF(203, 631, 122, 30);

// Manufacturing Engineer
PdfCertificate cert3 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "");
PdfSignature signature3 = new PdfSignature(pdf, page, cert3, "Signature3");
signature3.Certificated = true;
signature3.SignImageSource = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signature3.GraphicsMode = GraphicMode.SignImageOnly;
signature3.Bounds = new RectangleF(325, 631, 122, 30);

// Design Engineer
PdfCertificate cert4 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "");
PdfSignature signature4 = new PdfSignature(pdf, page, cert4, "Signature4");
signature4.Certificated = true;
signature4.SignImageSource = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signature4.GraphicsMode = GraphicMode.SignImageOnly;
signature4.Bounds = new RectangleF(446, 631, 122, 30);

tim8w123
 
Posts: 13
Joined: Wed Feb 22, 2023 3:55 pm

Wed Aug 21, 2024 8:38 am

Hello,

Thank you for your inquiry.If you want to add multiple signatures to a document, you can use the 'PdfOrdinarySignatureMaker' interface. I have made a simple demo for your reference. If you have any other questions, please feel free to write to me.
Code: Select all
PdfDocument pdf = new PdfDocument("testMulSign.pdf");
            PdfPageBase page = pdf.Pages[pdf.Pages.Count - 1];
            String imageUrl = "logo.png";
            //set IncrementalUpdate as true
            pdf.FileInfo.IncrementalUpdate = true;
            PdfCertificate cert = new PdfCertificate("gary.pfx", "password", X509KeyStorageFlags.DefaultKeySet);
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, cert);
            Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature = signatureMaker.Signature;
            PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);
            appearance.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
            appearance.SignatureImage = PdfImage.FromFile(imageUrl);   
            for (int i=0;i<4;i++)
            {
                signatureMaker.MakeSignature("Signature" + (i+1), page, 81+i*100, 431, 122, 80, appearance);
            }
            pdf.SaveToFile("newMulSign.pdf", FileFormat.PDF);

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Wed Aug 21, 2024 4:00 pm

That kind of worked. Now I get the first 3 signatures showing as "Signature Invalid, Document Altered" and the last one as "Valid Signature". How do I get all four to be valid?

tim8w123
 
Posts: 13
Joined: Wed Feb 22, 2023 3:55 pm

Thu Aug 22, 2024 5:58 am

Hello,

Thank you for your feedback. Did you also test with the latest version(spir.pdf for. NET Version: 10.8.1)? If so, as for your situation, please first make sure that you have trusted the root certificate at your side. My testing result shows that all four signatures are valid. You can share your output PDF for reference. You can send it to us here or via email(support@e-iceblue.com ). Thank you in advance.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Thu Aug 22, 2024 2:29 pm

I was using 10.6.1. I updated it to 10.8.1 and there was no change. Not sure I understand what you mean by "please first make sure that you have trusted the root certificate at your side.". Right now, the certificate I am using is the same for all four, and remember, Adobe is not claiming that the certificate is not a valid certificate, it's saying that the first three signatures are not valid because the document has changed.

Here is the code I am using again:
Code: Select all
pdf.FileInfo.IncrementalUpdate = true;
// Quality Engineer
PdfCertificate cert1 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "", X509KeyStorageFlags.DefaultKeySet);
PdfOrdinarySignatureMaker signatureMaker1 = new PdfOrdinarySignatureMaker(pdf, cert1);
Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature1 = signatureMaker1.Signature;
PdfSignatureAppearance appearance1 = new PdfSignatureAppearance(signature1);
appearance1.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
appearance1.SignatureImage = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signatureMaker1.MakeSignature("Signature1", page, 91, 641, 121, 29, appearance1);
// Supplier Quality Engineer
PdfCertificate cert2 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "", X509KeyStorageFlags.DefaultKeySet);
PdfOrdinarySignatureMaker signatureMaker2 = new PdfOrdinarySignatureMaker(pdf, cert2);
Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature2 = signatureMaker2.Signature;
PdfSignatureAppearance appearance2 = new PdfSignatureAppearance(signature2);
appearance2.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
appearance2.SignatureImage = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signatureMaker2.MakeSignature("Signature2", page, 213, 641, 121, 29, appearance2);
// Manufacturing Engineer
PdfCertificate cert3 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "", X509KeyStorageFlags.DefaultKeySet);
PdfOrdinarySignatureMaker signatureMaker3 = new PdfOrdinarySignatureMaker(pdf, cert3);
Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature3 = signatureMaker3.Signature;
PdfSignatureAppearance appearance3 = new PdfSignatureAppearance(signature3);
appearance3.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
appearance3.SignatureImage = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signatureMaker3.MakeSignature("Signature3", page, 335, 641, 121, 29, appearance3);
// Design Engineer
PdfCertificate cert4 = new PdfCertificate(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "", X509KeyStorageFlags.DefaultKeySet);
PdfOrdinarySignatureMaker signatureMaker4 = new PdfOrdinarySignatureMaker(pdf, cert4);
Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature4 = signatureMaker4.Signature;
PdfSignatureAppearance appearance4 = new PdfSignatureAppearance(signature4);
appearance4.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
appearance4.SignatureImage = PdfImage.FromFile(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png");
signatureMaker4.MakeSignature("Signature4", page, 457, 641, 123, 29, appearance4);

tim8w123
 
Posts: 13
Joined: Wed Feb 22, 2023 3:55 pm

Fri Aug 23, 2024 10:34 am

Hello,

Thank you for your inquiry.If you want to use the same certificate for multiple signatures, you cannot directly use multiple 'PdfOrdinarySignatureMaker' object. You can refer to the attached code.txt(code1). Then, if you want to use multiple certificates for multiple signatures, you can refer to the code2. If you have any other questions, please feel free to write to me.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Fri Aug 23, 2024 2:48 pm

If I'm following you correctly, for my case where I have 4 different signers for the same PDF file, I need to use Code2 in your example. I tried it for just two of the signatures. I have two valid PFX files and used the following code:

Code: Select all
pdf.FileInfo.IncrementalUpdate = true;
// Quality Engineer
CreateSignatures(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".pfx", "", pdf, @"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png", "Signature", new Rectangle(91, 641, 121, 29));
// Supplier Quality Engineer
CreateSignatures(@"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + "_2.pfx", "", pdf, @"\\kavmplvdt07.cst.global\NON-ITAR\Manufacturing\Software\AERO Projects\Projects\WIP Quality Database\Certs\" + sUserName.ToUpper() + ".png", "Signature", new Rectangle(213, 641, 121, 29));

pdf.SaveToFile(outputFile, FileFormat.PDF);


private void CreateSignatures( String certPath, String password, PdfDocument pdf, String imgURL, String sigName, Rectangle sigPosition)
{
PdfPageBase page = pdf.Pages[pdf.Pages.Count - 1];
PdfCertificate cert = new PdfCertificate(certPath, password, X509KeyStorageFlags.DefaultKeySet);
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, cert);
Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature = signatureMaker.Signature;
PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);
appearance.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
appearance.SignatureImage = PdfImage.FromFile(imgURL);
signatureMaker.MakeSignature(sigName, page, sigPosition.Left, sigPosition.Top, sigPosition.Width, sigPosition.Height, appearance);
}


The code works fine if I only create the first one. As soon as I add the second signature, Adobe Acrobat gives me the following error and will not load the PDF:
AdobeError.png


I am a little confused at the multiple pdf.SaveToFile calls. Do you have to create a new file every time you add a new signature? Doesn't seem to make much sense.

tim8w123
 
Posts: 13
Joined: Wed Feb 22, 2023 3:55 pm

Mon Aug 26, 2024 7:32 am

Hello,

Thank you for your inquiry.I have reviewed your code and found that you did not save the PDF document immediately after signing and then load it for the next signature. When your certificate information is different, you need to use 'pdf.SaveToFile' methods to save the signed information first. Here is the code I have adjusted, you can try running it again.If you have any other questions, please feel free to write to me.

Code: Select all
public static void multipleSignaturesCustom()
        {
            PdfDocument pdf = new PdfDocument(@"testMulSign.pdf");
            String imageUrl = @"logo.png";
            String outputFile = @"MulSignCustom.pdf";
            pdf.FileInfo.IncrementalUpdate = true;
            // Quality Engineer
            CreateSignatures(@"ComodoSSL.pfx", "", pdf, imageUrl, "Signature1", new Rectangle(91, 641, 121, 29), outputFile);
            // Supplier Quality Engineer
            pdf = new PdfDocument(outputFile);
            CreateSignatures(@"gary.pfx", "", pdf, imageUrl, "Signature2", new Rectangle(213, 641, 121, 29), outputFile);
            pdf.SaveToFile(outputFile, FileFormat.PDF);
        }

        private static void CreateSignatures(String certPath, String password, PdfDocument pdf, String imgURL, String sigName, Rectangle sigPosition, String outputFile)
        {
            PdfPageBase page = pdf.Pages[pdf.Pages.Count - 1];
            PdfCertificate cert = new PdfCertificate(certPath, password, X509KeyStorageFlags.DefaultKeySet);
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, cert);
            Spire.Pdf.Interactive.DigitalSignatures.PdfSignature signature = signatureMaker.Signature;
            PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);
            appearance.GraphicMode = Spire.Pdf.Interactive.DigitalSignatures.GraphicMode.SignImageOnly;
            appearance.SignatureImage = PdfImage.FromFile(imgURL);
            signatureMaker.MakeSignature(sigName, page, sigPosition.Left, sigPosition.Top, sigPosition.Width, sigPosition.Height, appearance);
            pdf.SaveToFile(outputFile);
        }

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.PDF