通過Java實現(xiàn)添加或刪除PDF中的附件
當我們在制作PDF文件或者PPT演示文稿的時候,為了讓自己的文件更全面詳細,就會在文件中添加附件。并且將相關(guān)文檔附加到 PDF 可以方便文檔的集中管理和傳輸。那么如何添加或刪除 PDF 中的附件呢?別擔心,我們可以通過編程方式輕松實現(xiàn)此操作。下面是我整理的具體步驟,并附上Java代碼供大家參考。
文檔級附件:PDF的文檔級附件不會顯示在頁面上,只能在PDF閱讀器的“附件”面板中查看。
注釋附件:文件將被添加到頁面的特定位置。注釋附件在頁面上顯示為回形針圖標;審閱者可以雙擊圖標打開文件。
- 在 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包
導入方法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 添加附件
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 基于外部文件創(chuàng)建 PdfAttachment 對象。
- 使用 PdfDocument.getAttachments().add() 方法將附件添加到 PDF。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 PDF 文件。
完整代碼
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 文件。
完整代碼
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 上繪制標簽
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 中刪除附件
詳細步驟如下。
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 使用 PdfDocument.getAttachments() 方法從文檔中獲取附件集合。
- 使用 PdfAttachmentCollection.removeAt() 方法刪除特定附件。 要一次刪除所有附件,可以使用 PdfAttachmentCollection.clear() 方法。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 PDF 文件。
完整代碼
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 中刪除注釋附件
以下是詳細步驟。
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 遍歷文檔中的頁面,并使用 PdfPageBase.getAnnotationsWidget() 方法從特定頁面獲取注釋集合。
- 確定注釋是否為 PdfAttachmentAnnotationWidget 的實例。如果是,請使用 PdfAnnotationCollection.remove() 方法刪除注釋附件。
- 使用 PdfDocument.saveToFile() 方法將文檔保存到另一個 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)建一個 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();
}
}以上就是通過Java實現(xiàn)添加或刪除PDF中的附件的詳細內(nèi)容,更多關(guān)于Java添加刪除PDF附件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot通過spel結(jié)合aop實現(xiàn)動態(tài)傳參的案例
SpEl 是Spring框架中的一個利器,Spring通過SpEl能在運行時構(gòu)建復雜表達式、存取對象屬性、對象方法調(diào)用等,今天通過本文給大家介紹springboot?spel結(jié)合aop實現(xiàn)動態(tài)傳參,需要的朋友可以參考下2022-07-07
使用Springboot實現(xiàn)word在線編輯保存
PageOffice目前支持的Web編程語言及架構(gòu)有:Java(JSP、SSH、MVC等),ASP.NET(C#、VB.NET、MVC、Razor等),PHP,ASP,本篇文章就帶你使用Springboot整合PageOffice實現(xiàn)word在線編輯保存2021-08-08
java.io.EOFException: Unexpected end of
本文主要介紹了java.io.EOFException: Unexpected end of ZLIB input stream異常解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05

