Java PDF添加附件

    xiaoxiao2022-07-02  101

    Free-Spire-PDF-API地址: http://www.e-iceblue.cn/Introduce/Free-Spire-PDF-JAVA.html

    pom.xml maven依赖导入

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.design.patterns</groupId> <artifactId>design-patterns</artifactId> <version>0.0.1-SNAPSHOT</version> <name>design-patterns</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-xtra</artifactId> <version>5.5.13</version> </dependency> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>2.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

    代码

    package com.design.patterns.designpatterns.pdf; import com.spire.pdf.PdfDocument; import com.spire.pdf.annotations.*; import com.spire.pdf.attachments.PdfAttachment; import com.spire.pdf.graphics.*; 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 AttachFiles { static String src = "E:\\pdf\\qqq.pdf"; // 原文件 static String dest = "E:\\pdf\\ccc.pdf"; // 最终文件 static String att = "E:\\pdf\\666.mp4"; // 附件 static String desc = "附件"; // 附件注释 public static void main(String[] args) throws IOException { //创建PdfDocument对象 PdfDocument doc = new PdfDocument(); //加载PDF文档 doc.loadFromFile(src); //添加附件到PDF PdfAttachment attachment = new PdfAttachment(att); attachment.setDescription(desc); doc.getAttachments().add(attachment); int count = doc.getPages().getCount() - 1; //绘制标签 String label = "视频.mp4"; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true); double x = 35; double y = doc.getPages().get(count).getActualSize().getHeight() - 800; doc.getPages().get(count).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y); //添加注释附件到PDF String filePath = att; byte[] data = toByteArray(filePath); Dimension2D size = font.measureString(label); Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (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("双击打开文件"); doc.getPages().get(0).getAnnotationsWidget().add(annotation); //保存文档 doc.saveToFile(dest); } //读取文件到byte数组 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("file too big..."); 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("Could not completely read file " + file.getName()); } fi.close(); return buffer; } }

    效果:

     

    GitHup: https://github.com/liu911025/pdf-addAttachments/tree/master

    最新回复(0)