Skip to content

Attachments

Foxit PDF SDK for Android provides embed, extract, enumerate, and delete APIs for document attachments (Embedded Files) through the Core SDK. Document attachments let you package arbitrary files into a PDF, similar to email attachments.

Document attachments vs file attachment annotations

This guide covers document-level attachments (Embedded Files) stored in the document name tree. File attachment annotations are page-level annotation types and are covered separately in the annotation documentation.

Built-in attachments panel in UI Extensions

If you use the full reader (UI Extensions), the attachments panel is built in; users can view, add, and export attachments without extra code.

Core Classes

ClassDescription
AttachmentsDocument attachment collection; add, remove, query, and update embedded files; convenience methods addFromFilePath() and extractEmbeddedFileTo()
FileSpecFile specification describing embedded file properties (name, size, timestamps, etc.); embed/export via path or stream
PDFNameTreePDF name tree; Attachments is built on the e_EmbeddedFiles name tree

Example: Embed Files in a PDF

java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.Attachments;
import com.foxit.sdk.pdf.FileSpec;
import com.foxit.sdk.pdf.objects.PDFNameTree;

PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);

PDFNameTree nameTree = new PDFNameTree(doc, PDFNameTree.e_EmbeddedFiles);
Attachments attachments = new Attachments(doc, nameTree);

// Method 1: embed via FileSpec
FileSpec fileSpec = new FileSpec(doc);
fileSpec.setFileName("report.xlsx");
fileSpec.embed("/sdcard/report.xlsx");
attachments.addEmbeddedFile("report.xlsx", fileSpec);

// Method 2: embed directly from file path (convenience)
attachments.addFromFilePath("image.png", "/sdcard/image.png");

doc.saveAs("path/to/output.pdf", PDFDoc.e_SaveFlagNormal);

Example: List and Export All Attachments

java
PDFNameTree nameTree = new PDFNameTree(doc, PDFNameTree.e_EmbeddedFiles);
Attachments attachments = new Attachments(doc, nameTree);

int count = attachments.getCount();
for (int i = 0; i < count; i++) {
    String key = attachments.getKey(i);
    FileSpec fileSpec = attachments.getEmbeddedFile(key);
    if (fileSpec != null && !fileSpec.isEmpty()) {
        String fileName = fileSpec.getFileName();
        // Export attachment
        fileSpec.exportToFile("/sdcard/exports/" + fileName);
    }
}

Example: Remove Attachments

java
// Remove a single attachment by key
attachments.removeEmbeddedFile("report.xlsx");

// Remove all attachments
attachments.removeAllEmbeddedFiles();

API Reference

See the API Reference for full Attachments and FileSpec documentation.