This short article describes how to remove hyperlinks from Word using Java. It has IDE setup details, a list of instructions to develop the application, and sample code demonstrating how to remove links from Word document using Java. A few enhancements are also shared in this article to customize the output Word file if required.
Steps to Delete Hyperlinks in Word using Java
- Set up the IDE for using Aspose.Words for Java to remove hyperlinks
- Load the source Word file containing various hyperlinks into the Document object
- List all the hyperlinks in the document, excluding those in the table of contents if required
- Store the visible text of each hyperlink before removing the hyperlinks
- Remove each hyperlink while keeping its displayed text
- Remove the hyperlink formatting from the text of each removed hyperlink
- Parse the document and remove the hyperlink style formatting from any remaining text
- Save the updated Word file that has plain text instead of hyperlinks
These steps summarize how to remove all hyperlinks in Word using Java. Load the source Word file with hyperlinks, collect all the hyperlinks, optionally skipping those in the table of contents, store the text displayed by each hyperlink, and remove all the hyperlinks from the document. Set the actual text in place of each hyperlink, remove the hyperlink-related styling from that text, remove similar styling from the rest of the text in the document, and save the Word file without hyperlinks in it.
Code to Remove Hyperlinks in Word Document using Java
This code demonstrates how to remove all hyperlinks in Word at once using Java. Note that the current loop selects only the FieldHyperlink fields, so if you want to remove links attached to images and shapes, clear the HRef property of each of them. If you want to handle the internal and external links separately, use the getSubAddress() and getAddress() methods of FieldHyperlink, respectively.
This article shows the process of working with hyperlinks in a Word file. To work with document revisions and accept all of them at once, refer to the article Accept all changes in Word using Java.