Change the font color and font type of an attachment content using PDFBox

77 Views Asked by At

I'm currently working on adding an attachment to a PDF using the PDFBox library in Java. While referring to the code snippet, Adding a File Attachment

I successfully attached the document created using ByteArrayInputStream to the attachment. However, I noticed that the font type and color of the attachment's contents remain the same. How to modify the font color and type for the contents of the attachment.

The code snippet I used is as below,

        // 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);

Also I would like to know if it is possible to add image and tables in the attachment along with other contents using pdfBox library . I am using 2.0.25 version of pdf box

0

There are 0 best solutions below