Hi Robert,
you can try something like the code below. The file is embedded only once but available using a pushpin annotation and
on document level. This also works at least in my KDE PDF viewer.
Unfortunately it has another drawback: Adobe reader lists such embedded files twice in its attachments tab.
As long as we don't find a way to make Adobe reader 'merge' its entries I think the PDFAction approach is better, given
that probably Adobe Reader is much more spread.
Cheers,
Jens
public static void main(String[] args) throws DocumentException, IOException {
String filename = "/home/jens/tmpfs/test.pdf";
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
String filenameInPdf = "test.txt";
PdfFileSpecification spec = PdfFileSpecification.fileEmbedded(writer,
"/home/jens/test.txt", filenameInPdf, null);
// Attach the file on document-level.
writer.addFileAttachment("Attached to chapter \"Process Description\"", spec);
document.add(new Paragraph("Attached files:"));
// Add a "link" to the attachment on the current page.
PdfAnnotation annotation = new PdfAnnotation(writer, null);
annotation.put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT);
annotation.put(PdfName.CONTENTS, new PdfString(filenameInPdf, PdfObject.TEXT_UNICODE));
annotation.put(PdfName.FS, spec.getReference());
Chunk linkChunk = new Chunk("\u00a0\u00a0");
linkChunk.setAnnotation(annotation);
Phrase phrase = new Phrase(filenameInPdf);
phrase.add(linkChunk);
document.add(new Paragraph(phrase));
document.close();