Add text and images to PDF pages in C++
Use PdfFileMend to place images and formatted text on PDF pages and configure compositing or word wrapping.
In AddImage01 through AddImage04, inputFile is an image path and the function creates a new PDF page. Text examples accept a PDF path. AddText03 appends three pages before adding text to pages 1 and 3.
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.
PdfFileMend workflows
- Add an image to a new PDF
- Add an image with Multiply blending
- Add an image with Exclusion blending and Flate filtering
- Add an image with explicit compositing options
- Add formatted text to a page
- Set word wrapping for formatted text
- Add text to multiple pages
Add an image to a new PDF
Create a new one-page PDF and add inputFile as an image in the rectangle (10, 650, 110, 750).
- Create PdfFileMend.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Place the image with PdfFileMend::AddImage().
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddImage01(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
document->get_Pages()->Add();
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(document);
mender->AddImage(inputFile, 1, 10, 650, 110, 750);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend
Add an image with Multiply blending
Place an image on a new page with Multiply compositing.
- Configure image blending and filtering with CompositingParameters.
- Create PdfFileMend.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Place the image with PdfFileMend::AddImage().
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/BlendMode.h>
#include <Aspose.PDF.Cpp/CompositingParameters.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddImage02(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
document->get_Pages()->Add();
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(document);
int pageNum = 1;
int lowerLeftX = 10;
int lowerLeftY = 650;
int upperRightX = 110;
int upperRightY = 750;
auto compositingParameters = MakeObject<CompositingParameters>(BlendMode::Multiply);
mender->AddImage(inputFile, pageNum, lowerLeftX, lowerLeftY, upperRightX, upperRightY, compositingParameters);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend
Add an image with Exclusion blending and Flate filtering
Place an image on a new page with Exclusion blending and Flate filtering.
- Configure image blending and filtering with CompositingParameters.
- Create PdfFileMend.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Place the image with PdfFileMend::AddImage().
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/BlendMode.h>
#include <Aspose.PDF.Cpp/CompositingParameters.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <Aspose.PDF.Cpp/ImageFilterType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddImage03(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
document->get_Pages()->Add();
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(document);
int pageNum = 1;
int lowerLeftX = 10;
int lowerLeftY = 650;
int upperRightX = 110;
int upperRightY = 750;
auto compositingParameters = MakeObject<CompositingParameters>(BlendMode::Exclusion, ImageFilterType::Flate);
mender->AddImage(inputFile, pageNum, lowerLeftX, lowerLeftY, upperRightX, upperRightY, compositingParameters);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend
Add an image with explicit compositing options
Place an image with Multiply blending, Flate filtering, and the third compositing option set to false.
- Configure image blending and filtering with CompositingParameters.
- Create PdfFileMend.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Place the image with PdfFileMend::AddImage().
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/BlendMode.h>
#include <Aspose.PDF.Cpp/CompositingParameters.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <Aspose.PDF.Cpp/ImageFilterType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddImage04(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
document->get_Pages()->Add();
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(document);
int pageNum = 1;
int lowerLeftX = 10;
int lowerLeftY = 650;
int upperRightX = 110;
int upperRightY = 750;
auto compositingParameters = MakeObject<CompositingParameters>(BlendMode::Multiply, ImageFilterType::Flate, false);
mender->AddImage(inputFile, pageNum, lowerLeftX, lowerLeftY, upperRightX, upperRightY, compositingParameters);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend
Add formatted text to a page
Add Welcome to Aspose! to page 1 at (10, 750).
- Create the text content and formatting with FormattedText.
- Create PdfFileMend.
- Bind the PDF input with Facade::BindPdf().
- Place formatted text with PdfFileMend::AddText().
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/Facades/FormattedText.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddText01(String inputFile, String outputFile)
{
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(inputFile);
auto message = MakeObject<FormattedText>(u"Welcome to Aspose!");
mender->AddText(message, 1, 10, 750);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend
Set word wrapping for formatted text
Add formatted text to page 1 and set WrapMode to ByWords before saving.
- Create the text content and formatting with FormattedText.
- Create PdfFileMend.
- Bind the PDF input with Facade::BindPdf().
- Place formatted text with PdfFileMend::AddText().
- Set wrap mode through PdfFileMend using the example values.
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/Facades/FormattedText.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <Aspose.PDF.Cpp/Facades/WordWrapMode.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddText02(String inputFile, String outputFile)
{
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(inputFile);
auto message = MakeObject<FormattedText>(u"Welcome to Aspose!");
mender->AddText(message, 1, 10, 750);
mender->set_WrapMode(WordWrapMode::ByWords);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend
Add text to multiple pages
Append three pages to the input PDF and add formatted text to pages 1 and 3 within the specified rectangle.
- Create the text content and formatting with FormattedText.
- Create PdfFileMend.
- Create or open the Document and prepare its pages as shown.
- Bind the PDF input with Facade::BindPdf().
- Place formatted text with PdfFileMend::AddText().
- Save the resulting PDF with PdfFileMend::
Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/FormattedText.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileMend.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <system/array.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileMend
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddText03(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->get_Pages()->Add();
document->get_Pages()->Add();
document->get_Pages()->Add();
auto mender = MakeObject<PdfFileMend>();
mender->BindPdf(document);
auto message = MakeObject<FormattedText>(u"Welcome to Aspose!");
auto pageNums = MakeArray<int32_t>({1, 3});
float llx = 10;
float lly = 750;
float urx = 310;
float ury = 760;
mender->AddText(message, pageNums, llx, lly, urx, ury);
mender->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileMend