Hi Team,
I am trying to embed attachments to my pdf. I tried to follow following documentation:
Adding Attachment to PDF document|Aspose.PDF for Java…_gaMjEwMTA1NzY1LjE3NDYwMDcyMDM._ga_W0DG8XJWKLMTc0NjAwNzIwMy4xLjAuMTc0NjAwNzIwMy4wLjAuMTcyMTI0OTMyMw…
My service looks like:
public void createPdfWithEmbeddedFile(String filePath, OutputStream outputStream) throws FileNotFoundException, IOException {
        // Create a new PDF document
        Document pdfDocument = new Document();
        // Add a page to the document
        Page page = pdfDocument.getPages().add();
        ClassPathResource resource = new ClassPathResource(filePath);
        if(!resource.exists()){
            throw new IllegalArgumentException("File not found: " + filePath);
        }
        FileSpecification fileSpecification = new FileSpecification((filePath) , "Sample file");
        page.setBackground(Color.getGray());
        fileSpecification.setMIMEType(getMimeType(Objects.requireNonNull(resource.getFilename())));
        pdfDocument.getEmbeddedFiles().add(fileSpecification);
        pdfDocument.save(outputStream);
    }
 private String getMimeType(String fileName){
        String extension = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
        switch (extension){
            case "txt": return "text/plain";
            case "pdf": return "application/pdf";
            case "doc": return "application/msword";
            case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            case "ppt": return "application/vnd.ms-powerpoint";
            case "pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
            case "jpg": case "jpeg": return "image/jpeg";
            case "png": return "image/png";
            default: return "application/octet-stream";
        }
    }
When i try to open embedded attachment in Adobe Acrobat Reader DC, it is throwing error:
image.png (4.4 KB)
Expected a dict object.
I did a workaround and modified my function like this:
    public void createPdfWithEmbeddedFile(String filePath, OutputStream outputStream) throws FileNotFoundException, IOException {
        // Create a new PDF document
        Document pdfDocument = new Document();
        // Add a page to the document
        Page page = pdfDocument.getPages().add();
        ClassPathResource resource = new ClassPathResource(filePath);
        if(!resource.exists()){
            throw new IllegalArgumentException("File not found: " + filePath);
        }
        byte[] fileBytes;
        try(InputStream inputStream = resource.getInputStream()){
            fileBytes = inputStream.readAllBytes();
        }
        // Create a FileSpecification object for the file to be embedded
        page.setBackground(Color.getGray());
        FileSpecification fileSpecification = new FileSpecification();
        fileSpecification.setContents(fileBytes);
        fileSpecification.setMIMEType(getMimeType(Objects.requireNonNull(resource.getFilename())));
        fileSpecification.setName(resource.getFilename());
        // Add the file to the PDF document
        pdfDocument.getEmbeddedFiles().add(fileSpecification);
        pdfDocument.save(outputStream);
    }
Now for txt files, this method is working. But for embedded pdf - all pages comes as blank. For word files getting following error:
image.png (2.9 KB)
image.png (3.6 KB)
Please guide me in correct direction