PDF 是一种多功能文件格式,可以在其页面上呈现文本和图形,也可以用作存储容器。我们可以将文件附加到 PDF 中,然后再提取它们。并且将相关文档附加到 PDF 可以方便文档的集中管理和传输。
Spire.PDF for Java 允许您以两种方式附加文件:
- 文档级附件:PDF 的文档级附件不会显示在页面上,只能在 PDF阅读器的“附件”面板中查看。
- 注释附件:文件将被添加到页面的特定位置。注释附件在页面上显示为回形针图标;审阅者可以双击图标打开文件。
本文演示了如何使用 Spire.PDF for Java 在 PDF 文档中添加或删除这两种类型的附件。
安装 Spire.PDF for Java
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>11.10.3</version>
    </dependency>
</dependencies>
在 Java 中向 PDF 添加附件
使用 PdfDocument.getAttachments().add() 方法可以轻松地向“附件”面板添加附件。 以下是详细步骤。
- 创建一个 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("C:\\Users\\Administrator\\Desktop\\AI创意赛.pdf");
        //基于外部文件创建 PdfAttachment 对象
        PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\参赛人员名单.xlsx");
        //将附件添加到 PDF
        doc.getAttachments().add(attachment);
        //保存文件
        doc.saveToFile("添加附件.pdf");
    }
}
在 Java 中向 PDF 添加注释附件
注释附件不但出现在某个特定页面,也会出现在“附件”面板中。以下是使用 Spire.PDF for Java 向 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("C:\\Users\\Administrator\\Desktop\\AI创意赛.pdf");
        //获取特定页面
        PdfPageBase page = doc.getPages().get(0);
        //在 PDF 上绘制标签
        String label = "现场相关事宜安排见附件:";
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", Font.PLAIN, 13));
        double x = 35;
        double y = doc.getPages().get(0).getActualSize().getHeight() - 220;
        page.getCanvas().drawString(label, font, PdfBrushes.getRed(), x, y);
        //附加文件作为注释
        String filePath = "C:\\Users\\Administrator\\Desktop\\相关事宜安排.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");
    }
    //将文件转换为字节数组
    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 中删除附件
PDF 文档的附件可以使用 PdfDocument.getAttachments() 方法访问,并且可以使用 PdfAttachmentCollection 对象的 removeAt() 方法或 clear() 方法删除。 详细步骤如下。
- 创建一个 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("C:\\Users\\Administrator\\Desktop\\添加附件.pdf");
        //获取附件集合,不包含注释附件
        PdfAttachmentCollection attachments = doc.getAttachments();
        //删除所有附件
        attachments.clear();
        //删除指定附件
        //attachments.removeAt(0);
        //保存文件
        doc.saveToFile("删除附件.pdf");
        doc.close();
    }
}在 Java 中从 PDF 中删除注释附件
注释是基于页面的元素。要从文档中获取所有注释,我们必须遍历页面并从每个页面中获取注释。然后判断某个注释是否为注释附件。最后,使用 remove() 方法从注释集合中删除注释附件。以下是详细步骤。
- 创建一个 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("C:\\Users\\Administrator\\Desktop\\添加注释附件.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();
    }
}申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



