Add image stamps to PDF in C++
Contents
[
Hide
]
Add image stamps with position, size, rotation, opacity, or quality settings and add a floating-box background image using Aspose.PDF for C++.
Add a positioned image stamp
- Open the input PDF as a Document.
- Create an ImageStamp from imageFile.
- Set background mode, position, dimensions, rotation, and opacity.
- Call AddStamp() on page 1.
- Save the result with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/ImageStamp.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rotation.h>
using namespace System;
using namespace Aspose::Pdf;
// Port of StampingExamples.addImageStamp.
void AddImageStamp(System::String inputFile, System::String imageFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto imageStamp = MakeObject<ImageStamp>(imageFile);
imageStamp->set_Background(true);
imageStamp->set_XIndent(100);
imageStamp->set_YIndent(100);
imageStamp->set_Height(300);
imageStamp->set_Width(300);
imageStamp->set_Rotate(Rotation::on270);
imageStamp->set_Opacity(0.5);
document->get_Pages()->idx_get(1)->AddStamp(imageStamp);
document->Save(outputFile);
}
Set image stamp quality
The low quality setting can affect image appearance. Review the saved output.
- Open the input PDF as a Document.
- Create an ImageStamp from imageFile.
- Set Quality to 10.
- Call AddStamp() on page 1.
- Save the result with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/ImageStamp.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
// Port of StampingExamples.addImageStampWithQualityControl.
void AddImageStampWithQualityControl(System::String inputFile, System::String imageFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto imageStamp = MakeObject<ImageStamp>(imageFile);
imageStamp->set_Quality(10);
document->get_Pages()->idx_get(1)->AddStamp(imageStamp);
document->Save(outputFile);
}
Add a floating-box background image
This adds a paragraph-based floating box rather than calling AddStamp().
- Open the input PDF as a Document.
- Select page 1 and create a FloatingBox sized 200 by 100.
- Configure position, alignment, text, and border.
- Create an Image and assign it as BackgroundImage.
- Set the background color and add the box to the page paragraphs.
- Save the result with Save().
#include <Aspose.PDF.Cpp/Generator/BorderInfo.h>
#include <Aspose.PDF.Cpp/Generator/BorderSide.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Generator/FloatingBox.h>
#include <Aspose.PDF.Cpp/Text/Font.h>
#include <Aspose.PDF.Cpp/HorizontalAlignment.h>
#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;
// Port of StampingExamples.addImageAsBackgroundInFloatingBox.
void AddImageAsBackgroundInFloatingBox(System::String inputFile, System::String imageFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto page = document->get_Pages()->idx_get(1);
auto box = MakeObject<FloatingBox>(200.0f, 100.0f);
box->set_Left(40);
box->set_Top(80);
box->set_HorizontalAlignment(HorizontalAlignment::Center);
box->get_Paragraphs()->Add(MakeObject<TextFragment>(u"Text in Floating Box"));
box->set_Border(MakeObject<BorderInfo>(BorderSide::All, Color::get_Red()));
auto image = MakeObject<Image>();
image->set_File(imageFile);
box->set_BackgroundImage(image);
box->set_BackgroundColor(Color::get_Yellow());
page->get_Paragraphs()->Add(box);
document->Save(outputFile);
}