Page 1 of 1

Extracting Attachments

Posted: Tue Nov 20, 2018 7:07 pm
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

Re: Extracting Attachments  SOLVED

Posted: Wed Nov 21, 2018 9:01 am
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

Re: Extracting Attachments

Posted: Fri Nov 30, 2018 10:02 pm
by mmasood
Hi Alex,

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

Regards,
M

Re: Extracting Attachments

Posted: Fri Nov 30, 2018 10:07 pm
by TrackerSupp-Daniel
:D