Knowledgebase (2311)
Children categories
Images play a vital role in various documents. They are helpful in conveying complex information that is difficult to present in plain text and making documents more visually appealing. In this article, we will focus on how to insert, replace or delete images in PDF documents in Java using Spire.PDF for Java.
- Insert an Image into a PDF Document
- Replace an Image with Another Image in a PDF Document
- Delete a Specific Image in a PDF Document
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.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.pdf</artifactId>
<version>11.12.16</version>
</dependency>
</dependencies>
Insert an Image into a PDF Document in Java
The following steps demonstrate how to insert an image into an existing PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page in the PDF document using PdfDocument.getPages().get() method.
- Load an image using PdfImage.fromFile() method.
- Specify the width and height of the image area on the page.
- Specify the X and Y coordinates to start drawing the image.
- Draw the image on the page using PdfPageBase.getCanvas().drawImage() method.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
public class AddImage {
public static void main(String []args){
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("Input.pdf");
//Get the first page in the PDF document
PdfPageBase page = pdf.getPages().get(0);
//Load an image
PdfImage image = PdfImage.fromFile("image.jpg");
//Specify the width and height of the image area on the page
float width = image.getWidth() * 0.50f;
float height = image.getHeight() * 0.50f;
//Specify the X and Y coordinates to start drawing the image
float x = 100f;
float y = 60f;
//Draw the image at a specified location on the page
page.getCanvas().drawImage(image, x, y, width, height);
//Save the result document
pdf.saveToFile("AddImage.pdf", FileFormat.PDF);
}
}

Replace an Image with Another Image in a PDF Document in Java
The following steps demonstrate how to replace an image with another image in a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page in the PDF document using PdfDocument.getPages().get() method.
- Load an image using PdfImage.fromFile() method.
- Create the PdfImageHelper class.
- Use the PdfImageHelper. getImagesInfo() method to obtain a collection of all images on the page.
- Use the PdfImageHelper. replaceImage() method to replace the specified index of images in the collection.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.utilities.PdfImageHelper;
import com.spire.pdf.utilities.PdfImageInfo;
public class ReplaceImage
{
public static void main(String []args)
{
//Create a PdfDocument instance
PdfDocument doc = new PdfDocument();
//Load a PDF document
doc.loadFromFile("AddImage.pdf");
//Get the first page
PdfPageBase page = doc.getPages().get(0);
//Load an image
PdfImage image = PdfImage.fromFile("image1.jpg");
// Get the image information from the page
PdfImageHelper imageHelper = new PdfImageHelper();
PdfImageInfo[] imageInfos = imageHelper.getImagesInfo(page);
// Replace Image
imageHelper.replaceImage(imageInfos[0], image);
//Save the result document
doc.saveToFile("ReplaceImage.pdf", FileFormat.PDF);
//Dispose the document
doc.dispose();
}
}

Delete a Specific Image in a PDF Document in Java
The following steps demonstrate how to delete an image from a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page in the PDF document using PdfDocument.getPages().get() method.
- Create the PdfImageHelper class.
- Use the PdfImageHelper. getImagesInfo() method to obtain a collection of all images on the page.
- Use the PdfImageHelper. deleteImage() method to delete specific images on the page.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*;
import com.spire.pdf.utilities.PdfImageHelper;
import com.spire.pdf.utilities.PdfImageInfo;
public class DeleteImage
{
public static void main(String []args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("AddImage.pdf");
//Get the first page
PdfPageBase page = pdf.getPages().get(0);
// Get the image information from the page
PdfImageHelper imageHelper = new PdfImageHelper();
PdfImageInfo[] imageInfos = imageHelper.getImagesInfo(page);
//Delete the first image on the page
imageHelper.deleteImage(imageInfos[0]);
//Save the result document
pdf.saveToFile("DeleteImage.pdf", FileFormat.PDF);
//Dispose the document
pdf.dispose();
}
}

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.
It is not uncommon at work that we may receive two versions of a Word document and face the need to find the differences between them. Document comparison is particularly important and popular in the fields of laws, regulations and education. In this article, you will learn how to compare two Word documents in C# and VB.NET by using Spire.Doc for .NET.
- Compare Two Documents and Save Result in a Third Word Document
- Compare Two Documents and Return Insertions and Deletions in Lists
Below is a screenshot of the two Word documents that’ll be compared.

Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Compare Two Documents and Save Result in a Third Word Document
Saving the comparison result in a separate Word document allows us to see all the changes made to the original document, including insertions, deletions as well as modifications on formatting. The following are the steps to compare two documents and save the result in a third Word document using Spire.Doc for .NET.
- Load two Word documents separately while initialing the Document objects.
- Compare these two documents using Document.Compare() method.
- Save the result in a third Word document using ;Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
namespace CompareDocuments
{
class Program
{
static void Main(string[] args)
{
//Load one Word document
Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");
//Load the other Word document
Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");
//Compare two documents
doc1.Compare(doc2, "John");
//Save the differences in a third document
doc1.SaveToFile("Differences.docx", FileFormat.Docx2013);
doc1.Dispose();
}
}
}

