Knowledgebase (2330)
Children categories
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
std::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
std::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.
PowerPoint is a popular format used to create presentations, training materials, business reports, etc. However, it has certain disadvantages, such as not being secure enough or having possible compatibility issues on different devices/software. While XPS, also known as XML Paper Specification, is a more secure and compatible file format suitable for high quality printing and fast transfer. As a result, there are many people who may choose to convert their PowerPoint files to XPS format. In this article, you will learn how to programmatically convert a PowerPoint Presentation to XPS using Spire.Presentation for C++.
Install Spire.Presentation for C++
There are two ways to integrate Spire.Presentation 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.Presentation for C++ in a C++ Application
Convert PowerPoint to XPS in C++
It's fairly simple to convert a PowerPoint presentation to an XPS file using Spire.Presentation for C++. You just need to load a sample PowerPoint document and then save it as XPS format using Workbook->SaveToFile(LPCWSTR_S fileName, FileFormat::XPS) method. The following are the detailed steps.
- Create a Presentation instance.
- Load a PowerPoint document using Presentation->LoadFromFile() method.
- Save the document to an XPS file using Presentation->SaveToFile(LPCWSTR_S fileName, FileFormat::XPS) method.
- C++
#include "Spire.Presentation.o.h"
using namespace std;
using namespace Spire::Presentation;
int main()
{
//Specify the input and output file paths
std::wstring inputFile = L"Data\\input.pptx";
std::wstring outputFile = L"ToXPS.xps";
//Create a Presentation instance
intrusive_ptr<Presentation> presentation = new Presentation();
//Load a sample PowerPoint document
presentation->LoadFromFile(inputFile.c_str());
//Save the document as an XPS file
presentation->SaveToFile(outputFile.c_str(), FileFormat::XPS);
}

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.
A list is an effective way to organize information and present it clearly and logically. The elements in the list stand out from other parts of the text, encouraging readers to pay attention to them. Depending on your requirements, you can add numbered lists, bulleted lists or multi-level lists to your Word documents. This article demonstrates how to create these types of lists in a Word document in Java using Spire.Doc for Java.
- Insert a Numbered List in Word in Java
- Insert a Bulleted List in Word in Java
- Insert a Multi-Level Numbered List in Word in Java
- Insert a Multi-Level Mixed-Type List in Word in Java
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.4.9</version>
</dependency>
</dependencies>
Insert a Numbered List in Word in Java
Spire.Doc for Java offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph.getListFormat().applyStyle() method. The steps to create a numbered list are as follows.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list using ListStyle.getLevels().get(index) method, and set the numbering type using ListLevel.setPatternType() method.
- Add the list style to the document using Document.getListStyles().add() method.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.getListFormat().applyStyle() method.
- Specify the list level using Paragraph.getListFormat().setListLevelNumber() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.collections.ListLevelCollection;
public class InsertNumberedList {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Create a numbered list style
ListStyle listStyle =document.getStyles().add(ListType.Numbered, "numberList");
ListLevelCollection levels = listStyle.getListRef().getLevels();
levels.get(0).setPatternType(ListPatternType.Decimal_Half_Width);
levels.get(0).setTextPosition(20);
//Add a paragraph
Paragraph paragraph = section.addParagraph();
paragraph.appendText("Required Web Development Skills:");
paragraph.getFormat().setAfterSpacing(5);
//Add a paragraph and apply the numbered list style to it
paragraph = section.addParagraph();
paragraph.appendText("HTML");
paragraph.getListFormat().applyStyle("numberedList");
paragraph.getListFormat().setListLevelNumber(0);
//Add another four paragraphs and apply the numbered list style to them
paragraph = section.addParagraph();
paragraph.appendText("CSS");
paragraph.getListFormat().applyStyle("numberedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("JavaScript");
paragraph.getListFormat().applyStyle("numberedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("Python");
paragraph.getListFormat().applyStyle("numberedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("MySQL");
paragraph.getListFormat().applyStyle("numberedList");
paragraph.getListFormat().setListLevelNumber(0);
//Save the document to file
document.saveToFile("output/NumberedList.docx", FileFormat.Docx);
}
}

Insert a Bulleted List in Word in Java
The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create an instance of ListStyle class, specifying the list type to Bulleted.
- Get a specific level of the list using ListStyle.getLevels().get(index) method, and set the bullet symbol using ListLevel.setBulletCharacter() method.
- Add the list style to the document using Document.getListStyles().add() method.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.getListFormat().applyStyle() method.
- Specify the list level using Paragraph.getListFormat().setListLevelNumber() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.collections.ListLevelCollection;
public class InsertBulletedList {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Create a bulleted list style
ListStyle numberList =document.getStyles().add(ListType.Bulleted, "bulletedList");
ListLevelCollection Levels = numberList.getListRef().getLevels();
Levels.get(0).setBulletCharacter("\u00B7");
Levels.get(0).getCharacterFormat().setFontName("Symbol");
Levels.get(0).setTextPosition(20);
//Add a paragraph
Paragraph paragraph = section.addParagraph();
paragraph.appendText("Computer Science Subjects:");
paragraph.getFormat().setAfterSpacing(5);
//Add a paragraph and apply the bulleted list style to it
paragraph = section.addParagraph();
paragraph.appendText("Data Structure");
paragraph.getListFormat().applyStyle("bulletedList");
paragraph.getListFormat().setListLevelNumber(0);
//Add another five paragraphs and apply the bulleted list style to them
paragraph = section.addParagraph();
paragraph.appendText("Algorithm");
paragraph.getListFormat().applyStyle("bulletedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("Computer Networks");
paragraph.getListFormat().applyStyle("bulletedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("Operating System");
paragraph.getListFormat().applyStyle("bulletedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("C Programming");
paragraph.getListFormat().applyStyle("bulletedList");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("Theory of Computations");
paragraph.getListFormat().applyStyle("bulletedList");
paragraph.getListFormat().setListLevelNumber(0);
//Save the document to file
document.saveToFile("output/BulletedList.docx", FileFormat.Docx);
}
}

Insert a Multi-Level Numbered List in Word in Java
A multi-level list consists of at least two different levels. Each level of a nested list can be accessed using ListStyle.getLevels().get(index) method. Through ListLevel object, you can set the numbering type and prefix for a certain level. The following are the steps to create a multi-level numbered list in Word.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list using ListStyle.getLevels().get(index) method, and set the numbering type and prefix.
- Add the list style to the document using Document.getListStyles().add() method.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.getListFormat().applyStyle() method.
- Specify the list level using Paragraph.getListFormat().setListLevelNumber() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.collections.ListLevelCollection;
public class InsertMultilevelNumberedList {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Create a numbered list style, specifying number prefix and pattern type of each level
ListStyle listStyle =document.getStyles().add(ListType.Numbered, "nestedStyle");
ListLevelCollection levels = listStyle.getListRef().getLevels();
levels.get(0).setPatternType(ListPatternType.Arabic);
levels.get(0).setTextPosition(20);
levels.get(1).setNumberPrefix("\u0000.");
levels.get(1).setPatternType(ListPatternType.Arabic);
levels.get(2).setNumberPrefix("\u0000.\u0001.");
levels.get(2).setPatternType(ListPatternType.Arabic);
//Add a paragraph
Paragraph paragraph = section.addParagraph();
paragraph.appendText("Here's a Multi-Level Numbered List:");
paragraph.getFormat().setAfterSpacing(5f);
//Add a paragraph and apply the numbered list style to it
paragraph = section.addParagraph();
paragraph.appendText("The first item");
paragraph.getListFormat().applyStyle("nestedStyle");
paragraph.getListFormat().setListLevelNumber(0);
//Add another five paragraphs and apply the numbered list stype to them
paragraph = section.addParagraph();
paragraph.appendText("The second item");
paragraph.getListFormat().applyStyle("nestedStyle");
paragraph.getListFormat().setListLevelNumber(0);
paragraph = section.addParagraph();
paragraph.appendText("The first sub-item");
paragraph.getListFormat().applyStyle("nestedStyle");
paragraph.getListFormat().setListLevelNumber(1);
paragraph = section.addParagraph();
paragraph.appendText("The second sub-item");
paragraph.getListFormat().continueListNumbering();
paragraph.getListFormat().applyStyle("nestedStyle");
paragraph = section.addParagraph();
paragraph.appendText("A sub-sub-item");
paragraph.getListFormat().applyStyle("nestedStyle");
paragraph.getListFormat().setListLevelNumber(2);
paragraph = section.addParagraph();
paragraph.appendText("The third item");
paragraph.getListFormat().applyStyle("nestedStyle");
paragraph.getListFormat().setListLevelNumber(0);
//Save the document to file
document.saveToFile("output/MultilevelNumberedList.docx", FileFormat.Docx);
}
}

Insert a Multi-Level Mixed-Type List in Word in Java
In some cases, you may want to mix number and symbol in a multi-level list. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create a numbered list style and a bulleted list style.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply different list style to different paragraphs using Paragraph.getListFormat().applyStyle() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.collections.ListLevelCollection;
public class InsertMultilevelMixedTypeList {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Create a numbered list style
ListStyle numberedListStyle =document.getStyles().add(ListType.Numbered, "numberedStyle");
ListLevelCollection levels = numberedListStyle.getListRef().getLevels();
levels.get(0).setPatternType(ListPatternType.Arabic);
levels.get(0).setTextPosition(20);
levels.get(1).setPatternType(ListPatternType.Low_Letter);
//Create a bulleted list style
ListStyle bulletedListStyle = document.getStyles().add(ListType.Bulleted,"bulletedStyle");
ListLevelCollection levels_bulletedListStyle = bulletedListStyle.getListRef().getLevels();
levels_bulletedListStyle.get(2).setBulletCharacter("\u002A");
levels_bulletedListStyle.get(2).getCharacterFormat().setFontName("Symbol");
//Add a paragraph
Paragraph paragraph = section.addParagraph();
paragraph.appendText("Here's a Multi-Level Mixed List:");
paragraph.getFormat().setAfterSpacing(5f);
//Add a paragraph and apply the numbered list style to it
paragraph = section.addParagraph();
paragraph.appendText("The first item");
paragraph.getListFormat().applyStyle("numberedStyle");
paragraph.getListFormat().setListLevelNumber(0);
//Add another five paragraphs and apply different list stype to them
paragraph = section.addParagraph();
paragraph.appendText("The first sub-item");
paragraph.getListFormat().applyStyle("numberedStyle");
paragraph.getListFormat().setListLevelNumber(1);
paragraph = section.addParagraph();
paragraph.appendText("The second sub-item");
paragraph.getListFormat().setListLevelNumber(1);
paragraph.getListFormat().applyStyle("numberedStyle");
paragraph = section.addParagraph();
paragraph.appendText("The first sub-sub-item");
paragraph.getListFormat().applyStyle("bulletedStyle");
paragraph.getListFormat().setListLevelNumber(2);
paragraph = section.addParagraph();
paragraph.appendText("The second sub-sub-item");
paragraph.getListFormat().applyStyle("bulletedStyle");
paragraph.getListFormat().setListLevelNumber(2);
paragraph = section.addParagraph();
paragraph.appendText("The second item");
paragraph.getListFormat().applyStyle("numberedStyle");
paragraph.getListFormat().setListLevelNumber(0);
//Save the document to file
document.saveToFile("output/MultilevelMixedList.docx", FileFormat.Docx);
}
}

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.