使用Java應(yīng)用程序添加或刪除 PDF 中的附件
如何通過Java應(yīng)用程序添加或刪除 PDF 中的附件
當(dāng)我們在制作PDF文件或者PPT演示文稿的時候,為了讓自己的文件更全面詳細(xì),就會在文件中添加附件。并且將相關(guān)文檔附加到 PDF 可以方便文檔的集中管理和傳輸。那么如何添加或刪除 PDF 中的附件呢?別擔(dān)心,我們可以通過編程方式輕松實現(xiàn)此操作。下面是我整理的具體步驟,并附上Java代碼供大家參考。
文檔級附件:PDF的文檔級附件不會顯示在頁面上,只能在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:
手動引入。將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)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 基于外部文件創(chuàng)建 PdfAttachment 對象。
- 使用 PdfDocument.getAttachments().add() 方法將附件添加到 PDF。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 PDF 文件。
完整代碼
Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.attachments.PdfAttachment; public class AttachFilesToPdf { public static void main(String[] args) { //創(chuàng)建一個 PdfDocument 對象 PdfDocument doc = new PdfDocument(); //加載 PDF 文檔 doc.loadFromFile("什么是AI數(shù)字人.pdf"); //基于外部文件創(chuàng)建 PdfAttachment 對象 PdfAttachment attachment = new PdfAttachment("到場嘉賓名單.xlsx"); //將附件添加到 PDF doc.getAttachments().add(attachment); //保存文件 doc.saveToFile("添加附件.pdf"); } }
效果圖
在 Java 中向 PDF 添加注釋附件
以下是向 PDF 添加注釋附件的步驟。
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 使用 PdfDocument.getPages().get() 方法獲取特定頁面以添加注釋。
- 基于外部文件創(chuàng)建 PdfAttachmentAnnotation 對象。
- 使用 PdfPageBase.getAnnotationsWidget().add() 方法將注釋附件添加到頁面。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 PDF 文件。
完整代碼
Java
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)建一個 PdfDocument 對象 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)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 使用 PdfDocument.getAttachments() 方法從文檔中獲取附件集合。
- 使用 PdfAttachmentCollection.removeAt() 方法刪除特定附件。 要一次刪除所有附件,可以使用 PdfAttachmentCollection.clear() 方法。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 PDF 文件。
完整代碼
Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.attachments.PdfAttachmentCollection; public class RemoveAttachments { public static void main(String[] args) { //創(chuàng)建一個 PdfDocument 對象 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)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 遍歷文檔中的頁面,并使用 PdfPageBase.getAnnotationsWidget() 方法從特定頁面獲取注釋集合。
- 確定注釋是否為 PdfAttachmentAnnotationWidget 的實例。如果是,請使用 PdfAnnotationCollection.remove() 方法刪除注釋附件。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 PDF 文件。
完整代碼
Java
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)建一個 PdfDocument 對象 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 的實例 if (annotation instanceof PdfAttachmentAnnotationWidget){ //刪除注釋附件 annotationCollection.remove((PdfAnnotation) annotation); } } } //保存文件 doc.saveToFile("刪除注釋附件.pdf"); doc.close(); } }
到此這篇關(guān)于如何通過Java應(yīng)用程序添加或刪除 PDF 中的附件的文章就介紹到這了,更多相關(guān)java添加或刪除 PDF 中的附件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中l(wèi)ock和tryLock及l(fā)ockInterruptibly的區(qū)別
這篇文章主要介紹了Java中l(wèi)ock和tryLock及l(fā)ockInterruptibly的區(qū)別,文章介紹詳細(xì),具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn)
本文主要介紹了SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12mybatis動態(tài)插入list傳入List參數(shù)的實例代碼
本文通過實例代碼給大家介紹了mybatis動態(tài)插入list,Mybatis 傳入List參數(shù)的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-04-04Java之Spring AOP 實現(xiàn)用戶權(quán)限驗證
本篇文章主要介紹了Java之Spring AOP 實現(xiàn)用戶權(quán)限驗證,用戶登錄、權(quán)限管理這些是必不可少的業(yè)務(wù)邏輯,具有一定的參考價值,有興趣的可以了解一下。2017-02-02SpringBoot this調(diào)用@Bean效果詳解
這篇文章主要介紹了在一個@Bean方法內(nèi),this調(diào)用同一個類的@Bean方法會有什么效果,我們可以通過bean的名稱、bean的類型或者bean的名稱+類型來獲取容器中的bean2023-02-02Java實現(xiàn)在線預(yù)覽的示例代碼(openOffice實現(xiàn))
本篇文章主要介紹了Java實現(xiàn)在線預(yù)覽的示例代碼(openOffice實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Java多線程并發(fā)編程 Volatile關(guān)鍵字
volatile 關(guān)鍵字是一個神秘的關(guān)鍵字,也許在 J2EE 上的 JAVA 程序員會了解多一點,但在 Android 上的 JAVA 程序員大多不了解這個關(guān)鍵字。只要稍了解不當(dāng)就好容易導(dǎo)致一些并發(fā)上的錯誤發(fā)生,例如好多人把 volatile 理解成變量的鎖2017-05-05