Compare Two Documents and Return Insertions and Deletions in Lists
Developers may only want to obtain the insertions and deletions instead of the whole differences. The following are the steps to get insertions and deletions in two separate lists.
- Load two Word documents separately while initialing the Document objects.
- Compare two documents using Document.Compare() method.
- Get the revisions using the constructor function of the DifferRevisions ;class.
- Get a list of insertions through DifferRevisions.InsertRevisions property.
- Get a list of deletions through DifferRevisions.DeleteRevisions property.
- Loop through the elements in the two lists to get the specific insertion and deletion.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System;
namespace GetDifferencesInList
{
class Program
{
static void Main(string[] args)
{
//Load one Word document
Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");
//Load the other Word document
Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");
//Compare the two Word documents
doc1.Compare(doc2, "Author");
//Get the revisions
DifferRevisions differRevisions = new DifferRevisions(doc1);
//Return the insertion revisions in a list
var insetRevisionsList = differRevisions.InsertRevisions;
//Return the deletion revisions in a list
var deletRevisionsList = differRevisions.DeleteRevisions;
//Create two int variables
int m = 0;
int n = 0;
//Loop through the insertion revision list
for (int i = 0; i < insetRevisionsList.Count; i++)
{
if (insetRevisionsList[i] is TextRange)
{
m += 1;
//Get the specific revision and get its content
TextRange textRange = insetRevisionsList[i] as TextRange;
Console.WriteLine("Insertion #" + m + ":" + textRange.Text);
}
}
Console.WriteLine("=====================");
//Loop through the deletion revision list
for (int i = 0; i < deletRevisionsList.Count; i++)
{
if (deletRevisionsList[i] is TextRange)
{
n += 1;
//Get the specific revision and get its content
TextRange textRange = deletRevisionsList[i] as TextRange;
Console.WriteLine("Deletion #" + n + ":" + textRange.Text);
}
}
Console.ReadKey();
}
}
}

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.
Java: Change the Color or Remove the Underline of Hyperlinks in Word
2022-08-04 07:49:00 Written by KoohjiHyperlinks in Word documents are usually displayed blue with an underline. Actually, if you don't like the default style of hyperlinks, you can change the color of hyperlinks or remove the underline to make your own hyperlink style while keeping the link. This article shows how to change the color or remove the underline of hyperlinks in Word by programming using Spire.Doc for 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.1.3</version>
</dependency>
</dependencies>
Change the Color of a Hyperlink or Remove the Underline of a Hyperlink in a Word Document
The detailed steps are as follows:
- Create an object of Document class.
- Add a section to the document using Document.addSection() method.
- Add a paragraph to the section using Section.addParagraph() method.
- Insert a normal hyperlink using Paragraph.appendHyperlink() method.
- Add a paragraph and a hyperlink, and then change the color of the hyperlink text to red using TextRange.getCharacterFormat().setTextColor() method.
- Add a paragraph and a hyperlink, and then remove the underline of the hyperlink using TextRange.getCharacterFormat().setUnderlineStyle() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class ChangeHyperlink {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Add a paragraph
Paragraph para= section.addParagraph();
para.appendText("A regular hyperlink: ");
//Insert a hyperlink
TextRange textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
para.appendBreak(BreakType.Line_Break);
//Add a paragraph
para = section.addParagraph();
para.appendText("Change the color of the hyperlink: ");
//Insert a hyperlink
textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
//Change the color of the hyperlink to red
textRange.getCharacterFormat().setTextColor(Color.RED);
para.appendBreak(BreakType.Line_Break);
//Add a paragraph
para = section.addParagraph();
para.appendText("Remove the underline of the hyperlink: ");
//Insert a hyperlink
textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
//Remove the underline of the hyperlink
textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
//Save the document
document.saveToFile("ChangeHyperlink.docx", FileFormat.Docx_2013);
}
}

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.