最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 添加、更新和移除PDF超鏈接的實(shí)現(xiàn)方法

 更新時(shí)間:2019年05月31日 10:44:32   作者:4207  
PDF超鏈接用一個(gè)簡(jiǎn)單的鏈接包含了大量的信息,滿(mǎn)足了人們?cè)诓徽加锰嗫臻g的情況下渲染外部信息的需求。這篇文章主要介紹了Java 添加、更新和移除PDF超鏈接的實(shí)現(xiàn)方法,需要的朋友可以參考下

簡(jiǎn)介

PDF超鏈接用一個(gè)簡(jiǎn)單的鏈接包含了大量的信息,滿(mǎn)足了人們?cè)诓徽加锰嗫臻g的情況下渲染外部信息的需求。下面將介紹通過(guò)Java 在PDF中添加、更新和移除超鏈接。

(一)工具使用:

•  Free Spire.PDF for Java 2.4.4(免費(fèi)版)
• Intellij IDEA

(二)導(dǎo)入Jar文件包:
•  方式一:首先,從官網(wǎng)獲取Free Spire.PDF for Java文件包。

Step 1: 下載控件包之后解壓,打開(kāi)“Project Structure”界面。(以下是三種在IDEA中快速打開(kāi)Project Structure界面的方式,可選其中任意一種)

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)入完成。如下圖:


•  方式二:使用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)建一個(gè)普通字體
 PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
 //創(chuàng)建一個(gè)帶下劃線(xiàn)的字體
 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對(duì)象
 PdfTextWebLink webLink = new PdfTextWebLink();
 //設(shè)置超鏈接文本
 webLink.setText("主頁(yè)");
 //設(shè)置超鏈接地址
 webLink.setUrl("https://www.google.com");
 //設(shè)置超鏈接字體和字體顏色
 webLink.setFont(plainFont);
 webLink.setBrush(PdfBrushes.getBlue());
 //添加超鏈接到頁(yè)面
 webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
 y= y +40;
 //保存文檔
 doc.saveToFile("AddLinks.pdf");
 doc.close();
 }
}

添加結(jié)果:

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)建一個(gè)普通字體
 PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
 //創(chuàng)建一個(gè)帶下劃線(xiàn)的字體
 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對(duì)象
 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());
 //添加超鏈接到頁(yè)面
 webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
 y = y + 40;

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

添加結(jié)果:

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)建一個(gè)普通字體
 PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);
 //創(chuàng)建一個(gè)帶下劃線(xiàn)的字體
 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("打開(kāi)文件", plainFont, PdfBrushes.getBlue(), x, y, format);
 Rectangle2D rect = new Rectangle2D.Float(x,y+10,60,15);
 //創(chuàng)建一個(gè)文件超鏈接對(duì)象并加載文件
 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é)果:

(二) 更新和移除超鏈接

      測(cè)試文檔:

  

  使用PDFAnnotatioCollection 類(lèi)和PdfTextWebLinkAnnotationWidget類(lèi)創(chuàng)建超鏈注釋集合并獲取到第一個(gè)超鏈接,使用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");
 //獲取文檔第一頁(yè)
 PdfPageBase page = doc.getPages().get(0);
 //獲取第一頁(yè)超鏈接注釋的集合
 PdfAnnotationCollection annotationCollection = page.getAnnotationsWidget();
 //獲取第一個(gè)超鏈接
 PdfTextWebLinkAnnotationWidget uriAnnotationWidget = (PdfTextWebLinkAnnotationWidget) annotationCollection.get(0);
 //設(shè)置超鏈接
 uriAnnotationWidget.setUrl("www.baidu.com");
 //removeAt()方法移除第二條超鏈接
 annotationCollection.removeAt(1);
 //保存文件
 doc.saveToFile("Output.pdf");
 }
}

更新移除結(jié)果:

相關(guān)文章

  • Springboot訪(fǎng)問(wèn)html頁(yè)面的教程詳解

    Springboot訪(fǎng)問(wèn)html頁(yè)面的教程詳解

    這篇文章主要介紹了Springboot訪(fǎng)問(wèn)html頁(yè)面的教程,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-03-03
  • java編寫(xiě)簡(jiǎn)易貪吃蛇游戲

    java編寫(xiě)簡(jiǎn)易貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了java編寫(xiě)簡(jiǎn)易貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Springboot集成activity過(guò)程圖解

    Springboot集成activity過(guò)程圖解

    這篇文章主要介紹了Springboot集成activity過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 深入剖析java中String、StringBuffer、StringBuilder的區(qū)別

    深入剖析java中String、StringBuffer、StringBuilder的區(qū)別

    下面小編就為大家?guī)?lái)一篇深入剖析java中String、StringBuffer、StringBuilder的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • Eclipse將Maven項(xiàng)目打成jar包的方法

    Eclipse將Maven項(xiàng)目打成jar包的方法

    這篇文章主要介紹了Eclipse將Maven項(xiàng)目打成jar包的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2007-09-09
  • 如何為Spring Cloud Gateway加上全局過(guò)濾器

    如何為Spring Cloud Gateway加上全局過(guò)濾器

    這篇文章主要介紹了如何為Spring Cloud Gateway加上全局過(guò)濾器,幫助大家更好得理解和學(xué)習(xí)使用Gateway,感興趣的朋友可以了解下
    2021-03-03
  • java操作ftp下載文件示例

    java操作ftp下載文件示例

    這篇文章主要介紹了java操作ftp下載文件的示例,需要的朋友可以參考下
    2014-02-02
  • struts2實(shí)現(xiàn)文件下載功能

    struts2實(shí)現(xiàn)文件下載功能

    這篇文章主要為大家詳細(xì)介紹了struts2實(shí)現(xiàn)文件下載功能,一個(gè)非常常見(jiàn)的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(15)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(15)

    下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你
    2021-07-07
  • java實(shí)現(xiàn)圖書(shū)館管理系統(tǒng)

    java實(shí)現(xiàn)圖書(shū)館管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖書(shū)館管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評(píng)論

河北省| 化州市| 哈巴河县| 新巴尔虎左旗| 辽宁省| 黎城县| 清流县| 马山县| 辉县市| 青神县| 榆林市| 罗江县| 宁强县| 临邑县| 海口市| 云霄县| 崇阳县| 体育| 宜川县| 泗水县| 灵寿县| 浪卡子县| 青海省| 丹寨县| 江北区| 读书| 遂平县| 墨江| 桦川县| SHOW| 独山县| 天津市| 富顺县| 瑞昌市| 呼和浩特市| 台东市| 双桥区| 当雄县| 闽侯县| 潞西市| 应用必备|