Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java adds, updates, and removes PDF hyperlinks

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report