Extract PDF signature information in C++

  1. Open the input PDF as a Document.
  2. Create a PdfFileSignature facade for the document.
  3. Call GetSignaturesInfo().
  4. Print ToString() for each returned information object.
#include <iostream>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileSignature.h>
#include <Aspose.PDF.Cpp/Security/SignatureAlgorithmInfo.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

namespace SecuringAndSigning {

// Port of SecuringAndSigningExamples.getSignaturesInfo.
void GetSignaturesInfo(System::String inputFile)
{
    {
        auto document = MakeObject<Document>(inputFile);
        auto signature = MakeObject<PdfFileSignature>(document);

        for (const auto& signatureInfo : signature->GetSignaturesInfo()) {
            std::cout << signatureInfo->ToString() << std::endl;
        }
    }
}

}

Save a certificate from a signature field

The function can finish without creating a file if no field supplies a certificate. It saves only the first available result.

  1. Open the Document with the sample owner password.
  2. Iterate through the form fields and select SignatureField objects.
  3. Call ExtractCertificate() and skip null streams.
  4. Copy the first available certificate stream to outputFile, close the output, and return.
#include <system/exceptions.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/io/stream.h>
#include <system/io/path.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/WidgetAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Field.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/SignatureField.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;

namespace SecuringAndSigning {

// Port of SecuringAndSigningExamples.extractCertificate.
void ExtractCertificate(System::String inputFile, System::String outputFile)
{
    {
        auto document = MakeObject<Document>(inputFile, String(u"owner"));

        for (const auto& field : document->get_Form()) {
            if (!System::DynamicCast_noexcept<SignatureField>(field)) {
                continue;
            }

            auto signatureField = System::DynamicCast<SignatureField>(field);
            {
                auto certificateStream = signatureField->ExtractCertificate();

                if (certificateStream == nullptr) {
                    continue;
                }
                auto output = System::IO::File::Create(outputFile);
                certificateStream->CopyTo(output);
                output->Close();
                return;
            }
        }
    }
}

}

Check certificate extraction

This sample receives a certificate stream but does not write it to a file.

  1. Open the Document with the sample owner password.
  2. Create a PdfFileSignature facade for the document.
  3. Enumerate the signature names.
  4. Call TryExtractCertificate() for each name.
  5. Print a message when extraction succeeds.
#include <iostream>
#include <system/exceptions.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/io/stream.h>
#include <system/io/path.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileSignature.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

namespace SecuringAndSigning {

// Port of SecuringAndSigningExamples.extractCertificateTryExtractCertificateMethod.
void ExtractCertificateTryExtractCertificateMethod(System::String inputFile)
{
    {
        auto document = MakeObject<Document>(inputFile, String(u"owner"));
        auto signature = MakeObject<PdfFileSignature>(document);

        for (const auto& signatureName : signature->GetSignatureNames(true)) {
            SharedPtr<System::IO::Stream> certificate;
            if (signature->TryExtractCertificate(signatureName, certificate)) {
                std::cout << "The certificate extraction succeeded" << std::endl;
            }
        }
    }
}

}

Save a signature appearance image

An appearance image is a visual signature resource, not proof of cryptographic validity. No file is created when no matching image stream is found.

  1. Open the input PDF as a Document.
  2. Iterate through form fields and select SignatureField objects.
  3. Call ExtractImage() with JPEG format and skip null streams.
  4. Copy the first available image stream to outputFile, close the output, and return.
#include <system/exceptions.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/io/stream.h>
#include <system/io/path.h>
#include <drawing/imaging/image_format.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/WidgetAnnotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Field.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/SignatureField.h>
#include <Aspose.PDF.Cpp/Presentation/IndependentImpl/ImageFormat.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Forms;

namespace SecuringAndSigning {

// Port of SecuringAndSigningExamples.extractImagesFromSignatureField.
void ExtractImagesFromSignatureField(System::String inputFile, System::String outputFile)
{
    {
        auto document = MakeObject<Document>(inputFile);

        for (const auto& field : document->get_Form()) {
            if (!System::DynamicCast_noexcept<SignatureField>(field)) {
                continue;
            }

            auto signatureField = System::DynamicCast<SignatureField>(field);
            {
                auto imageStream = signatureField->ExtractImage(System::Drawing::Imaging::ImageFormat::get_Jpeg());

                if (imageStream == nullptr) {
                    continue;
                }
                auto output = System::IO::File::Create(outputFile);
                imageStream->CopyTo(output);
                output->Close();
                return;
            }
        }
    }
}

}