代码之家  ›  专栏  ›  技术社区  ›  Rosa Mystica

使用PDFBox更改附件内容的字体颜色和字体类型

  •  0
  • Rosa Mystica  · 技术社区  · 2 年前

    我目前正在使用Java中的PDFBox库为PDF添加附件。在参考代码片段的同时, Adding a File Attachment

    我成功地将使用ByteArrayInputStream创建的文档附加到了附件中。然而,我注意到附件内容的字体类型和颜色保持不变。如何修改附件内容的字体颜色和类型。

    我使用的代码片段如下,

            // create a new tree node and add the embedded file
            PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
            Map<String, PDComplexFileSpecification> treeNodeMap = new HashMap<>();
    
            for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
    
                // first create the file specification, which holds the embedded file
                PDComplexFileSpecification fs = new PDComplexFileSpecification();
                fs.setFile(entry.getKey());
                // create a dummy file stream, this would probably normally be a FileInputStream
                byte[] data = entry.getValue().getBytes();
                ByteArrayInputStream fakeFile = new ByteArrayInputStream(data);              
                // now lets some of the optional parameters
                PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile);
                ef.setSubtype("text/plain");
                ef.setSize(data.length);
                ef.setCreationDate(Calendar.getInstance());
                fs.setEmbeddedFile(ef);
                treeNodeMap.put(entry.getKey(), fs);
            }
    
            efTree.setNames(treeNodeMap);
    
            // add the tree to the document catalog
            PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
            names.setEmbeddedFiles(efTree);
            doc.getDocumentCatalog().setNames(names);
    

    此外,我想知道是否可以使用pdfBox库在附件中添加图像和表以及其他内容。我使用的是2.0.25版本的pdf盒子

    0 回复  |  直到 2 年前
    推荐文章