Java PDF添加附件

短命女 2022-01-29 14:04 454阅读 0赞

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

pom.xml maven依赖导入

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.design.patterns</groupId>
  12. <artifactId>design-patterns</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>design-patterns</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <repositories>
  20. <repository>
  21. <id>com.e-iceblue</id>
  22. <name>e-iceblue</name>
  23. <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
  24. </repository>
  25. </repositories>
  26. <dependencies>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-test</artifactId>
  34. <scope>test</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.itextpdf</groupId>
  38. <artifactId>itextpdf</artifactId>
  39. <version>5.4.3</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>com.itextpdf</groupId>
  43. <artifactId>itext-asian</artifactId>
  44. <version>5.2.0</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.itextpdf</groupId>
  48. <artifactId>itext-xtra</artifactId>
  49. <version>5.5.13</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>e-iceblue</groupId>
  53. <artifactId>spire.pdf.free</artifactId>
  54. <version>2.2.2</version>
  55. </dependency>
  56. </dependencies>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

代码

  1. package com.design.patterns.designpatterns.pdf;
  2. import com.spire.pdf.PdfDocument;
  3. import com.spire.pdf.annotations.*;
  4. import com.spire.pdf.attachments.PdfAttachment;
  5. import com.spire.pdf.graphics.*;
  6. import java.awt.*;
  7. import java.awt.geom.Dimension2D;
  8. import java.awt.geom.Rectangle2D;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. public class AttachFiles {
  13. static String src = "E:\\pdf\\qqq.pdf"; // 原文件
  14. static String dest = "E:\\pdf\\ccc.pdf"; // 最终文件
  15. static String att = "E:\\pdf\\666.mp4"; // 附件
  16. static String desc = "附件"; // 附件注释
  17. public static void main(String[] args) throws IOException {
  18. //创建PdfDocument对象
  19. PdfDocument doc = new PdfDocument();
  20. //加载PDF文档
  21. doc.loadFromFile(src);
  22. //添加附件到PDF
  23. PdfAttachment attachment = new PdfAttachment(att);
  24. attachment.setDescription(desc);
  25. doc.getAttachments().add(attachment);
  26. int count = doc.getPages().getCount() - 1;
  27. //绘制标签
  28. String label = "视频.mp4";
  29. PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);
  30. double x = 35;
  31. double y = doc.getPages().get(count).getActualSize().getHeight() - 800;
  32. doc.getPages().get(count).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);
  33. //添加注释附件到PDF
  34. String filePath = att;
  35. byte[] data = toByteArray(filePath);
  36. Dimension2D size = font.measureString(label);
  37. Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);
  38. PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
  39. annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
  40. annotation.setFlags(PdfAnnotationFlags.Default);
  41. annotation.setIcon(PdfAttachmentIcon.Graph);
  42. annotation.setText("双击打开文件");
  43. doc.getPages().get(0).getAnnotationsWidget().add(annotation);
  44. //保存文档
  45. doc.saveToFile(dest);
  46. }
  47. //读取文件到byte数组
  48. public static byte[] toByteArray(String filePath) throws IOException {
  49. File file = new File(filePath);
  50. long fileSize = file.length();
  51. if (fileSize > Integer.MAX_VALUE) {
  52. System.out.println("file too big...");
  53. return null;
  54. }
  55. FileInputStream fi = new FileInputStream(file);
  56. byte[] buffer = new byte[(int) fileSize];
  57. int offset = 0;
  58. int numRead = 0;
  59. while (offset < buffer.length
  60. && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
  61. offset += numRead;
  62. }
  63. if (offset != buffer.length) {
  64. throw new IOException("Could not completely read file "
  65. + file.getName());
  66. }
  67. fi.close();
  68. return buffer;
  69. }
  70. }

效果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdTkxMTAyNQ_size_16_color_FFFFFF_t_70

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

发表评论

表情:
评论列表 (有 0 条评论,454人围观)

还没有评论,来说两句吧...

相关阅读