Digitally sign PDF files in C++
Contents
[
Hide
]
Sign with PKCS#7
- Open the input PDF as a Document.
- Create a PdfFileSignature facade for the document.
- Load PKCS7 from the PFX file with the sample password 12345.
- Call Sign() for page 1 with a visible signature rectangle.
- Save the signed PDF with PdfFileSignature::Save().
#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/PKCS7.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.signDocument.
void SignDocument(System::String inputFile, System::String outputFile, System::String pfxFile)
{
{
auto document = MakeObject<Document>(inputFile);
auto signature = MakeObject<PdfFileSignature>(document);
auto pkcs = MakeObject<PKCS7>(pfxFile, u"12345");
signature->Sign(1, true, System::Drawing::Rectangle(300, 100, 400, 200), pkcs);
signature->Save(outputFile);
}
}
}
Sign with a detached SHA-256 signature
- Open the input PDF as a Document.
- Create a PdfFileSignature facade for the document.
- Create PKCS7Detached from the PFX path and password with DigestHashAlgorithm::Sha256.
- Call Sign() for page 1 with the supplied visible rectangle.
- Save the signed PDF with PdfFileSignature::Save().
#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/PKCS7Detached.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Security/DigestHashAlgorithm.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Facades;
namespace SecuringAndSigning {
// Port of SecuringAndSigningExamples.signDocumentPkcs7Detached.
void SignDocumentPkcs7Detached(System::String inputFile, System::String outputFile, System::String pfxFile, System::String password)
{
{
auto document = MakeObject<Document>(inputFile);
auto signature = MakeObject<PdfFileSignature>(document);
auto pkcs = MakeObject<PKCS7Detached>(pfxFile, password, DigestHashAlgorithm::Sha256);
signature->Sign(1, true, System::Drawing::Rectangle(300, 100, 400, 200), pkcs);
signature->Save(outputFile);
}
}
}
ECDSA signing sample
The supplied README reports that SDK 26.7.0 rejected algorithm 1.2.840.10045.2.1 when running this example. The source calls are preserved; confirm support in your SDK before using this workflow.
- Open the input PDF as a Document.
- Create a PdfFileSignature facade for the document.
- Load the supplied ECDSA PFX into PKCS7Detached with SHA-256.
- Call Sign() for page 1.
- Save the signed PDF with PdfFileSignature::Save().
#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/PKCS7Detached.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Security/DigestHashAlgorithm.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Facades;
namespace SecuringAndSigning {
// Port of SecuringAndSigningExamples.signEcdsa.
void SignEcdsa(System::String inputFile, System::String outputFile, System::String pfxFile, System::String password)
{
{
auto document = MakeObject<Document>(inputFile);
auto signature = MakeObject<PdfFileSignature>(document);
auto pkcs = MakeObject<PKCS7Detached>(pfxFile, password, DigestHashAlgorithm::Sha256);
signature->Sign(1, true, System::Drawing::Rectangle(300, 100, 400, 200), pkcs);
signature->Save(outputFile);
}
}
}
Sign using a timestamp server
Running this example contacts https://freetsa.org/tsr. It requires network access and a reachable timestamp service; the documentation-generation checks do not contact that service.
- Open the input PDF as a Document.
- Create a PdfFileSignature facade for the document.
- Create a PKCS7 signature using the PFX path and password.
- Assign TimestampSettings using the sample server URL and SHA-256.
- Call Sign() for page 1 with the supplied reason, contact, location, and rectangle.
- Save the signed PDF with PdfFileSignature::Save().
#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/PKCS7.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Signature.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/TimestampSettings.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Security/DigestHashAlgorithm.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;
using namespace Aspose::Pdf::Facades;
namespace SecuringAndSigning
{
void SignWithTimeStampServer(System::String inputFile, System::String outputFile, System::String pfxFile, System::String password)
{
{
auto document = MakeObject<Document>(inputFile);
auto signature = MakeObject<PdfFileSignature>(document);
auto pkcs = MakeObject<PKCS7>(pfxFile, password);
pkcs->set_TimestampSettings(MakeObject<TimestampSettings>(u"https://freetsa.org/tsr", u"", DigestHashAlgorithm::Sha256));
signature->Sign(1, u"Signature Reason", u"Contact", u"Location", true,
System::Drawing::Rectangle(100, 100, 200, 100), pkcs);
signature->Save(outputFile);
}
}
}