通過Java實(shí)現(xiàn)添加或刪除PDF中的附件
當(dāng)我們?cè)谥谱鱌DF文件或者PPT演示文稿的時(shí)候,為了讓自己的文件更全面詳細(xì),就會(huì)在文件中添加附件。并且將相關(guān)文檔附加到 PDF 可以方便文檔的集中管理和傳輸。那么如何添加或刪除 PDF 中的附件呢?別擔(dān)心,我們可以通過編程方式輕松實(shí)現(xiàn)此操作。下面是我整理的具體步驟,并附上Java代碼供大家參考。
文檔級(jí)附件:PDF的文檔級(jí)附件不會(huì)顯示在頁面上,只能在PDF閱讀器的“附件”面板中查看。
注釋附件:文件將被添加到頁面的特定位置。注釋附件在頁面上顯示為回形針圖標(biāo);審閱者可以雙擊圖標(biāo)打開文件。
- 在 Java 中向 PDF 添加附件
- 在 Java 中向 PDF 添加注釋附件
- 在 Java 中從 PDF 中刪除附件
- 在 Java 中從 PDF 中刪除注釋附件
代碼編譯環(huán)境:
IntelliJ IDEA 2018(jdk 1.8.0)
PDF Jar包:Free Spire.PDF for Java 5.1.0
引入jar包
導(dǎo)入方法1:
手動(dòng)引入。將Free Spire.PDF for Java下載到本地,解壓,找到lib文件夾下的Spire.PDF.jar文件。在IDEA中打開如下界面,將本地路徑中的jar文件引入Java程序:
導(dǎo)入方法2:如果您想通過 Maven安裝,則可以在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
在 Java 中向 PDF 添加附件
- 創(chuàng)建一個(gè) PdfDocument 對(duì)象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 基于外部文件創(chuàng)建 PdfAttachment 對(duì)象。
- 使用 PdfDocument.getAttachments().add() 方法將附件添加到 PDF。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個(gè) PDF 文件。
完整代碼
import com.spire.pdf.PdfDocument; import com.spire.pdf.attachments.PdfAttachment; public class AttachFilesToPdf { public static void main(String[] args) { //創(chuàng)建一個(gè) PdfDocument 對(duì)象 PdfDocument doc = new PdfDocument(); //加載 PDF 文檔 doc.loadFromFile("什么是AI數(shù)字人.pdf"); //基于外部文件創(chuàng)建 PdfAttachment 對(duì)象 PdfAttachment attachment = new PdfAttachment("到場(chǎng)嘉賓名單.xlsx"); //將附件添加到 PDF doc.getAttachments().add(attachment); //保存文件 doc.saveToFile("添加附件.pdf"); } }
效果圖
在 Java 中向 PDF 添加注釋附件
以下是向 PDF 添加注釋附件的步驟。
- 創(chuàng)建一個(gè) PdfDocument 對(duì)象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 使用 PdfDocument.getPages().get() 方法獲取特定頁面以添加注釋。
- 基于外部文件創(chuàng)建 PdfAttachmentAnnotation 對(duì)象。
- 使用 PdfPageBase.getAnnotationsWidget().add() 方法將注釋附件添加到頁面。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個(gè) PDF 文件。
完整代碼
import com.spire.pdf.PdfPageBase; import com.spire.pdf.annotations.*; import com.spire.pdf.graphics.*; import com.spire.pdf.PdfDocument; import java.awt.*; import java.awt.geom.Dimension2D; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class AnnotationAttachment { public static void main(String[] args) throws IOException { //創(chuàng)建一個(gè) PdfDocument 對(duì)象 PdfDocument doc = new PdfDocument(); //加載 PDF 文檔 doc.loadFromFile("什么是AI數(shù)字人1.pdf"); //獲取特定頁面 PdfPageBase page = doc.getPages().get(0); //在 PDF 上繪制標(biāo)簽 String label = "AI數(shù)字人詳情介紹見附件:"; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋體", Font.PLAIN, 13)); double x = 35; double y = doc.getPages().get(0).getActualSize().getHeight() - 270; page.getCanvas().drawString(label, font, PdfBrushes.getRed(), x, y); //附加文件作為注釋 String filePath = "AI數(shù)字人詳情介紹.pptx"; byte[] data = toByteArray(filePath); Dimension2D size = font.measureString(label); Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 5), (float) y, 10, 15); PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data); annotation.setColor(new PdfRGBColor(new Color(0, 128, 128))); annotation.setFlags(PdfAnnotationFlags.Default); annotation.setIcon(PdfAttachmentIcon.Graph); annotation.setText("單擊此處打開文件"); page.getAnnotationsWidget().add(annotation); //保存文件 doc.saveToFile("添加注釋附件.pdf"); } //將文件轉(zhuǎn)換為字節(jié)數(shù)組 public static byte[] toByteArray(String filePath) throws IOException { File file = new File(filePath); long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { System.out.println("文件過大..."); return null; } FileInputStream fi = new FileInputStream(file); byte[] buffer = new byte[(int) fileSize]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } if (offset != buffer.length) { throw new IOException("無法完全讀取文件 " + file.getName()); } fi.close(); return buffer; } }
效果圖
在 Java 中從 PDF 中刪除附件
詳細(xì)步驟如下。
- 創(chuàng)建一個(gè) PdfDocument 對(duì)象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 使用 PdfDocument.getAttachments() 方法從文檔中獲取附件集合。
- 使用 PdfAttachmentCollection.removeAt() 方法刪除特定附件。 要一次刪除所有附件,可以使用 PdfAttachmentCollection.clear() 方法。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個(gè) PDF 文件。
完整代碼
import com.spire.pdf.PdfDocument; import com.spire.pdf.attachments.PdfAttachmentCollection; public class RemoveAttachments { public static void main(String[] args) { //創(chuàng)建一個(gè) PdfDocument 對(duì)象 PdfDocument doc = new PdfDocument(); //加載 PDF 文檔 doc.loadFromFile("添加附件.pdf"); //獲取附件集合,不包含注釋附件 PdfAttachmentCollection attachments = doc.getAttachments(); //刪除所有附件 attachments.clear(); //刪除指定附件 //attachments.removeAt(0); //保存文件 doc.saveToFile("刪除附件.pdf"); doc.close(); } }
在 Java 中從 PDF 中刪除注釋附件
以下是詳細(xì)步驟。
- 創(chuàng)建一個(gè) PdfDocument 對(duì)象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 遍歷文檔中的頁面,并使用 PdfPageBase.getAnnotationsWidget() 方法從特定頁面獲取注釋集合。
- 確定注釋是否為 PdfAttachmentAnnotationWidget 的實(shí)例。如果是,請(qǐng)使用 PdfAnnotationCollection.remove() 方法刪除注釋附件。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個(gè) PDF 文件。
完整代碼
import com.spire.pdf.PdfDocument; import com.spire.pdf.annotations.PdfAnnotation; import com.spire.pdf.annotations.PdfAnnotationCollection; import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget; public class RemoveAnnotationAttachments { public static void main(String[] args) { //創(chuàng)建一個(gè) PdfDocument 對(duì)象 PdfDocument doc = new PdfDocument(); //加載 PDF 文檔 doc.loadFromFile("添加注釋附件.pdf"); //遍歷文檔中的頁面 for (int i = 0; i < doc.getPages().getCount(); i++) { //獲取注釋集合 PdfAnnotationCollection annotationCollection = doc.getPages().get(i).getAnnotationsWidget(); //循環(huán)遍歷注釋 for (Object annotation: annotationCollection) { //確定注釋是否為 PdfAttachmentAnnotationWidget 的實(shí)例 if (annotation instanceof PdfAttachmentAnnotationWidget){ //刪除注釋附件 annotationCollection.remove((PdfAnnotation) annotation); } } } //保存文件 doc.saveToFile("刪除注釋附件.pdf"); doc.close(); } }
以上就是通過Java實(shí)現(xiàn)添加或刪除PDF中的附件的詳細(xì)內(nèi)容,更多關(guān)于Java添加刪除PDF附件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java泛型的局限探究及知識(shí)點(diǎn)總結(jié)
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于java泛型的局限探究及知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-07-07Java實(shí)現(xiàn)的漢語拼音工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的漢語拼音工具類,結(jié)合完整實(shí)例形式分析了java基于pinyin4j包實(shí)現(xiàn)編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11springboot通過spel結(jié)合aop實(shí)現(xiàn)動(dòng)態(tài)傳參的案例
SpEl 是Spring框架中的一個(gè)利器,Spring通過SpEl能在運(yùn)行時(shí)構(gòu)建復(fù)雜表達(dá)式、存取對(duì)象屬性、對(duì)象方法調(diào)用等,今天通過本文給大家介紹springboot?spel結(jié)合aop實(shí)現(xiàn)動(dòng)態(tài)傳參,需要的朋友可以參考下2022-07-07使用Springboot實(shí)現(xiàn)word在線編輯保存
PageOffice目前支持的Web編程語言及架構(gòu)有:Java(JSP、SSH、MVC等),ASP.NET(C#、VB.NET、MVC、Razor等),PHP,ASP,本篇文章就帶你使用Springboot整合PageOffice實(shí)現(xiàn)word在線編輯保存2021-08-08通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解
這篇文章主要介紹了通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12java.io.EOFException: Unexpected end of
本文主要介紹了java.io.EOFException: Unexpected end of ZLIB input stream異常解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05