Embed or Attach a File to a PDF on Android
PSPDFKit supports embedding files inside PDF documents via file annotations. You can embed any file type, including PDFs that embed another PDF which, in turn, embeds a PDF, and so on. There’s also basic UI support for sharing files embedded in file annotations via Android’s share framework.
Embedding files can be a handy feature for various business cases. For example, you can embed a photo of a driver’s license in an application form (a PDF document) or an Excel file in a monthly report (a PDF document).
Creating File Annotations
FileAnnotation
can be created programmatically in the same way as other supported annotations. The source for the embedded file needs to be wrapped in the EmbeddedFileSource
, and you’ll need to provide at least a DataProvider
serving file data, and its name.
The following example shows how to create a file annotation with data served from application assets:
// Create a file annotation instance. val fileAnnotation = FileAnnotation( // Page index 0. 0, // Page rectangle (in PDF coordinates). RectF(180f, 692f, 212f, 660f), // Embedded file source serving file data from assets. EmbeddedFileSource( AssetDataProvider("FileInAssets.pdf"), "FileInAssets.pdf", "Optional file description" )) // Multiple file icons are supported. fileAnnotation.iconName = FileAnnotation.GRAPH // Annotation color is supported too. fileAnnotation.color = Color.GREEN // Add the created file annotation to the page. document.annotationProvider.addAnnotationToPage(fileAnnotation)
// Create a file annotation instance. final FileAnnotation fileAnnotation = new FileAnnotation( // Page index 0. 0, // Page rectangle (in PDF coordinates). new RectF(180, 692, 212, 660), // Embedded file source serving file data from assets. new EmbeddedFileSource( new AssetDataProvider("FileInAssets.pdf"), "FileInAssets.pdf", "Optional file description" )); // Multiple file icons are supported. fileAnnotation.setIconName(FileAnnotation.GRAPH); // Annotation color is supported too. fileAnnotation.setColor(Color.GREEN); // Add the created file annotation to the page. document.getAnnotationProvider().addAnnotationToPage(fileAnnotation);
For additional examples, take a look at FileAnnotationCreationExample
inside the Catalog app.