Java add hyperlink in PDF

Keywords: Java Maven

After adding hyperlinks to specific elements, users can activate these links by clicking on the linked elements, which are usually underlined or displayed in different colors. According to the different objects used, links can be divided into text hyperlinks, image hyperlinks, E-mail links, anchor links, multimedia file links, empty links and other links. In this article, we will introduce several methods to add different types of hyperlinks to PDF, including:

  • Ordinary links
  • hyperlink
  • Mailbox link
  • Document link

Using tool: free flame.pdf for Java (free version)
Jar file import:
Method 1: through the official website download File package. After downloading, extract the file and import the Spire.Pdf.jar file under the lib folder into the java program.
Method 2: it can be installed and imported through maven warehouse. For configuration path and import method, please refer to Course.

Java code example

import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.*;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;

public class AddLinksToPdf {

    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 normal font
        PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);

        //Create an underlined font
        HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();
        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 simple link to PDF
        String label = "Simple 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();
        page.getCanvas().drawString("https://www.baidu.com/", underlineFont, PdfBrushes.getBlue(), x, y+2);
        y = y + 26;

        //Add hypertext link to PDF 
        label= "Hypertext link: ";
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
        x = (float)plainFont.measureString(label,format).getWidth();
        PdfTextWebLink webLink = new PdfTextWebLink();
        webLink.setText("Baidu Homepage");
        webLink.setUrl("https://www.baidu.com/");
        webLink.setFont(plainFont);
        webLink.setBrush(PdfBrushes.getBlue());
        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
        y= y + 26;

        //Add mailbox link to PDF 
        label = "Mailbox link:  ";
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
        x = (float)plainFont.measureString(label, format).getWidth();
        webLink = new PdfTextWebLink();
        webLink.setText("Contact us");
        webLink.setUrl("ask@baidu.com");
        webLink.setFont(plainFont);
        webLink.setBrush(PdfBrushes.getBlue());
        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
        y = y + 26;

        //Add document link to PDF 
        label = "Document links: ";
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
        x = (float)plainFont.measureString(label, format).getWidth();
        page.getCanvas().drawString("Refer to the original document for details", plainFont, PdfBrushes.getBlue(), x, y, format);
        Rectangle2D rect = new Rectangle2D.Float(x,y+2,60,15);
        PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rect,"C:\\Users\\Administrator\\Desktop\\Test file.docx");
        fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f));
        ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(fileLinkAnnotation);

        //Save document
        doc.saveToFile("Hyperlink.pdf");
        doc.close();
    }
}

Link add results:

(end of this paper)

Posted by cloudzilla on Thu, 21 Nov 2019 10:16:22 -0800