Sign PDF Documents in C++

Use PdfFileSignature for this task. The examples below retain the original C++ code and API Reference steps.

Sign PDF with basic parameters

Set the certificate and sign page 1 with approval details and the visible flag set to false.

  1. Create PdfFileSignature.
  2. Bind the PDF input with Facade::BindPdf().
  3. Set the signing certificate with PdfFileSignature::SetCertificate().
  4. Apply the digital signature with PdfFileSignature::Sign().
  5. Save the resulting PDF with PdfFileSignature::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileSignature.h>
#include <drawing/rectangle.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileSignature
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void SignPdfWithBasicParameters(String inputFile, String certificateFile, String outputFile)
{
    auto pdfSignature = MakeObject<PdfFileSignature>();
    pdfSignature->BindPdf(inputFile);
    pdfSignature->SetCertificate(certificateFile, u"Aspose2021");
    pdfSignature->Sign(1, u"Document approval", u"qa@example.com", u"New York, USA", false,
                       System::Drawing::Rectangle(10, 10, 200, 60));
    pdfSignature->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileSignature

Sign PDF with certificate object

Configure a PKCS7 certificate object and sign page 1 with the visible flag set to false.

  1. Load the certificate and configure a PKCS7 signature object.
  2. Create PdfFileSignature.
  3. Bind the PDF input with Facade::BindPdf().
  4. Apply the digital signature with PdfFileSignature::Sign().
  5. Save the resulting PDF with PdfFileSignature::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileSignature.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/PKCS7.h>
#include <drawing/rectangle.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileSignature
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using PKCS7 = Aspose::Pdf::Forms::PKCS7;

void SignPdfWithCertificateObject(String inputFile, String certificateFile, String outputFile)
{
    auto certificateSignature = MakeObject<PKCS7>(certificateFile, u"Aspose2021");
    certificateSignature->set_Reason(u"Document approval");
    certificateSignature->set_ContactInfo(u"qa@example.com");
    certificateSignature->set_Location(u"New York, USA");
    certificateSignature->set_Authority(u"Aspose.PDF Example");

    auto pdfSignature = MakeObject<PdfFileSignature>();
    pdfSignature->BindPdf(inputFile);
    pdfSignature->Sign(1, false, System::Drawing::Rectangle(10, 10, 200, 60), certificateSignature);
    pdfSignature->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileSignature