附件未显示在pdf文档中 - 使用pdfbox创建

时间:2012-08-20 07:03:30

标签: pdf embed pdfbox attachment

我试图将swf文件附加到pdf文档。下面是我的代码(摘自pdfbox-examples)。虽然我可以看到该文件是根据文件的大小附加的 - 与&没有附件,我无法在pdf文档中看到/找到它。我确实看到了正确显示的文本内容。有人能告诉我我做错了什么吗?帮我解决这个问题?

        doc = new PDDocument();

        PDPage page = new PDPage();
        doc.addPage( page );
        PDFont font = PDType1Font.HELVETICA_BOLD;
        String inputFileName =   "sample.swf";

        InputStream fileInputStream = new FileInputStream(new File(inputFileName));

        PDEmbeddedFile ef = new PDEmbeddedFile(doc, fileInputStream );
        PDPageContentStream contentStream = new PDPageContentStream(doc, page,true,true);
        //embedded files are stored in a named tree
        PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
        //first create the file specification, which holds the embedded file
        PDComplexFileSpecification fs = new PDComplexFileSpecification();
        fs.setEmbeddedFile(ef);

        //now lets some of the optional parameters
        ef.setSubtype( "swf" );
        ef.setCreationDate( new GregorianCalendar() );

        //now add the entry to the embedded file tree and set in the document.
        Map<String, COSObjectable> efMap = new HashMap<String, COSObjectable>();
        efMap.put("My first attachment", fs );
        efTree.setNames( efMap );
        //attachments are stored as part of the "names" dictionary in the document catalog
        PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
        names.setEmbeddedFiles( efTree );
        doc.getDocumentCatalog().setNames( names );

3 个答案:

答案 0 :(得分:1)

在与同样的事情挣扎之后,我发现这是一个已知的问题。我想,附件还没有工作一段时间。 这是apache论坛上问题的link。 这里有一个黑客建议你可以使用。 我试过了,它有效!

  

我发现的其他工作是在你的PDEmbeddedFilesNameTreeNode上调用setNames后删除限制:((COSDictionary)   )efTree.getCOSObject())。removeItem(COSName.LIMITS); 丑陋的黑客,但它   有效,无需重新编译pdfbox

答案 1 :(得分:0)

要“找到”或查看PDF中的附件,您无法翻阅其页面以查找其中的任何痕迹(例如,注释)。

例如,在Acrobat Reader 9.x中,您必须单击左侧边栏上的“查看附件”图标(看起来像一个纸夹)。

答案 2 :(得分:0)

附件适用于新版本的PDFBox 2.0,

public static boolean addAtachement(final String fileName, final String... attachements) {
    if (Objects.isNull(fileName)) {
        throw new NullPointerException("fileName shouldn't be null");
    }

    if (Objects.isNull(attachements)) {
        throw new NullPointerException("attachements shouldn't be null");
    }

    Map<String, PDComplexFileSpecification> efMap = new HashMap<String, PDComplexFileSpecification>();
    /*
     * Load PDF Document.
     */
    try (PDDocument doc = PDDocument.load(new File(fileName))) {

        /*
         * Attachments are stored as part of the "names" dictionary in the
         * document catalog
         */
        PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());

        /*
         * First we need to get all the existed attachments, after that we
         * can add new attachments
         */
        PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
        if (Objects.isNull(efTree)) {
            efTree = new PDEmbeddedFilesNameTreeNode();
        }

        Map<String, PDComplexFileSpecification> existedNames = efTree.getNames();

        if (existedNames == null || existedNames.isEmpty()) {
            existedNames = new HashMap<String, PDComplexFileSpecification>();
        }

        for (String attachement : attachements) {
            /*
             * Create the file specification, which holds the embedded file
             */
            PDComplexFileSpecification fs = new PDComplexFileSpecification();

            fs.setFile(attachement);

            try (InputStream is = new FileInputStream(attachement)) {
                /*
                 * This represents an embedded file in a file specification
                 */
                PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);

                /* Set some relevant properties of embedded file */
                ef.setCreationDate(new GregorianCalendar());
                fs.setEmbeddedFile(ef);

                /*
                 * now add the entry to the embedded file tree and set in
                 * the document.
                 */
                efMap.put(attachement, fs);
            }
        }

        efTree.setNames(efMap);
        names.setEmbeddedFiles(efTree);
        doc.getDocumentCatalog().setNames(names);
        doc.save(fileName);
        return true;
    } catch (IOException e) {
        System.out.println(e.getMessage());
        return false;
    }

}