After adding hyperlinks particular element, the user can activate by clicking the link elements are linked, typically underlined elements are linked in a different color or displayed to distinguish. According to the use of different objects, links can be divided into: a variety of link text hyperlinks, images, hyperlinks, E-mail links, anchor links, multimedia files, links, air links, etc., will be introduced in this article to add the PDF several different types of hyperlinks, comprising:
- Normal Connection
- Hypertext links
- E-mail link
- Document links
Use tools: as Free Spire.PDF for the Java (free version)
Jar file import:
Method 1: through the official network download package. After the download, unzip the file and the lib folder under the Spire.Pdf.jar file into java program.
Method 2: can be introduced by mounting maven repository. Configuration and method for introducing path may refer to the tutorial .
Java code examples
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 {
//创建PDF文档
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.getPages().add();
//初始化X,Y坐标
float y = 30;
float x = 0;
// 创建一个普通字体
PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
//创建一个带下划线的字体
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);
//添加简单链接到PDF
String label = "简单链接: ";
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;
//添加超文本链接到PDF
label= "超文本链接: ";
page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
x = (float)plainFont.measureString(label,format).getWidth();
PdfTextWebLink webLink = new PdfTextWebLink();
webLink.setText("百度主页");
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;
//添加邮箱链接到PDF
label = "邮箱链接: ";
page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
x = (float)plainFont.measureString(label, format).getWidth();
webLink = new PdfTextWebLink();
webLink.setText("联系我们");
webLink.setUrl("[email protected]");
webLink.setFont(plainFont);
webLink.setBrush(PdfBrushes.getBlue());
webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
y = y + 26;
//添加文档链接到PDF
label = "文档链接: ";
page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
x = (float)plainFont.measureString(label, format).getWidth();
page.getCanvas().drawString("详情参阅原文件", 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\\测试文件.docx");
fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f));
((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(fileLinkAnnotation);
//保存文档
doc.saveToFile("超链接.pdf");
doc.close();
}
}
Add link results:
(This article End)