java - 如何添加指向PDF文档文件附件的链接

标签 java pdfbox

我正在使用 PDFBox,我想知道是否可以将文件添加为附件并链接到它?

我们使用下面的代码https://svn.apache.org/viewvc/pdfbox/tags/1.8.9/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java举个例子:

        ...
        // create a new tree node and add the embedded file 
        PDEmbeddedFilesNameTreeNode treeNode = new PDEmbeddedFilesNameTreeNode();
        treeNode.setNames( Collections.singletonMap( "My first attachment",  fs ) );
        // add the new node as kid to the root node
        List<PDEmbeddedFilesNameTreeNode> kids = new ArrayList<PDEmbeddedFilesNameTreeNode>();
        kids.add(treeNode);
        efTree.setKids(kids);
        // add the tree to the document catalog
        PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
        names.setEmbeddedFiles( efTree );
        doc.getDocumentCatalog().setNames( names );
        ...

最佳答案

您可以尝试使用“PDAnnotationFileAttachment”。它将提供注释。通过单击注释,您可以打开附件。

示例代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.pdmodel.graphics.color.PDGamma;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFileAttachment;

public class FileAttach {

    public static void main(String arg[]) throws IOException, COSVisitorException
    {
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);

        // Adding attachment
        String caption = "DEMO.mp4";
//        PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
//        final Map<String, PDComplexFileSpecification> embeddedFileMap = new HashMap<String, PDComplexFileSpecification>();

        PDComplexFileSpecification fs = new PDComplexFileSpecification();
        File f1 = new File("C:\\PDF\\DEMO.mp4");
        FileInputStream fins1 = new FileInputStream(f1);
        PDEmbeddedFile ef = new PDEmbeddedFile(doc, fins1 );
        ef.setSubtype( "application/octet-stream" );
        fs.setEmbeddedFile( ef );
        fs.setFile(caption);
//        embeddedFileMap.put(caption, fs);

//        efTree.setNames(embeddedFileMap);

//        PDDocumentNameDictionary nameDir = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
//        nameDir.setEmbeddedFiles( efTree );
//        doc.getDocumentCatalog().setNames( nameDir );

        int offsetX = 20;
        int offsetY = 600;

        //PDAnnotationLink txtLink = new PDAnnotationLink();
        PDAnnotationFileAttachment txtLink = new PDAnnotationFileAttachment();
        txtLink.setFile(fs);
        PDGamma blueColor = new PDGamma();
        blueColor.setB(1);
        txtLink.setColour(blueColor);
        txtLink.setAnnotationFlags(PDAnnotationFileAttachment.FLAG_LOCKED);

        // Set the rectangle containing the link
        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(offsetX);
        position.setLowerLeftY(offsetY);  
        position.setUpperRightX(offsetX + 20);
        position.setUpperRightY(offsetY + 20);
        txtLink.setRectangle(position);

        page.getAnnotations().add(txtLink);

        doc.save("C:\\PDF\\Vishal\\aa.pdf");
        doc.close();
    }

}

关于java - 如何添加指向PDF文档文件附件的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465817/

相关文章:

java - 如何从 Amazon elastic map reduce 中的映射器访问文件内容?

java - pdfbox 标题版本信息错误

PDF表格提取

java - 如何使用 PDFBOX 从 pdf 中删除整个书签

java - PDF/A 已通过印前检查正确验证,但在线 pdf-tools 无法验证它

java - 如何(水平)对齐 PDFBox 中 PDTextField 的文本?

java - 我在哪里可以找到适合中级 Java 开发人员的优秀而简洁的 Android 开发教程?

java - 如何通过方法将item添加到HashMap中

java - 如何在java中将 ""保存“结果到文本文件中

java - 如何从 spring boot thymeleaf 获取输入值到java类?