We have some PDFs that contain file attachments (e.g. an embedded JPEG file). In Adobe Reader, we could double-click the link to open the jpg file in Windows Photo Viewer. While, in our PDF Viewer (using PDFNet) or your PDFViewer sample application, when double-clicking the link we only got a small empty "Sticky Note" window. We are wondering whether this kind of internal or external file links in a pdf document is supported by PDFNet or not? Could we implement something using PDFNet SDK so that the file link will work?
------------------
You can use the following code as a starting point to extract file attachments via PDFViewCtrl (WinForms):
protected override void OnMouseDoubleClick(System.Windows.Forms.MouseEventArgs e)
{
// base.OnMouseDown(e); // First process the event in the base class
int page_num = GetPageNumberFromScreenPt(e.X, e.Y);
if (page_num < 1) return;
// Find the click point in page coordinate system... is there a file attachment annotation at this point?
double x = e.X, y = e.Y;
ConvScreenPtToPagePt(ref x, ref y, page_num);
Page page = GetDoc().GetPage(page_num);
int annot_num = page.GetNumAnnots();
for (int i = 0; i < annot_num; ++i)
{
Annot annot = page.GetAnnot(i);
// Process only file attachment annotations...
if (annot.IsValid() == false ||
annot.GetType() != Annot.Type.e_FileAttachment) continue;
Rect box = annot.GetRect();
if (box.Contains(x, y)) {
// Extract the file attachment ...
// See https://groups.google.com/d/msg/pdfnet-sdk/gA8o_eKVG7c/kc0BsgEhif0J
pdftron.PDF.Annots.FileAttachment fileAttachment = new pdftron.PDF.Annots.FileAttachment(annot);
FileSpec file_spec = fileAttachment.GetFileSpec();
using (Filter stm = file_spec.GetFileData()) {
if (stm != null) {
FilterReader reader = new FilterReader(stm);
// use file_spec.GetFilePath() to get the filename...
using (StdFile out_file = new StdFile("c:/1.jpg", StdFile.OpenMode.e_write_mode)) {
FilterWriter writer = new pdftron.Filters.FilterWriter(out_file);
writer.WriteFilter(reader);
writer.Flush();
}
// Launch the attachment in an external viewer ... ?
try {
System.Diagnostics.Process.Start("c:/1.jpg");
}
catch (System.ComponentModel.Win32Exception noBrowser) {
if (noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other) {
MessageBox.Show(other.Message);
}
}
}
}
}
}
Please keep in mind that extracting and executing file attachments from PDF is potentially a dangerous operations (since embedded files may also contain executables, viruses, etc.).
Filter stm = file_spec.GetFileData();
stm.WriteToFile(path);