Sometimes we need to make use of the attachments added to the PDF files. There are 2 kinds of attachment in pdf, one is common attachment that added to the file directly, the other is attachment annotations which is add to the particular place as an annotation like the following picture, when you click correlative icon, that attachment will be opened.
In this article, we will learn how to extract these two kinds of attachment in C# via Spire.PdfViewer.
Here are the steps:
Step 1: Download PdfViewer, add PdfViewer Control to VS Toolbox (How to add control).
Step 2: Create a Windows Forms application, design Form as below.
- Two buttons respectively for common attachment and attachment annotation, and set name for them.
- One open file button and one close attachment button.
- A PdfDocmentViewer in the middle and one ViewList on the bottom.
Step 3: Get attachments
1) Get common attachment via methord GetAttachments(), then traverse attachment array and get common attachment property.
PdfDocumentAttachment[] attchments = this.pdfDocumentViewer1.GetAttachments(); if (attchments != null && attchments.Length > 0) { for (int i = 0; i < attchments.Length; i++) { PdfDocumentAttachment attachment = attchments[i]; string fileName = attachment.FileName; string mimeType = attachment.MimeType; string desc = attachment.Description; DateTime createDate = attachment.CreationTime; DateTime modifyDate = attachment.ModifyTime; Object data = attachment.Data; ListViewItem item = new ListViewItem(); item.Text = Path.GetFileName(fileName); item.SubItems.Add(mimeType); item.SubItems.Add(desc); item.SubItems.Add(createDate.ToShortDateString()); item.SubItems.Add(modifyDate.ToShortDateString()); item.Tag = attachment; this.listView1.Items.Add(item); } }
Results:
2) Extract attachment annotation:
Get attachment annotations via methord GetAttachmentAnnotaions(). Then traverse annotation array and save each annotation property as an individual item in listview.
PdfDocumentAttachmentAnnotation[] annotations = this.pdfDocumentViewer1.GetAttachmentAnnotaions(); if (annotations != null && annotations.Length > 0) { for (int i = 0; i < annotations.Length; i++) { PdfDocumentAttachmentAnnotation annotation = annotations[i]; ListViewItem item = new ListViewItem(annotation.FileName); item.SubItems.Add(annotation.Text); item.SubItems.Add(annotation.PageIndex.ToString()); item.SubItems.Add(annotation.Location.ToString()); item.Tag = annotation; this.listView1.Items.Add(item); } }
Results:
3) ListView
Here if we click the file name information of annotation attachment in the listView, PdfDocumentViewer will go to specified attachment annotation.
if (this.m_isAttachmentAnnotation) { PdfDocumentAttachmentAnnotation annotation = (PdfDocumentAttachmentAnnotation)this.listView1.SelectedItems[0].Tag; this.pdfDocumentViewer1.GotoAttachmentAnnotation(annotation); }
Double click it, the attachment can be saved to local. Get data of annotation, and write into file.
PdfDocumentAttachmentAnnotation annotation = (PdfDocumentAttachmentAnnotation)item.Tag; byte[] data = annotation.Data; writer.Write(data);
About saving common attachment:
PdfDocumentAttachment annotation = (PdfDocumentAttachment)item.Tag; byte[] data = annotation.Data; writer.Write(data);
Spire.PDFViewer is a powerful PDF Viewer component performed on .NET and WPF. It enables developers to load PDF document from stream, file and byte array. Also it supports rotation, page layout setup and thumbnail control. Worth a shot. Click to know more