0

我正在尝试将 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 );
4

3 回答 3

1

在经历了同样的事情之后,我发现这是一个已知问题。我猜附件已经有一段时间没有工作了。这是apache 论坛上的问题的链接。这里有一个建议你可以使用的 hack。我试过了,它奏效了!

我发现的其他解决方法是在您调用 pDEmbeddedFilesNameTreeNode 上的 setNames 后删除限制: ((COSDictionary )efTree.getCOSObject()).removeItem(COSName.LIMITS); 丑陋的hack,但它可以工作,无需重新编译pdfbox

于 2012-09-04T03:52:22.817 回答
0

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

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

于 2012-08-20T22:10:37.560 回答
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;
    }

}
于 2017-06-22T10:55:16.410 回答