手动引入。将 Free Spire.PDF for Java 下载到本地,解压,找到 lib 文件夹下的 Spire.PDF.jar 文件。在 IDEA 中打开如下界面,将本地路径中的 jar 文件引入 Java 程序:
<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 添加附件
import com.spire.pdf.PdfDocument; import com.spire.pdf.attachments.PdfAttachment; public class AttachFilesToPdf { public static void main(String[] args) { //创建一个 PdfDocument 对象 www.duidaima.com PdfDocument doc = new PdfDocument(); //加载 PDF 文档 doc.loadFromFile("什么是AI数字人.pdf"); //基于外部文件创建 PdfAttachment 对象 PdfAttachment attachment = new PdfAttachment("到场嘉宾名单.xlsx"); //将附件添加到 PDF doc.getAttachments().add(attachment); //保存文件 doc.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 { //创建一个 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 = 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; } }效果图
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(); } }
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 对象 www.duidaima.com 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(); } }