How to add an excel as an attachment at some specific position to aspose pdf in java

I have a pdf which is created using aspose.

I want to add an excel file as attachment at a particular section of the pdf document .

Could you please help me with java code for this.

I am using aspose pdf 22.11 version.

I have tried with the solution mentioned in the aspose pdf guide.

That doesn’t work .

I hope the attachment file extension doesn’t matter but my requirement is to add the attachment at a particular position of a section in the PDF generated by aspose.

@rinkusm

Would you kindly share sample file i.e. PDF and Excel for our reference with which you are trying the code? Also, please make sure that you are using a valid or 30-days free temporary license before using any method of the API. We will further proceed to assist you accordingly.

@asad.ali
You can use any pdf and an excel with some data ,
You just have to attach the excel as attachment to pdf in some specific position, like after some content.

I am using the licensed version with the new license file and 22.11 jar.

Pdf conversion and other things are working fine b

@asad.ali

Let me simplify the query for better understanding.

I am following this but I want to add the attachment to the PDF (and show it as an attachment with the document) after some specific text in the PDF.

sample.pdf (3.0 KB)
you can take out any sample excel for attachment purpose.

Add the attachment to sample.pdf after the text in the pdf file (This is a small demonstration .pdf file -) on the first line after heading which can later opened as attachment by user.

@rinkusm

In order to add attachment link inside PDF, you need to add FileAttachmentAnnotation like below:

Document doc = new Document(dataDir + "sample.pdf");
        Page page = doc.getPages().get_Item(1);

        final FileSpecification fileSpecification = new FileSpecification();
        InputStream is = new FileInputStream(new File(dataDir + "sample.xlsx"));
        fileSpecification.setIncludeContents(true);
        fileSpecification.setContents(is);
        fileSpecification.setDescription("");
        fileSpecification.setName("");
        System.out.println("Size before attach: " +  doc.getEmbeddedFiles().size());
        doc.getEmbeddedFiles().add(fileSpecification);

        TextFragmentAbsorber absorber = new TextFragmentAbsorber(".pdf file");
        page.accept(absorber);

        for (TextFragment fragment : absorber.getTextFragments())
        {
            FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(fragment.getPage()
                    , fragment.getRectangle()
                    , doc.getEmbeddedFiles().get_Item(doc.getEmbeddedFiles().size()));
            fileAttachment.setIcon(FileIcon.Paperclip);
            // add FileAttachment Annotation to Annotations collection of first page
            //fileAttachment.setOpacity(0);
            //fileAttachment.getFile().setIncludeContents(false);
            fragment.getPage().getAnnotations().add(fileAttachment);
        }

        doc.save(dataDir + "hellofile.pdf");

This code snippet adds an annotation on the desired text. You can change the location of the icon as per your desire by changing the rectangle values. Upon double clicking the icon, the attachment will get opened. Please let us know in case you still face any issues.

@asad.ali
I have tried with the above code .

Now I get a clickable at that text. But the attachment is corrupted and doesn’t open at all.

(1) I want it to show as an attachment on the pdf after that particular text
(2) I want the attachment to be downloaded
IMG_20230123_135506.jpg (1.7 MB)

We were able to notice this issue in our environment as well. Therefore, we have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-42446

You can obtain Paid Support services if you need support on a priority basis, along with the direct access to our Paid Support management team.