Java 添加附件到PDF文档

 时间:2026-02-12 03:04:20

1、步骤1:在Java程序中新建一个文件夹可命名为Lib。并将以下路径中的2个jar文件复制到新建的文件夹下。

Java 添加附件到PDF文档

2、步骤2:复制文件后,添加到引用类库:选中这两个jar文件,点击鼠标右键,选择“Build Path” – “Add to Build Path”。完成引用。

Java 添加附件到PDF文档

1、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;

    }

}

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

Java 添加附件到PDF文档

  • 妙用搜狗浏览器中的账号助手一键登录微博邮箱
  • 树形结构网站与平行结构网站对比
  • 创业过程中要思考的问题
  • 嘴上起泡该咋办
  • 镀锌钢带的麻点漏镀是怎么造成的
  • 热门搜索
    yd是什么意思 11月8日是什么星座 考二建需要什么条件 gt是什么意思 rps是什么意思 星星像什么 2000年是什么年 min什么意思 9月18日是什么纪念日 pass是什么意思