/// <summary> /// Export PDF document attachment /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listView1_DoubleClick(object sender, EventArgs e) { ListViewItem item = this.listView1.SelectedItems[0]; SaveFileDialog dialog = new SaveFileDialog(); DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { string fileName = dialog.FileName; FileStream stream = new FileStream(fileName, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); if (this.m_isAttachmentAnnotation) { PdfDocumentAttachmentAnnotation annotation = (PdfDocumentAttachmentAnnotation)item.Tag; byte[] data = annotation.Data; writer.Write(data); } else { PdfDocumentAttachment annotation = (PdfDocumentAttachment)item.Tag; byte[] data = annotation.Data; writer.Write(data); } writer.Close(); stream.Close(); System.Diagnostics.Process.Start(fileName); } }
/// <summary> /// Read PDF document attachment annotation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAttachmentAnnotation_Click(object sender, EventArgs e) { this.tableLayoutPanel1.SetRowSpan(this.pdfDocumentViewer1, 1); this.m_isAttachmentAnnotation = true; this.listView1.Visible = true; this.listView1.Items.Clear(); this.listView1.Columns.Clear(); if (this.pdfDocumentViewer1.IsDocumentLoaded && this.pdfDocumentViewer1.PageCount > 0) { this.listView1.View = View.Details; this.listView1.Columns.Add("FileName", 200); this.listView1.Columns.Add("Text", 180); this.listView1.Columns.Add("PageIndex", 80); this.listView1.Columns.Add("Location", 160); //Get attachment annotations in PDF document PdfDocumentAttachmentAnnotation[] annotations = this.pdfDocumentViewer1.GetAttachmentAnnotaions(); if (annotations != null && annotations.Length > 0) { //read attachment annotation property 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); } } } }
/// <summary> /// Go to Specified attachment annotation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listView1_Click(object sender, EventArgs e) { if (this.m_isAttachmentAnnotation) { PdfDocumentAttachmentAnnotation annotation = (PdfDocumentAttachmentAnnotation)this.listView1.SelectedItems[0].Tag; this.pdfDocumentViewer1.GotoAttachmentAnnotation(annotation); } }
//export attachment data to file private void listView1_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (this.listView1.SelectedItem != null && this.listView1.SelectedItem is PdfDocumentAttachment) { PdfDocumentAttachment attachment = this.listView1.SelectedItem as PdfDocumentAttachment; SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "All files(*.*)|*.*"; bool?isOk = dialog.ShowDialog(); if (isOk.Value) { string fileName = dialog.FileName; attachment.SaveAS(fileName); string info = "Export attachment data to file: " + "\n" + fileName; MessageBox.Show(info, "Export", MessageBoxButton.OK, MessageBoxImage.Information); try { System.Diagnostics.Process.Start(fileName); } catch { } } return; } if (this.listView1.SelectedItem != null && this.listView1.SelectedItem is PdfDocumentAttachmentAnnotation) { PdfDocumentAttachmentAnnotation annotation = this.listView1.SelectedItem as PdfDocumentAttachmentAnnotation; SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "All files(*.*)|*.*"; bool?isOk = dialog.ShowDialog(); if (isOk.Value) { string fileName = dialog.FileName; annotation.SaveAs(fileName); string info = "Export attachment data to file: " + "\n" + fileName; MessageBox.Show(info, "Export", MessageBoxButton.OK, MessageBoxImage.Information); try { System.Diagnostics.Process.Start(fileName); } catch { } } //can go to specify PdfDocumentAttachmentAnnotation instance this.pdfDocumentViewer1.GotoAttachmentAnnotation(annotation); } }