Java 插入附件到PDF文档

  在文档中插入附件,可以起到与源文档配套使用的目的,以一种更简便的方式对文档起到补充说明的作用。下面将介绍通过Java编程插入附件到PDF文档中的方法。这里插入的文档可以是常见的文档类型,如Word、Excel、Ppt、Txt或者其他文件类型。插入方法,分两种情况,一种是直接加载文档内容作为附件添加到PDF文档,另一种是通过给PDF文档添加注释并添加文档到注释的形式。两种方式中可根据文档需要,选择相应的附件添加方法。

  使用工具:Free Spire.PDF for Java v2.2.2(免费版)

  关于jar文件添加:

  步骤1:下载安装包,解压。并复制文件夹lib下的jar文件。

  


  步骤2:在程序中新建一个directory目录,将步骤1中的jar文件复制到该目录下。

  步骤3:鼠标选中复制后的jar文件,点击鼠标右键,选择“Add as Library”。完成jar文件导入。

  Java代码(供参考)

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 {

  public static void main(String[] args) throws IOException {

  //实例化PdfDocument类的对象

  PdfDocument doc = new PdfDocument();

  //加载需要添加附件的PDF文档

  doc.loadFromFile(test.pdf);

  //加载附件文档(Excel)并作为附件添加到PDF

  PdfAttachment attachment = new PdfAttachment(Sample.xlsx);

  doc.getAttachments().add(attachment);

  //在PDF页面指定位置绘制标签

  String label = TestReport.docx;

  PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(Arial, Font.BOLD, 14));

  double x = 40;

  double y = doc.getPages().get(0).getActualSize().getHeight() -800;

  doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);

  //以注释的形式添加附件到PDF

  String filePath = 测试文档.docx;

  byte[] data = toByteArray(filePath);

  Dimension2D size = font.measureString(label);

  Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 3), (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(点击打开测试报告文档.docx);

  doc.getPages().get(0).getAnnotationsWidget().add(annotation);

  //保存文档

  doc.saveToFile(Attachments.pdf);

  }

  //读取文件到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;

  }

  }

  附件添加效果(如下图):

  


转载于:https://juejin.im/post/5cad9922e51d456e537ef2b1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值