Knowledgebase (2300)
Spire.Doc load the word document with macros, it also supports to detect if a Word document contains VBA macros and remove all the VBA macros from a word document. This article demonstrates how to detect and remove VBA macros from Word document in Java applications.
Firstly, please view the sample document with macros:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class RemoveMacro {
public static void main(String[] args) throws Exception {
//Load the Sample Word document.
Document doc = new Document();
doc.loadFromFile("VBAMacros.docm");
//If the document contains Macros, remove them from the document.
if (doc.isContainMacro() )
{
doc.clearMacros();
}
//save to file
doc.saveToFile("output/RemoveMacro.docm", FileFormat.Docm);
}
}
Effective screenshot after clear the VBA macros from word document:

Word documents often contain images, which are useful elements to enhance the aesthetics of the document. If the overall design of the document changes, the existing images might no longer match the new style. Replacing them with updated images can help maintain a consistent look. In this article, you will learn how to replace images in Word in Java using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, 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>13.11.2</version>
</dependency>
</dependencies>
Replace Images with New Images in Word in Java
To replace an image in a Word document with another image, you need to loop through the elements of the document, find the images and add them to a list, then get the image that you want to replace from the list and call the DocPicture.loadImage() method to replace it with the loaded image. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Create an ArrayList instance.
- Iterate through all sections in the document.
- Iterate through all paragraphs in each section.
- Iterate through all child objects in each paragraph.
- Find the images and add them to the list.
- Get a specific image from the list and replace it with another image using DocPicture.loadImage() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.ArrayList;
public class ReplaceImages {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("E:\\PythonDoc\\WordImage.docx");
//Create an ArrayList instance
ArrayList<DocumentObject> pictures = new ArrayList();
//Iterate through all sections in the document
for (Section sec : (Iterable<Section>) doc.getSections()
) {
//Iterate through all paragraphs in each section
for (Paragraph para : (Iterable<Paragraph>) sec.getParagraphs()
) {
//Iterate through all child objects in each paragraph
for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects()
) {
//Find the images and add them to the list
if (obj.getDocumentObjectType() == DocumentObjectType.Picture) {
pictures.add(obj);
}
}
}
}
//Replace the first picture in the list with a new image
DocPicture picture = (DocPicture)pictures.get(0) ;
picture.loadImage("pic.png");
//Save the result document
doc.saveToFile("ReplaceWithNewImage.docx", FileFormat.Docx);
}
}

Replace Image with Text in Word in Java
Spire.Doc for Java doesn't provide a direct method to replace image with text, but you can achieve this task by inserting the text at the image location and then removing the image from the document.
The following steps demonstrate how to replace all images in a Word document with text:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Create an ArrayList instance.
- Iterate through all sections in the document.
- Iterate through all paragraphs in each section.
- Iterate through all child objects in each paragraph.
- Find the images and add them to the list.
- Iterate through the images in the list.
- Get the index of the image in the paragraph using Paragraph.getChildObjects().indexOf() method.
- Create a TextRange instance and set text for the text range through TextRange.setText() method.
- Insert the text range at the image location using Paragraph.getChildObjects().insert() method.
- Remove the image from the paragraph using Paragraph.getChildObjects().remove() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.ArrayList;
public class replaceImageWithText {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("WordImage.docx");
//Create an ArrayList instance
ArrayList<DocumentObject> pictures = new ArrayList();
int j = 1;
//Iterate through all sections in the document
for (Section sec : (Iterable<Section>) doc.getSections()
) {
//Iterate through all paragraphs in each section
for (Paragraph para : (Iterable<Paragraph>) sec.getParagraphs()
) {
//Iterate through all child objects in each paragraph
for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects()
) {
//Find the images and add them to the list
if (obj.getDocumentObjectType() == DocumentObjectType.Picture) {
pictures.add(obj);
}
}
//Iterate through all images in the list and replace them with text "Here was image {image index}"
for (DocumentObject pic : pictures)
{
int index = para.getChildObjects().indexOf(pic);
TextRange range = new TextRange(doc);
range.setText(String.format("Here was image-%d", j));
para.getChildObjects().insert(index, range);
para.getChildObjects().remove(pic);
j++;
}
}
}
//Save the result document
doc.saveToFile("ReplaceImageWithText.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.
How to Mannually Add Spire.PDF as Dependency in a .NET Standard Library Project
2019-10-30 03:43:03 Written by KoohjiStep 1: Download the latest version of Spire.PDF Pack from the link below, unzip it, and you'll get the DLL files for .NET Standarad from the "netstandard2.0" folder. If you already have this folder in your disk, go straight to step two.

Step 2: Create a .NET Standard library project in your Visual Studio.

Step 3: Add all DLL files under the "netstandard2.0" folder as dependencies in your project.
Right-click "Dependencies" – select "Add Reference" – click "Browse" – selcet all DLLs under "netstandard2.0" folder – click "Add".

Step 4: Install the other five packages in your project via the NuGet Package Manager. They are SkiaSharp, System.Buffers, System.Memory, System.Text.Encoding.CodePages and System.Runtime.CompilerServices.Unsafe.
Right-click "Dependencies" – select "Manage NuGet Packages" – click "Browse" –type the package name – select the package from the search results – click "Install".

Step 5: Now that you've added all the dependences successfully, you can start to write your own .NET Standard library that is capable of creating and processing PDF documents.
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace SpirePdfStandard
{
public class Class1
{
public void CreatePdf()
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.Pages.Add();
//Draw text on the page at the specified position
page.Canvas.DrawString("Hello World",
new PdfFont(PdfFontFamily.Helvetica, 13f),
new PdfSolidBrush(Color.Black),
new PointF(50, 50));
//Save the document
doc.SaveToFile("Output.pdf");
}
}
}