Java add, update, and remove PDF hyperlinks

Brief introduction

PDF hyperlinks with a simple link contains a lot of information to meet the people rendering external information without taking up too much space requirements. The following describes the PDF by adding Java, update, and remove hyperlinks.

 

(A) tools:

  •   Free Spire.PDF for Java 2.4.4 (free version)
  •  Intellij IDEA

(B) introducing Jar package:

1 the STEP : After downloading the control package unzip, open the "Project Structure" interface. (There are three ways to quickly open the Project Structure interface in the IDEA, any of which optionally)

2 STEP : import according to the following procedure. ① Select "Modules" - "Dependencies", add external jar package; ② into the "Attach File or Directories" interface to select the jar file path, and then click "OK"; ③ Check the jar path options, and click "OK" / "Apply "; ④ import is complete. As shown below:

Referring to the example of Java code

(A)  add a hyperlink to a PDF

Add namespace: 

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. 添加超文本连接
public class TextLink {
    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();
        //创建PdfTextWebLink对象
        PdfTextWebLink webLink = new PdfTextWebLink();
        //设置超链接文本
        webLink.setText("主页");
        //设置超链接地址
        webLink.setUrl("https://www.google.com");
        //设置超链接字体和字体颜色
        webLink.setFont(plainFont);
        webLink.setBrush(PdfBrushes.getBlue());
        //添加超链接到页面
        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
        y= y +40;
        //保存文档
        doc.saveToFile("AddLinks.pdf");
        doc.close();
    }
}

添加结果:

2. 添加邮箱链接

public class EMailLink {
    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);
        //添加邮箱链接
        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();
        //创建PdfTextWebLink对象
        PdfTextWebLink webLink = new PdfTextWebLink();
        webLink = new PdfTextWebLink();
        //设置超链接文本
        webLink.setText("联系我们");
        //设置超链接地址
        webLink.setUrl("mailto:[email protected]");
        //设置超链接字体和字体颜色
        webLink.setFont(plainFont);
        webLink.setBrush(PdfBrushes.getBlue());
        //添加超链接到页面
        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
        y = y + 40;

        //保存文档
        doc.saveToFile("AddLinks.pdf");
        doc.close();
    }
}

添加结果:

3.   添加文档链接

public class FileLink {
    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("打开文件", plainFont, PdfBrushes.getBlue(), x, y, format);
        Rectangle2D rect = new Rectangle2D.Float(x,y+10,60,15);
        //创建一个文件超链接对象并加载文件
        PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rect,"C:\\Users\\Administrator\\Desktop\\Sample.pdf");
        fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f));
        //添加文件到超链接
        ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(fileLinkAnnotation);
        //保存文档
        doc.saveToFile("AddLinks.pdf");
        doc.close();
    }
}
 

添加结果:

(二) 更新和移除超链接

      测试文档:

  

  使用PDFAnnotatioCollection 类和PdfTextWebLinkAnnotationWidget类创建超链注释集合并获取到第一个超链接,使用getUrl ()方法设置超链接地址,removeAt()方法移除超链接。

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 {
        //创建PDF文档
        PdfDocument doc = new PdfDocument();
        //加载PDF源文件
        doc.loadFromFile("data/AddLinks.pdf");
        //获取文档第一页
        PdfPageBase page = doc.getPages().get(0);
        //获取第一页超链接注释的集合
        PdfAnnotationCollection annotationCollection = page.getAnnotationsWidget();
        //获取第一个超链接
        PdfTextWebLinkAnnotationWidget uriAnnotationWidget = (PdfTextWebLinkAnnotationWidget) annotationCollection.get(0);
        //设置超链接
        uriAnnotationWidget.setUrl("www.baidu.com");
        //removeAt()方法移除第二条超链接
        annotationCollection.removeAt(1);
       //保存文件
        doc.saveToFile("Output.pdf");
    }
}

更新移除结果:

(本文完)

转载请注明出处! 

 

Guess you like

Origin www.cnblogs.com/MariaWang/p/10950608.html