Configure PDF image and text stamps in C++

Use Stamp to create image, PDF-page, and text stamps with placement, rotation, transparency, and page selection.

These examples use Aspose::Pdf::Facades::Stamp. Supply the required image or stamp PDF file; the PDF-page sample uses page 1 of the stamp source.

The snippets are independent example functions with their SDK includes and example namespaces. Configure the Aspose.PDF for C++ SDK, supply the input resources referenced by the sample, and choose writable output paths. The examples retain the source field names, page indexes, coordinates, and options.

Stamp workflows

Add background image stamp

Apply an image stamp behind page content with opacity 0.35, rotation 45 degrees, size 160 by 80, and origin (200, 300).

  1. Create PdfFileStamp and Stamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Bind the stamp image with Stamp::BindImage().
  4. Set is background through Stamp using the example values.
  5. Set opacity through Stamp using the example values.
  6. Set quality through Stamp using the example values.
  7. Set rotation through Stamp using the example values.
  8. Set the image stamp dimensions with Stamp::SetImageSize().
  9. Set stamp placement with Stamp::SetOrigin().
  10. Apply the configured stamp with PdfFileStamp::AddStamp().
  11. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/Facades/Stamp.h>
#include <system/string.h>

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

	void AddBackgroundImageStamp(String inputFile, String imageFile, String outputFile)
	{
		auto pdfStamper = MakeObject<PdfFileStamp>();
		pdfStamper->BindPdf(inputFile);
		auto stamp = MakeObject<Stamp>();
		stamp->BindImage(imageFile);
		stamp->set_IsBackground(true);
		stamp->set_Opacity(0.35f);
		stamp->set_Quality(90);
		stamp->set_Rotation(45.0f);
		stamp->SetImageSize(160, 80);
		stamp->SetOrigin(200, 300);
		pdfStamper->AddStamp(stamp);
		pdfStamper->Save(outputFile);
	}
} // namespace Facades::Examples::PDF_Stamp

Add image stamp

Apply an image stamp with ID 1 at (36, 520).

  1. Create PdfFileStamp and Stamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Bind the stamp image with Stamp::BindImage().
  4. Set stamp id through Stamp using the example values.
  5. Set stamp placement with Stamp::SetOrigin().
  6. Apply the configured stamp with PdfFileStamp::AddStamp().
  7. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/Facades/Stamp.h>
#include <system/string.h>

namespace Facades::Examples::PDF_Stamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using Stamp = Aspose::Pdf::Facades::Stamp;

