Java添加、更新和移除PDF超鏈接-創(chuàng)新互聯(lián)

簡介

PDF超鏈接用一個簡單的鏈接包含了大量的信息,滿足了人們在不占用太多空間的情況下渲染外部信息的需求。下面將介紹通過Java 在PDF中添加、更新和移除超鏈接。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比蘭坪網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式蘭坪網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋蘭坪地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
(一)工具使用:
  • ? Free Spire.PDF for Java 2.4.4(免費(fèi)版)
  • ? Intellij IDEA
    (二)導(dǎo)入Jar文件包:

方式一:首先,從官網(wǎng)獲取Free Spire.PDF for Java文件包。

Step 1: 下載控件包之后解壓,打開“Project Structure”界面。(以下是三種在IDEA中快速打開Project Structure界面的方式,可選其中任意一種)
Java 添加、更新和移除PDF超鏈接
Step 2:按以下操作步驟進(jìn)行導(dǎo)入。① 選擇“Modules”—“Dependencies”,添加外置jar包;② 進(jìn)入"Attach File or Directories"界面選擇jar文件路徑,然后點(diǎn)擊“OK”;③ 勾選jar路徑選項(xiàng),點(diǎn)擊”O(jiān)K”/”Apply”;④ 導(dǎo)入完成。如下圖:
Java 添加、更新和移除PDF超鏈接
Java 添加、更新和移除PDF超鏈接
? 方式二:使用Maven配置導(dǎo)包??梢詤⒖?導(dǎo)入方式。

Java代碼示例參考

(一) 添加超鏈接到PDF

添加命名空間:

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{
        //創(chuàng)建PDF文檔
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();
        //初始化X,Y坐標(biāo)
        float y = 30;
        float x = 0;
        // 創(chuàng)建一個普通字體
        PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
        //創(chuàng)建一個帶下劃線的字體
        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();
        //創(chuàng)建PdfTextWebLink對象
        PdfTextWebLink webLink = new PdfTextWebLink();
        //設(shè)置超鏈接文本
        webLink.setText("主頁");
        //設(shè)置超鏈接地址
        webLink.setUrl("https://www.google.com");
        //設(shè)置超鏈接字體和字體顏色
        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();
    }
}

添加結(jié)果:
Java 添加、更新和移除PDF超鏈接

2. 添加郵箱鏈接
public class EMailLink {
    public static void main(String[] args) throws Exception{
        //創(chuàng)建PDF文檔
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();
        //初始化X,Y坐標(biāo)
        float y = 30;
        float x = 0;
        // 創(chuàng)建一個普通字體
        PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
        //創(chuàng)建一個帶下劃線的字體
        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();
        //創(chuàng)建PdfTextWebLink對象
        PdfTextWebLink webLink = new PdfTextWebLink();
        webLink = new PdfTextWebLink();
        //設(shè)置超鏈接文本
        webLink.setText("聯(lián)系我們");
        //設(shè)置超鏈接地址
        webLink.setUrl("mailto:123@qq.com");
        //設(shè)置超鏈接字體和字體顏色
        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();
    }
}

添加結(jié)果:
Java 添加、更新和移除PDF超鏈接

3. 添加文檔鏈接
public class FileLink {
    public static void main(String[] args) throws Exception{
        //創(chuàng)建PDF文檔
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();
        //初始化X,Y坐標(biāo)
        float y = 30;
        float x = 0;
        // 創(chuàng)建一個普通字體
        PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
        //創(chuàng)建一個帶下劃線的字體
        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);
        //創(chuàng)建一個文件超鏈接對象并加載文件
        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();
    }
}

添加結(jié)果:
Java 添加、更新和移除PDF超鏈接

(二) 更新和移除超鏈接

測試文檔:
Java 添加、更新和移除PDF超鏈接

使用PDFAnnotatioCollection 類和PdfTextWebLinkAnnotationWidget類創(chuàng)建超鏈注釋集合并獲取到第一個超鏈接,使用getUrl ()方法設(shè)置超鏈接地址,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 {
        //創(chuàng)建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);
        //設(shè)置超鏈接
        uriAnnotationWidget.setUrl("www.baidu.com");
        //removeAt()方法移除第二條超鏈接
        annotationCollection.removeAt(1);
       //保存文件
        doc.saveToFile("Output.pdf");
    }
}

更新移除結(jié)果:
Java 添加、更新和移除PDF超鏈接

(本文完)

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

分享文章:Java添加、更新和移除PDF超鏈接-創(chuàng)新互聯(lián)
地址分享:http://muchs.cn/article30/cocpso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、自適應(yīng)網(wǎng)站標(biāo)簽優(yōu)化、搜索引擎優(yōu)化、域名注冊、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)