Get the App
SLTechnology News&Howtos  ›  Development  › 

How Java adds, updates, and removes PDF hyperlinks

Shulou Source: shulou.com Published: 2022-06-03 12:00:18 09月17日 Update

This article mainly explains "Java how to add, update and remove PDF hyperlinks", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to add, update and remove PDF hyperlinks in Java!

profile

PDF hyperlink contains a lot of information with a simple link, which meets the needs of people to render external information without taking up too much space. The following describes how to add, update, and remove hyperlinks in PDFs using Java.

(a) Use of tools:

· Free Spire.PDF for Java 2.4.4 (Free Edition)· Intellij IDEA

(2) Import the Jar package: ·Method 1: First, get the Free Spire.PDF for Java package from the official website.

Step 1: After downloading the control package, decompress it and open the "Project Structure" interface. (Here are three ways to quickly open the Project Structure interface in IDEA, choose any of them)

Step 2: Follow these steps to import.① Select "Modules"-"Dependencies" to add external jar package;② Enter "Attach File or Directories" interface, select jar file path, and then click "OK";③ Check jar path option, click "OK"/"Apply";④ Import completed. As shown below:

·Method 2: Use Maven to configure the guide package. You can refer to the import method.

Java code sample reference

(i) Adding hyperlinks to PDF

Add namespaces:

import com.spire.pdf.*; import com.spire.pdf.annotations.*; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.font.TextAttribute;import java.awt.geom.*; import java.util.HashMap;

1. Add hypertext links

public class TextLink { public static void main (String[] args) throws Exception{ //Create PDF Document PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.getPages().add(); //Initialize X,Y coordinates float y = 30; float x = 0; //Create a plain font PdfTrueTypeFont plainFont = new PdfTrueTypeFont (new Font("Arial Unicode MS", Font.PLAIN,13),true); //Create an underlined font HashMap hm = new HashMap(); hm.put (TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); hm.put (TextAttribute.SIZE, 13); hm.put (TextAttribute.FAMILY, "Arial"); Font font = new Font(hm); PdfTrueTypeFont underlineFont = new PdfTrueTypeFont (font,true); //Add hypertext link to PDF String label= "hypertext link: "; PdfStringFormat format = new PdfStringFormat(); format.setMeasureTrailingSpaces(true); page.getCanvas().drawString (label, plainFont, PdfBrushes.getOrange(), 0, y, format); x = (float)plainFont.measureString (label,format).getWidth(); //Create PdfTextWebLink object PdfTextWebLink webLink = new PdfTextWebLink(); //Set hyperlink text webLink.setText("Home"); //Set hyperlink address webLink.setUrl(" https://www.google.com"); //Set hyperlink font and font color webLink.setFont (plainFont); webLink.setBrush(PdfBrushes.getBlue()); //add hyperlink to page webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y)); y= y +40; //save document doc.saveToFile("AddLinks.pdf"); doc.close(); }}

2. Add mailbox link

public class EMailLink { public static void main (String[] args) throws Exception{ //Create PDF Document PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.getPages().add(); //Initialize X,Y coordinates float y = 30; float x = 0; //Create a plain font PdfTrueTypeFont plainFont = new PdfTrueTypeFont (new Font("Arial Unicode MS", Font.PLAIN,13),true); //Create an underlined font HashMap hm = new HashMap(); hm.put (TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); hm.put (TextAttribute.SIZE, 13); hm.put (TextAttribute.FAMILY, "Arial"); Font font = new Font(hm); PdfTrueTypeFont underlineFont = new PdfTrueTypeFont (font,true); //Add Mailbox Link String label = "Mailbox Link: "; PdfStringFormat format = new PdfStringFormat(); format.setMeasureTrailingSpaces(true); page.getCanvas().drawString (label, plainFont, PdfBrushes.getOrange(), 0, y, format); x = (float)plainFont.measureString (label, format).getWidth(); //Create PdfTextWebLink object PdfTextWebLink webLink = new PdfTextWebLink(); webLink = new PdfTextWebLink(); //Set hyperlink text webLink.setText("Contact Us"); //Set hyperlink address webLink.setUrl(" mailto:123@qq.com");//Set hyperlink font and font color webLink.setFont(plainFont); webLink.setBrush(PdfBrushes.getBlue()); //Add hyperlink to page webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y)); y = y + 40; //Save document doc.saveToFile("AddLinks.pdf"); doc.close(); }}

3. Add Document Link

public class FileLink { public static void main (String[] args) throws Exception{ //Create PDF Document PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.getPages().add(); //Initialize X,Y coordinates float y = 30; float x = 0; //Create a plain font PdfTrueTypeFont plainFont = new PdfTrueTypeFont (new Font("Arial Unicode MS", Font.PLAIN,13),true); //Create an underlined font HashMap hm = new HashMap(); hm.put (TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); hm.put (TextAttribute.SIZE, 13); hm.put (TextAttribute.FAMILY, "Arial"); Font font = new Font(hm); PdfTrueTypeFont underlineFont = new PdfTrueTypeFont (font,true); //Add Document Link to PDF String label = "Document Hyperlink: "; PdfStringFormat format = new PdfStringFormat(); format.setMeasureTrailingSpaces(true); page.getCanvas().drawString (label, plainFont, PdfBrushes.getOrange(), 0, y, format); x = (float)plainFont.measureString (label, format).getWidth(); page.getCanvas().drawString ("Open File", plainFont, PdfBrushes.getBlue(), x, y, format); Rectangle2D rect = new Rectangle2D.Float (x,y+10,60,15); //Create a file hyperlink object and load the file PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation (rect,"C:\\Users\\Administrator\\Desktop\\Sample.pdf"); fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f)); //Add file to hyperlink ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null).getAnnotations().add(fileLinkAnnotation); //save doc.saveToFile("AddLinks.pdf"); doc.close(); }}

Update and remove hyperlinks

Test documentation:

Use PDFAnnotatioCollection class and PdfTextWebLinkAnnotationWidget class to create hyperlink annotation collection and get to the first hyperlink, use getUrl () method to set hyperlink address, removeAt() method to remove hyperlink.

import com.spire.pdf.PdfDocument;import com.spire.pdf.PdfPageBase;import com.spire.pdf.annotations.PdfAnnotationCollection;import com.spire.pdf.annotations.PdfTextWebLinkAnnotationWidget;public class UpdateDelLinks { public static void main (String[] args) throws Exception { //Create PDF Document PdfDocument doc = new PdfDocument(); //Load PDF Source Doc.loadFromFile ("data/AddLinks.pdf"); //Get the first page of the document PdfPageBase page = doc.getPages().get(0); //Get the collection of hyperlink comments on the first page PdfAnnotationCollection = page.getAnnotationsWidget(); //Get the first hyperlink PdfTextWebLinkAnnotationWidget uriAnnotationWidget =(PdfTextWebLinkAnnotationWidget) annotationCollection.get(0); //set hyperlink uriAnnotationWidget.setUrl(" www.baidu.com"); //removeAt() method removes second hyperlink annotationCollection.removeAt(1); //save file doc.saveToFile("Output.pdf"); }}

At this point, I believe that we have a deeper understanding of "Java how to add, update and remove PDF hyperlinks," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Links documents fonts files updates methods normal underlined addresses coordinates objects methods interfaces mailboxes messages content text comments spaces paths Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB macOS Shulou Tech Info NVidia