Hyperlinks in PDF documents provide convenient navigation and access to external resources. However, there are scenarios where you may need to remove or update these hyperlinks to maintain document integrity or reflect changes in the linked content. In this article, we will explore how to efficiently update or remove hyperlinks in PDF using Spire.PDF for Java.
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>10.10.7</version> </dependency> </dependencies>
Update Hyperlinks in PDF in Java
Spire.PDF for Java provides the PdfUriAnnotationWidget.setUri() method to assist you in resetting the URL. The following are the detailed steps.
- Create a PdfDocument object.
- Load the sample PDF file using PdfDocument.loadFromFile() method.
- Get the first page of the document using PdfDocument.getPages().get() method.
- Get annotation collection using PdfPageBase.getAnnotationsWidget() method.
- Get the first url annotation widget using PdfAnnotationCollection.get() method and reset the url using PdfUriAnnotationWidget.setUri() method.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.annotations.PdfAnnotationCollection; import com.spire.pdf.annotations.PdfUriAnnotationWidget; public class UpdateHyperlinks { public static void main(String[] args) throws Exception { //Create a PdfDocument object PdfDocument document = new PdfDocument(); //Load the sample PDF file document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf"); //Get the first page PdfPageBase page = document.getPages().get(0); //Get annotation collection PdfAnnotationCollection widgetCollection = page.getAnnotationsWidget(); //Get the first url annotation widget PdfUriAnnotationWidget uriWidget = (PdfUriAnnotationWidget)widgetCollection.get(0); //Reset the url uriWidget.setUri("https://www.e-iceblue.com/Introduce/pdf-for-java.html"); //Save to file document.saveToFile("output.pdf"); document.close(); } }
Remove Hyperlinks in PDF in Java
Spire.PDF for Java also offers the PdfAnnotationCollection.removeAt() method, which allows users to delete hyperlinks. When you use this method to remove a hyperlink, the visual appearance of the hyperlink will remain unchanged, but the underlying link will be deleted. The following steps demonstrate how to delete hyperlinks in a PDF.
- Create a PdfDocument object.
- Load the sample PDF file using PdfDocument.loadFromFile() method.
- Get the first page of the document using PdfDocument.getPages().get() method.
- Get annotation collection using PdfPageBase.getAnnotationsWidget() method.
- Remove the second annotation which is a hyperlink in the document using PdfAnnotationCollection.removeAt() method.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.annotations.PdfAnnotationCollection; public class RemoveHyperlinks { public static void main(String[] args) throws Exception { //Create a PdfDocument object PdfDocument document = new PdfDocument(); //Load the sample PDF file document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf"); //Get the first page PdfPageBase page = document.getPages().get(0); //Get annotation collection PdfAnnotationCollection widgetCollection = page.getAnnotationsWidget(); //Remove the second annotation which is a hyperlink in the document widgetCollection.removeAt(1); //Save to file document.saveToFile("Result.pdf"); document.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.