Download link to embedded attachments in the PDF

Hi,
I have a requirement to create a download link inside my main pdf, that will download the respective attachments that are already embedded into the pdf file. This is what i have done so far:

//File Specification objects
        FileSpecification excel = new FileSpecification(excelStream, excelFileName, "Excel File");
        FileSpecification word = new FileSpecification(wordStream, wordFileName, "Word File");
        FileSpecification image = new FileSpecification(imageStream, imageName, "Image File");
        
        // Add files to document collection
        Collection collection = pdfDoc.getCollection();
        if (collection == null){
            collection = new Collection();
            pdfDoc.setCollection(collection);

        }
        pdfDoc.getCollection().add(excel);
        pdfDoc.getCollection().add(word);
        pdfDoc.getCollection().add(image);

        // Create link
        Page page = pdfDoc.getPages().get_Item(1);
    
        FileAttachmentAnnotation fileAttachmentAnnotation = new FileAttachmentAnnotation(page, new Rectangle(100, 200, 300, 300), excel);
  
        page.getAnnotations().add(fileAttachmentAnnotation);

        // Save Portfolio document
        pdfDoc.save(dataDir + "CreatePDFPortfolio_out.pdf");

The problem is that with the File Annotation, my embedded files do not get downloaded. I want a launch action or something that will download the respective embedded file when clicked on the link. Please guide how to achieve this

Hi,

To achieve that, you will have to create a LinkAnotation(LinkAnnotation | Aspose.PDF for Java API Reference) object using a Page and Rectangle Object.
You must pay attention to the setAction (LaunchAction | Aspose.PDF for Java API Reference) function of LinkAnotation because that will set the application that you want to start.
After all that you will have to add the link to the Page Obect´s annotation.

This example will give you a general overview on what I described above:
package com.aspose.pdf.examples;

import com.aspose.pdf.*;

public class ExampleLinks {

private static String _dataDir = "/home/aspose/pdf-examples/Samples/";

private static String GetDataDir() {
    String os = System.getProperty("os.name");
    if (os.startsWith("Windows"))
        _dataDir = "C:\\Samples\\Links-Actions";
    return _dataDir;
}

public static void CreateLink() {

    // Open document
    Document document = new Document(GetDataDir() + "CreateApplicationLink.pdf");

    // Create link
    Page page = document.getPages().get_Item(1);
    LinkAnnotation link = new LinkAnnotation(page, new Rectangle(100, 200, 300, 300));
    link.setColor(Color.getGreen());
    link.setAction(new LaunchAction(document, _dataDir + "sample.pdf"));
    page.getAnnotations().add(link);

    // Save updated document
    document.save(_dataDir + "CreateApplicationLink_out.pdf");
} 

I hope it helps.

I am assuming that the parameter of (String file) in Launch Action is the path to the file that i want to refer? But i have the embedded attachments and i won’t know their paths. I have FileSpecification objects in my program for embedded attachments. How can i define a launch action for my embedded attachments to download as an external file on click?

@ahmedtariq

Could you please attach your input and expected output PDF files? We will then provide you more information about your query.