Sign PDFs with local certificates and smart cards in C++

Certificate-selection placeholder

This is a placeholder, not an implementation that reads a certificate store or communicates with a smart card.

  1. Inspect GetLocalCertificate() in the SecuringAndSigning namespace.
  2. Observe that the helper returns nullptr.
  3. Provide an application-specific certificate-selection implementation before expecting the dependent examples to sign a PDF.
namespace SecuringAndSigningExamples {

	// Placeholder retained from SecuringAndSigningExamples.getLocalCertificate.
	System::SharedPtr<System::Security::Cryptography::X509Certificates::X509Certificate2> GetLocalCertificate()
	{
		//// Sign with certificate selection in the windows certificate store
		//auto store = MakeObject<System::Security::Cryptography::X509Certificates::X509Store>(
		//	StoreLocation::CurrentUser);
		//store->Open(OpenFlags::ReadOnly);
		//auto sel = System::Security::Cryptography::X509Certificates::X509Certificate2UI::SelectFromCollection(store->get_Certificates(),
		//	nullptr, nullptr, X509SelectionFlag::SingleSelection);
		//return sel;
		throw System::NotImplementedException(u"GetLocalCertificate is not implemented. Please implement this function to retrieve a local certificate.");
	}

} // namespace SecuringAndSigningExamples

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.

  1. Open the input PDF as a Document.
  2. Create a SignatureField on page 1 using an Aspose::Pdf::Rectangle.
  3. Call GetLocalCertificate().
  4. When no certificate is returned, add the unsigned field, save the PDF, and return.
  5. When a certificate is available, create an ExternalSignature and configure its properties.
  6. 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 SecuringAndSigningExamples {

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

} // namespace SecuringAndSigning

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.

  1. Open the input PDF as a Document.
  2. Create a PdfFileSignature facade for the document.
  3. Call GetLocalCertificate().
  4. When the helper returns nullptr, print a message, save an unsigned PDF, and return.
  5. When a certificate is available, create an ExternalSignature.
  6. 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 SecuringAndSigningExamples {

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

} // namespace SecuringAndSigning