void AddImageStamp(String inputFile, String imageFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    auto stamp = MakeObject<Stamp>();
    stamp->BindImage(imageFile);
    stamp->set_StampId(1);
    stamp->SetOrigin(36, 520);
    pdfStamper->AddStamp(stamp);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_Stamp

Add PDF page as stamp

Use page 1 from stampPdf as a stamp on page 1 of the target PDF at (36, 250).

  1. Create PdfFileStamp and Stamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Bind the source PDF page with Stamp::BindPdf().
  4. Set page number through Stamp using the example values.
  5. Set stamp placement with Stamp::SetOrigin().
  6. Apply the configured stamp with PdfFileStamp::AddStamp().
  7. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/Facades/Stamp.h>
#include <system/string.h>

namespace Facades::Examples::PDF_Stamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using Stamp = Aspose::Pdf::Facades::Stamp;

void AddPdfPageAsStamp(String inputFile, String stampPdf, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    auto stamp = MakeObject<Stamp>();
    stamp->BindPdf(stampPdf, 1);
    stamp->set_PageNumber(1);
    stamp->SetOrigin(36, 250);
    pdfStamper->AddStamp(stamp);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_Stamp

Add rotating stamp to PDF

Place a 100 by 100 image stamp on page 1 at half the page width and height and rotate it 90 degrees.

  1. Create PdfFileInfo and Stamp and PdfFileStamp.
  2. Bind the stamp image with Stamp::BindImage().
  3. Set is background through Stamp using the example values.
  4. Set pages through Stamp using the example values.
  5. Set rotation through Stamp using the example values.
  6. Set stamp placement with Stamp::SetOrigin().
  7. Read the page width with PdfFileInfo::GetPageWidth().
  8. Read the page height with PdfFileInfo::GetPageHeight().
  9. Set the image stamp dimensions with Stamp::SetImageSize().
  10. Bind the PDF input with Facade::BindPdf().
  11. Apply the configured stamp with PdfFileStamp::AddStamp().
  12. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileInfo.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/Facades/Stamp.h>
#include <system/array.h>
#include <system/string.h>

namespace Facades::Examples::PDF_Stamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using Stamp = Aspose::Pdf::Facades::Stamp;

void AddRotatingStampToPdf(String inputFile, String imageFile, String outputFile)
{
    auto fileInfo = MakeObject<PdfFileInfo>(inputFile);

    auto stamp = MakeObject<Stamp>();

    stamp->BindImage(imageFile);

    stamp->set_IsBackground(false);

    stamp->set_Pages(MakeArray<int32_t>({1}));

    stamp->set_Rotation(90);

    stamp->SetOrigin(fileInfo->GetPageWidth(1) / 2, fileInfo->GetPageHeight(1) / 2);

    stamp->SetImageSize(100, 100);

    auto stamper = MakeObject<PdfFileStamp>();

    stamper->BindPdf(inputFile);

    stamper->AddStamp(stamp);
    stamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_Stamp

Add stamp to specific pages

Limit the image stamp to page 1 with size 120 by 60 and origin (400, 40).

  1. Create PdfFileStamp and Stamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Bind the stamp image with Stamp::BindImage().
  4. Set pages through Stamp using the example values.
  5. Set stamp placement with Stamp::SetOrigin().
  6. Set the image stamp dimensions with Stamp::SetImageSize().
  7. Apply the configured stamp with PdfFileStamp::AddStamp().
  8. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/Facades/Stamp.h>
#include <system/array.h>
#include <system/string.h>

namespace Facades::Examples::PDF_Stamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using Stamp = Aspose::Pdf::Facades::Stamp;

void AddStampToSpecificPages(String inputFile, String imageFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    auto stamp = MakeObject<Stamp>();
    stamp->BindImage(imageFile);
    stamp->set_Pages(MakeArray<int32_t>({1}));
    stamp->SetOrigin(400, 40);
    stamp->SetImageSize(120, 60);
    pdfStamper->AddStamp(stamp);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_Stamp

Add text stamp with text state

Bind formatted approval text and a TextState to a stamp and apply it at (36, 700) with 15-degree rotation.

  1. Create the text content and formatting with FormattedText.
  2. Configure text appearance through TextState.
  3. Create PdfFileStamp and Stamp.
  4. Bind the PDF input with Facade::BindPdf().
  5. Bind the formatted stamp text with Stamp::BindLogo().
  6. Set stamp text styling with Stamp::BindTextState().
  7. Set stamp placement with Stamp::SetOrigin().
  8. Set rotation through Stamp using the example values.
  9. Apply the configured stamp with PdfFileStamp::AddStamp().
  10. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Facades/EncodingType.h>
#include <Aspose.PDF.Cpp/Facades/FontStyle.h>
#include <Aspose.PDF.Cpp/Facades/FormattedText.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/Facades/Stamp.h>
#include <Aspose.PDF.Cpp/Text/FontStyles.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>
#include <drawing/color.h>
#include <system/string.h>

namespace Facades::Examples::PDF_Stamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using namespace Aspose::Pdf::Text;
using Stamp = Aspose::Pdf::Facades::Stamp;

void AddTextStampWithTextState(String inputFile, String outputFile)
{
    auto text = MakeObject<FormattedText>(u"Approved by signing workflow", System::Drawing::Color::get_Blue(),
                                          System::Drawing::Color::get_LightGray(), FontStyle::HelveticaBold,
                                          EncodingType::Winansi, true, 14);
    auto textState = MakeObject<TextState>();
    textState->set_ForegroundColor(Aspose::Pdf::Color::get_DarkBlue());
    textState->set_FontSize(16);
    textState->set_FontStyle(FontStyles::Bold);

    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    auto stamp = MakeObject<Stamp>();
    stamp->BindLogo(text);
    stamp->BindTextState(textState);
    stamp->SetOrigin(36, 700);
    stamp->set_Rotation(15.0f);
    pdfStamper->AddStamp(stamp);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_Stamp