當我們在制作PDF檔案或者PPT演示文稿的時候,為了讓自己的檔案更全面詳細,就會在檔案中添加附件,并且將相關檔案附加到 PDF 可以方便檔案的集中管理和傳輸,那么如何添加或洗掉 PDF 中的附件呢?別擔心,我們可以通過編程方式輕松實作此操作,下面是我整理的具體步驟,并附上Java代碼供大家參考,
檔案級附件:PDF的檔案級附件不會顯示在頁面上,只能在PDF閱讀器的“附件”面板中查看,
注釋附件:檔案將被添加到頁面的特定位置,注釋附件在頁面上顯示為回形針圖示;審閱者可以雙擊圖示打開檔案,
- 在 Java 中向 PDF 添加附件
- 在 Java 中向 PDF 添加注釋附件
- 在 Java 中從 PDF 中洗掉附件
- 在 Java 中從 PDF 中洗掉注釋附件
代碼編譯環境:
IntelliJ IDEA 2018(jdk 1.8.0)
PDF Jar包:Free Spire.PDF for Java 5.1.0
引入jar包
匯入方法1:
手動引入,將Free Spire.PDF for Java下載到本地,解壓,找到lib檔案夾下的Spire.PDF.jar檔案,在IDEA中打開如下界面,將本地路徑中的jar檔案引入Java程式:

匯入方法2:如果您想通過 Maven安裝,則可以在 pom.xml 檔案中添加以下代碼匯入 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 添加附件
- 創建一個 PdfDocument 物件,
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 檔案,
- 基于外部檔案創建 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) { //創建一個 PdfDocument 物件 PdfDocument doc = new PdfDocument(); //加載 PDF 檔案 doc.loadFromFile("什么是AI數字人.pdf"); //基于外部檔案創建 PdfAttachment 物件 PdfAttachment attachment = new PdfAttachment("到場嘉賓名單.xlsx"); //將附件添加到 PDF doc.getAttachments().add(attachment); //保存檔案 doc.saveToFile("添加附件.pdf"); } }
效果圖

在 Java 中向 PDF 添加注釋附件
以下是向 PDF 添加注釋附件的步驟,
- 創建一個 PdfDocument 物件,
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 檔案,
- 使用 PdfDocument.getPages().get() 方法獲取特定頁面以添加注釋,
- 基于外部檔案創建 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 { //創建一個 PdfDocument 物件 PdfDocument doc = new PdfDocument(); //加載 PDF 檔案 doc.loadFromFile("什么是AI數字人1.pdf"); //獲取特定頁面 PdfPageBase page = doc.getPages().get(0); //在 PDF 上繪制標簽 String label = "AI數字人詳情介紹見附件:"; 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數字人詳情介紹.pptx"; byte[] data =https://www.cnblogs.com/Carina-baby/p/ 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"); } //將檔案轉換為位元組陣列 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 中洗掉附件
詳細步驟如下,
- 創建一個 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) { //創建一個 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 中洗掉注釋附件
以下是詳細步驟,
- 創建一個 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) { //創建一個 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(); //回圈遍歷注釋 for (Object annotation: annotationCollection) { //確定注釋是否為 PdfAttachmentAnnotationWidget 的實體 if (annotation instanceof PdfAttachmentAnnotationWidget){ //洗掉注釋附件 annotationCollection.remove((PdfAnnotation) annotation); } } } //保存檔案 doc.saveToFile("洗掉注釋附件.pdf"); doc.close(); } }
—本文完—
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542669.html
標籤:Java
