Inserting images into a document is a great way to enhance its visual appearance and make it more understandable for readers. For example, if you are creating a product guide for a piece of furniture that has complex assembly steps, including images of each step can help users understand how to assemble the product quickly. In this article, you will learn how to insert images into PDF documents along with how to replace and delete images in PDF documents in C++ using Spire.PDF for C++.
- Insert an Image into a PDF Document
- Replace an Image with Another Image in a PDF Document
- Delete an Image from a PDF Document
Install Spire.PDF for C++
There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.PDF for C++ in a C++ Application
Insert an Image into a PDF Document in C++
Spire.PDF for C++ offers the PdfPageBase->GetCanvas()->DrawImage(intrusive_ptr<PdfImage> image, float x, float y, float width, float height) method to add an image to a specific page in a PDF document. The detailed steps are as follows:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument->LoadFromFile(LPCWSTR_S filename) method.
- Get a specific page of the PDF document using the PdfDocument->GetPages()->GetItem(int index) method.
- Initialize an instance of the PdfImage class.
- Load an image using the PdfImage->FromFile(LPCWSTR_S filename) method.
- Draw the image to a specific location on the page using the PdfPageBase->GetCanvas()->DrawImage(intrusive_ptr<PdfImage> image, float x, float y, float width, float height) method.
- Save the result document using the PdfDocument->SaveToFile(LPCWSTR_S filename) method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
int main()
{
//Initialize an instance of the PdfDocument class
intrusive_ptr<PdfDocument> pdf = new PdfDocument();
//Load a PDF file
pdf->LoadFromFile(L"Input.pdf");
//Get the first page of the PDF file
intrusive_ptr<PdfPageBase> page = pdf->GetPages()->GetItem(0);
//Initialize an instance of the PdfImage class
intrusive_ptr<PdfImage> image = new PdfImage();
//Load an image
image = image->FromFile(L"PDF-CPP.png");
float width = image->GetWidth() * 0.5;
float height = image->GetHeight() * 0.5;
float x = (page->GetCanvas()->GetClientSize()->GetWidth() - width) / 2;
//Draw the image to a specific location on the page
page->GetCanvas()->DrawImage(image, x, 120, width, height);
//Save the result file
pdf->SaveToFile(L"AddImage.pdf");
pdf->Close();
}

Replace an Image with Another Image in a PDF Document in C++
You can use the PdfImageHelper->ReplaceImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo,intrusive_ptr<PdfImage>image) method to replace an existing image on a PDF page with another image. The detailed steps are as follows:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument->LoadFromFile(LPCWSTR_S filename) method.
- Get a specific page of the PDF document using the PdfDocument->GetPages()->GetItem(int index) method.
- Create an instance of the PdfImageHelper class.
- Get images of the page using the PdfImageHelper->GetImagesInfo(intrusive_ptr<PdfPageBase> page) method.
- Initialize an instance of the PdfImage class.
- Load an image using the PdfImage->FromFile(LPCWSTR_S filename) method.
- Replace a specific image on the page with the loaded image using the PdfImageHelper->ReplaceImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo,intrusive_ptr<PdfImage>image) method.
- Save the result document using the PdfDocument->SaveToFile(LPCWSTR_S filename) method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
int main()
{
//Initialize an instance of the PdfDocument class
intrusive_ptr<PdfDocument> pdf = new PdfDocument();
//Load a PDF file
pdf-> LoadFromFile(L"AddImage.pdf");
//Get the first page of the PDF file
intrusive_ptr<PdfPageBase> page = pdf-> GetPages()-> GetItem(0);
//Create a PdfImageHelper object
intrusive_ptr<PdfImageHelper> imagehelper = new PdfImageHelper();
//Get image information from the page
vector<intrusive_ptr<Utilities_PdfImageInfo> > imageInfo = imagehelper-> GetImagesInfo(page);
//Initialize an instance of the PdfImage class
intrusive_ptr<PdfImage> image = new PdfImage();
//Load an image
image = image-> FromFile(L"PDF-java.png");
//Replace the first image on the first page
imagehelper-> ReplaceImage(imageInfo[0],image);
//Save the result file
pdf-> SaveToFile(L"ReplaceImage.pdf");
pdf-> Close();
}

Delete an Image from a PDF Document in C++
You can use the PdfImageHelper->DeleteImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo) method to delete a specific image from a PDF page. The detailed steps are as follows:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument->LoadFromFile(LPCWSTR_S filename) method.
- Get a specific page of the PDF document using the PdfDocument->GetPages()->GetItem(int index) method.
- Create an instance of the PdfImageHelper class.
- Get images of the page using the PdfImageHelper->GetImagesInfo(intrusive_ptr<PdfPageBase> page) method.
- Delete a specific image on the page using the PdfImageHelper->DeleteImage(intrusive_ptr<Utilities_PdfImageInfo> imageInfo) method.
- Save the result document using the PdfDocument->SaveToFile(LPCWSTR_S filename) method.
- C++
#include "Spire.Pdf.o.h"
using namespace Spire::Pdf;
int main()
{
//Initialize an instance of the PdfDocument class
intrusive_ptr<PdfDocument> pdf = new PdfDocument();
//Load a PDF file
pdf->LoadFromFile(L"AddImage.pdf");
//Get the first page of the PDF file
intrusive_ptr<PdfPageBase> page = pdf->GetPages()->GetItem(0);
//Create a PdfImageHelper object
intrusive_ptr<PdfImageHelper> imagehelper = new PdfImageHelper();
//Get image information from the page
vector<intrusive_ptr<Utilities_PdfImageInfo>> imageInfo = imagehelper->GetImagesInfo(page);
//Delete the first image on the first page
imagehelper->DeleteImage(imageInfo[0]);
//Save the result file
pdf->SaveToFile(L"DeleteImage.pdf");
pdf->Close();
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
