Sign PDFs with local certificates and smart cards in C++
Contents
[
Hide
]
Certificate-selection placeholder
This is a placeholder, not an implementation that reads a certificate store or communicates with a smart card.
- Inspect GetLocalCertificate() in the SecuringAndSigning namespace.
- Observe that the helper returns nullptr.
- Provide an application-specific certificate-selection implementation before expecting the dependent examples to sign a PDF.
namespace SecuringAndSigning {
// Placeholder retained from SecuringAndSigningExamples.getLocalCertificate.
System::SharedPtr<System::Security::Cryptography::X509Certificates::X509Certificate2> GetLocalCertificate()
{
return nullptr;
}
}
Create a signature field with a certificate fallback
Despite its function name, this sample creates a signature field rather than extracting existing signature information. With the supplied helper, it saves an unsigned field.
- Open the input PDF as a Document.
- Create a SignatureField on page 1 using an Aspose::Pdf::Rectangle.
- Call GetLocalCertificate().
- When no certificate is returned, add the unsigned field, save the PDF, and return.
- When a certificate is available, create an ExternalSignature and configure its properties.
- Name and add the field, call its Sign() method, and save the PDF.
#include <iostream>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/WidgetAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/ExternalSignature.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Field.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Signature.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/SignatureField.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
namespace SecuringAndSigning {
// Port of SecuringAndSigningExamples.getSignatureInfoUsingSignatureField.
void GetSignatureInfoUsingSignatureField(System::String inputFile, System::String outputFile)
{
{
auto document = MakeObject<Document>(inputFile);
auto signatureField = MakeObject<SignatureField>(
document->get_Pages()->idx_get(1),
MakeObject<Rectangle>(100, 400, 110, 410, true));
auto selectedCertificate = GetLocalCertificate();
if (selectedCertificate == nullptr) {
std::cout << "Local certificate was not found." << std::endl;
document->get_Form()->Add(signatureField, 1);
document->Save(outputFile);
return;
}
auto externalSignature = MakeObject<ExternalSignature>(selectedCertificate);
externalSignature->set_Authority(u"Me");
externalSignature->set_Reason(u"Reason");
externalSignature->set_ContactInfo(u"Contact");
signatureField->set_PartialName(u"sig1");
document->get_Form()->Add(signatureField, 1);
signatureField->Sign(externalSignature);
document->Save(outputFile);
}
}
}
Smart-card example with an unsigned fallback
The supplied code does not select or access a smart card. Implement certificate selection and make its private-key provider available before expecting a signed output.
- Open the input PDF as a Document.
- Create a PdfFileSignature facade for the document.
- Call GetLocalCertificate().
- When the helper returns nullptr, print a message, save an unsigned PDF, and return.
- When a certificate is available, create an ExternalSignature.
- Set the PNG signature appearance, call Sign(), and save through PdfFileSignature.
#include <iostream>
#include <system/exceptions.h>
#include <drawing/rectangle.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileSignature.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/ExternalSignature.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Facades;
namespace SecuringAndSigning {
// Port of SecuringAndSigningExamples.signWithSmartCard.
void SignWithSmartCard(System::String inputFile, System::String outputFile, System::String pngFile)
{
{
auto document = MakeObject<Document>(inputFile);
auto pdfSignature = MakeObject<PdfFileSignature>(document);
auto selectedCertificate = GetLocalCertificate();
if (selectedCertificate == nullptr) {
std::cout << "Local certificate was not found." << std::endl;
document->Save(outputFile);
return;
}
auto externalSignature = MakeObject<ExternalSignature>(selectedCertificate);
pdfSignature->set_SignatureAppearance(pngFile);
pdfSignature->Sign(1, u"Reason", u"Contact", u"Location", true,
System::Drawing::Rectangle(100, 100, 200, 200), externalSignature);
pdfSignature->Save(outputFile);
}
}
}