<< Click to Display Table of Contents >> Navigation: Create a PDF Editor > .NET (C#) Example > Extract Attachments |
To extract attachments of a PDF we have added a menu item to the "Info" menu. This menu item will get a drop down with all attachment names in the current document.
This event is triggered when the "Info" menu drops down:
private void infoToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
menFileattachment.DropDownItems.Clear();
int j = pdfViewer1.Command(commands.COMPDF_Attachment_List);
for(int i = 0; i<j;i++)
{
System.Windows.Forms.ToolStripMenuItem men = new System.Windows.Forms.ToolStripMenuItem();
men.Text = pdfViewer1.CommandGetStr( commands.COMPDF_Attachment_GetProp, "", i );
men.Tag = i;
men.Click += new System.EventHandler(OnClickAttachment);
menFileattachment.DropDownItems.Add(men);
}
if(j<=0) menFileattachment.DropDownItems.Add("<empty>").Enabled = false;
}
This event handles the click on any of the attachment menu items to save the attachment to a file.
private void OnClickAttachment(object sender, EventArgs e)
{
System.Windows.Forms.ToolStripMenuItem men = sender as System.Windows.Forms.ToolStripMenuItem;
int l = pdfViewer1.Command(commands.COMPDF_Attachment_GetData, (int)men.Tag);
if (l > 0)
{
byte[] databytes = pdfViewer1.GetMemory();
saveFileDialog1.FileName = men.Text;
if ( saveFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK )
{
System.IO.FileStream file = new System.IO.FileStream(saveFileDialog1.FileName, System.IO.FileMode.Create );
file.Write(databytes,0,databytes.Length);
file.Close();
}
}
}