Add images to PDF in C++
Contents
[
Hide
]
Create PDFs containing images using a page rectangle, drawing operators, Flate compression, or alternative text with Aspose.PDF for C++.
Add an image to a new page
In these four examples, inputFile is an image file and the output is a newly created PDF.
- Create a Document and add a page.
- Define a Rectangle with coordinates (20, 730, 120, 830).
- Call AddImage() on the Page with the input image path and rectangle.
- Save the PDF with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
using namespace System;
using namespace Aspose::Pdf;
void ImageExamples::AddImage(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
auto page = document->get_Pages()->Add();
page->AddImage(inputFile, MakeObject<Rectangle>(20, 730, 120, 830));
document->Save(outputFile);
}
Draw an image with PDF operators
The matrix scales the image to the page width and centers it vertically. A tall image can extend beyond the page bounds.
- Create a Document and open the input image stream.
- Add a page and set its size to 842 by 595 points.
- Add the stream to XImageCollection and retain the returned resource name.
- Calculate a page-width image height from the XImage pixel aspect ratio.
- Add GSave, ConcatenateMatrix, Do, and GRestore operators to the page contents.
- Close the image stream.
- Save the PDF with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Operator.h>
#include <Aspose.PDF.Cpp/OperatorCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Operators;
void ImageExamples::AddUsingOperators(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
auto stream = System::IO::File::OpenRead(inputFile);
auto page = document->get_Pages()->Add();
page->SetPageSize(842, 595);
auto images = page->get_Resources()->get_Images();
auto name = images->Add(stream);
auto image = images->idx_get(images->get_Count());
double width = page->get_MediaBox()->get_Width();
double height = width * image->get_Height() / image->get_Width();
page->get_Contents()->Add(MakeObject<GSave>());
page->get_Contents()->Add(MakeObject<ConcatenateMatrix>(
width, 0, 0, height, 0, (page->get_MediaBox()->get_Height() - height) / 2));
page->get_Contents()->Add(MakeObject<Do>(name));
page->get_Contents()->Add(MakeObject<GRestore>());
stream->Close();
document->Save(outputFile);
}
Add an image with Flate compression
The fixed square matrix can change the displayed aspect ratio of a nonsquare image.
- Create a Document, open the image stream, and add a page.
- Add the image to XImageCollection with ImageFilterType set to Flate.
- Add GSave and a ConcatenateMatrix scaling the image to 600 by 600 points.
- Draw the named resource with Do and restore the graphics state with GRestore.
- Close the image stream.
- Save the PDF with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/ImageFilterType.h>
#include <Aspose.PDF.Cpp/Operator.h>
#include <Aspose.PDF.Cpp/OperatorCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Operators;
void ImageExamples::AddWithFlateCompression(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
auto stream = System::IO::File::OpenRead(inputFile);
auto page = document->get_Pages()->Add();
auto name = page->get_Resources()->get_Images()->Add(stream, ImageFilterType::Flate);
page->get_Contents()->Add(MakeObject<GSave>());
page->get_Contents()->Add(MakeObject<ConcatenateMatrix>(600, 0, 0, 600, 0, 0));
page->get_Contents()->Add(MakeObject<Do>(name));
page->get_Contents()->Add(MakeObject<GRestore>());
stream->Close();
document->Save(outputFile);
}
Add alternative text to an image
The example attempts to set alternative text and reports whether it succeeded. It does not validate PDF/UA compliance. Use an SDK version that exposes TrySetAlternativeText().
- Create a Document and add a page sized 842 by 595 points.
- Call AddImage() on the Page using a rectangle that covers the page.
- Select the first XImage resource and call TrySetAlternativeText() with the description and page.
- Print the returned success flag.
- Save the PDF with Save().
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
void ImageExamples::AddWithAltText(String inputFile, String outputFile)
{
auto document = MakeObject<Document>();
auto page = document->get_Pages()->Add();
page->SetPageSize(842, 595);
page->AddImage(inputFile, MakeObject<Rectangle>(0, 0, 842, 595));
bool added = page->get_Resources()->get_Images()->idx_get(1)->TrySetAlternativeText(
u"Alternative text for image", page);
std::cout << "Alternative text added: " << added << std::endl;
document->Save(outputFile);
}