美文网首页程序员
Java 添加、删除PDF附件

Java 添加、删除PDF附件

作者: Tina_Tang | 来源:发表于2020-03-16 09:58 被阅读0次

在PDF文档中,附件是用于展示关联信息的文件。在文档中插入附件,可以起到补充和丰富文件内容的作用。一般来说,附件的文档类型可以是常见的,如Word、Excel、PPT等,也可是其他文件类型。除此之外,附件在文档中有两种存在形式,一种是普通的文件附件,另一种则是注释附件。本文将通过使用Java程序来演示如何在PDF文档中添加和删除以上两种附件形式。

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

Jar文件获取及导入:

方法1通过官网下载获取jar包。解压后将lib文件夹下的Spire.Pdf.jar文件导入Java程序。(如下图)

方法2通过maven仓库安装导入。具体安装详解参见此网页

【示例1】添加附件

Part 1 添加文件附件

import com.spire.pdf.PdfDocument;

import com.spire.pdf.attachments.PdfAttachment;

public class AddAttachments {

public static void main(String[] args) {

//创建PdfDocument对象

        PdfDocument doc = new PdfDocument();

//加载PDF文档

        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//添加附件到PDF

        PdfAttachment attachment = new PdfAttachment("C:\\Users\\Test1\\Desktop\\注意事项说明书.docx");

       doc.getAttachments().add(attachment);

//保存文档

        doc.saveToFile("output/Attachments.pdf");

    }

    }

添加效果:

Part 2 添加注释附件

import com.spire.pdf.PdfDocument;

import com.spire.pdf.annotations.*;

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 AddAnnotationAttachment{

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

//创建PdfDocument对象

        PdfDocument doc = new PdfDocument();

//加载PDF文档

        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//绘制标签

        String label = "财务报表.xlsx";

        PdfTrueTypeFont font =new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);

double x = 35;

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

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

//添加注释附件到PDF

        String filePath = "C:\\Users\\Test1\\Desktop\\财务报表.xlsx";

byte[] data =toByteArray(filePath);

        Dimension2D size =font.measureString(label);

        Rectangle2D bound =new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);

        PdfAttachmentAnnotationannotation =new PdfAttachmentAnnotation(bound,filePath, data);

        annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));

       annotation.setFlags(PdfAnnotationFlags.Default);

       annotation.setIcon(PdfAttachmentIcon.Graph);

        annotation.setText("点击打开财务报表.xlsx");

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

//保存文档

        doc.saveToFile("output/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】删除附件

Part 1 删除文件附件

import com.spire.pdf.PdfDocument;

import com.spire.pdf.attachments.PdfAttachmentCollection;

public class DeleteAttachments {

public static void main(String[] args) {

//加载PDF文档

        PdfDocument doc = new PdfDocument();

        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Attachments.pdf");

//获取附件集合(不包含注释附件)

        PdfAttachmentCollection attachments = doc.getAttachments();

//删除所有附件

        attachments.clear();

//删除指定附件

        //attachments.removeAt(0);

 //保存文档

        doc.saveToFile("output/DeleteAttachments.pdf");

        doc.close();

    }

}

Part 2 删除注释附件

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 DeleteAnnotationAttachments {

public static void main(String[] args) {

//加载PDF文档

        PdfDocument doc = new PdfDocument();

        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Attachments.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("output/DeleteAnnotationAttachments.pdf");

        doc.close();

    }

}

(本文完)

相关文章

网友评论

    本文标题:Java 添加、删除PDF附件

    本文链接:https://www.haomeiwen.com/subject/zqpwehtx.html