Extracting Attachments  SOLVED

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
mmasood
User
Posts: 101
Joined: Fri Sep 18, 2015 9:49 pm

Extracting Attachments

Post by mmasood »

Hi Alex,

I am trying to extract the attachments from a pdf file and save them. Following is the code that I have wrote so far:

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            IPXC_Document loDoc = null;
            int liResult = 0;
            string lsFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "AttachmentTest4.pdf");

            try
            {
                // load the pdf file
		
                //Getting IAFS_Inst from IPXV_Inst
                IAFS_Inst fsInst = (IAFS_Inst)mTrackerInstance.GetExtension("AFS");
		
                IPXC_NameTree loNameTree = loDoc.GetNameTree("EmbeddedFiles");
                
                for (int i=1; i<= loNameTree.Count; i++)
                {
                	string lsName = string.Empty;
                        IPXS_PDFVariant loPdfVariant = null;
			
			// get the name of the attachment
                        loNameTree.Item((uint)i - 1, out lsName, out loPdfVariant);

                        // Converting string to name
                        lsName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), lsName);
			
			// delete the file if it already exists on the file system
                        if (File.Exists(lsName))
                        {
                            File.Delete(lsName);
                        }
                        
                        //
                        // what do I need to do here to save the attachment
                        //

                        Marshal.ReleaseComObject(loPdfVariant);
                        loPdfVariant = null;
                    }

                    Marshal.ReleaseComObject(loNameTree);
                    loNameTree = null;
                    loDoc.Close();
                    loDoc = null;
                    Marshal.ReleaseComObject(fsInst);
                    fsInst = null;
                }
            }
            catch(Exception loException)
            {
                liResult = -2;
            }
            finally
            {
                if (loDoc != null)
                {
                    loDoc.Close();
                    loDoc = null;
                }
            }
        }
Can you provide me with the code that I need here to extract the attachment from the pdf file?

Regards,
M
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Extracting Attachments  SOLVED

Post by Sasha - Tracker Dev Team »

Hello mmasood,

Check the CoreAPIDemo project's OpenAttachment method - it saves the attachment to the temporary file before opening:

Code: Select all

\\CoreAPIDemo\Form1.cs(1258)
public void OpenAttachment()
		{
			if (m_CurDoc == null)
				return;
			if (attachmentView.SelectedItems.Count == 0)
				return;

			IAFS_Inst afsInst = m_pxcInst.GetExtension("AFS");
			ListItemAttachment item = attachmentView.SelectedItems[0] as ListItemAttachment;

			string sType = item.SubItems[0].Text.Split('.')[1];
			m_sFilePath = Path.GetTempFileName();
			m_sFilePath = m_sFilePath.Replace(".tmp", "." + sType);
			bool bIsSuccessful = false;
			IPXC_Document coreDoc = null;
			do
			{
				//Check whether it's a PDF file
				try
				{
					IAFS_Name name = afsInst.DefaultFileSys.StringToName(m_sFilePath);
					IAFS_File file = afsInst.DefaultFileSys.OpenFile(name, (int)AFS_OpenFileFlags.AFS_OpenFile_Write | (int)AFS_OpenFileFlags.AFS_OpenFile_Read
						| (int)AFS_OpenFileFlags.AFS_OpenFile_CreateNew | (int)AFS_OpenFileFlags.AFS_OpenFile_ShareRead);

					item.m_pxcEmbeddedFileStream.SaveToFile(file);
					file.Close();

					coreDoc = m_pxcInst.OpenDocumentFromFile(m_sFilePath, null);
					bIsSuccessful = true;
					break;
				}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
mmasood
User
Posts: 101
Joined: Fri Sep 18, 2015 9:49 pm

Re: Extracting Attachments

Post by mmasood »

Hi Alex,

Thanks I was able to extract the code and get the code working.

Regards,
M
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Extracting Attachments

Post by TrackerSupp-Daniel »

:D
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Post Reply