We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Check if msg (with attached pdf) file is a pdf file

Hello everyone.

I have faced an issue checking .msg file (containing attached pdf files) whether it is (or not) a pdf file.

I am using the following piece of code:

Aspose.Pdf.Facades.PdfFileInfo fileInfo = new Aspose.Pdf.Facades.PdfFileInfo(msgStream);

boolean isPdf = fileInfo.IsPdfFile;

the mentioned code returns true if my msg file has any pdfs attached so I cannot determine if this is a common pdf file or this is a msg file containing pdf file.

I was wondering if you could help me to find the way how to solve this.

Thanks in advance.

Hi,


Thanks for contacting support.

The reason you are getting IsPdfFile property as true, is because you are passing a stream object to the PdfFileInfo and in a case if stream contains PDF file (either as attachment or complete PDF file), it will return true.

As per my understanding, you want to determine if file is common PDF file or outlook MSG file. So in order to achieve that, you can use Path.GetExtension() method to check the file format and do stuff as per your requirement. This way you can differentiate between files that whether they are .MSG or PDF.

In a case if you have requirements different than my assumption, please let us know about some more details along with complete code snippet and sample input file(s), so that we can test the scenario in our environment and address it accordingly.


Best Regards,

Hello Asad Ali and thank you very much for your reply.


The way you proposed works sucessfully for file streams. I just forgot to mention in the previous message that as an input stream parameter I have a System.IO.MemoryStream stream type which I cannot use to determine what extention I have (at least I have not found any way).

I am attaching a zip archive containing sample msg file so you would be able to reproduce it in your environment. The following code snippet is enough to get the result I am writing about:

// This is just to emulate my initial conditions having memory stream in the beginning -->
// converting fileStream to a MemoryStream
System.String msgFilepath = “filepath\MSG_PDF_Attached_Sample.msg”;

System.IO.FileStream filestream = new System.IO.FileStream(msgFilepath, System.IO.FileMode::Open, System.IO.FileAccess::Read);

System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

filestream.CopyTo(memoryStream);
// This is just to emulate my initial conditions having memory stream in the beginning <–

// I have a memory stream as an input parameter

// Passing input msg memory stream containing pdf attachments
Aspose.Pdf.Facades.PdfFileInfo fileInfo = new Aspose.Pdf.Facades.PdfFileInfo(memoryStream);

// IsPdfFile returns true as msg memory stream contains pdf file as an attachment
// I would expect to have false in case this is initially an outlook message format file
boolean isPdf = fileInfo.IsPdfFile;

filestream.Close();
memoryStream.Close();

Hi,


Thanks for adding more details to the scenario and sharing input file. I have tested the scenario using your code snippet with Aspose.Pdf for .NET 17.4.0 and was unable to reproduce the issue which you have mentioned. The property IsPdfFile was returned as false, when I used same file which you have shared. It seems that there can be two possibilities.

1) The file which you have shared does not contain a PDF file as an attachment.

2) If it contains PDF file as an attachment, you might be using an old version of the API because while testing with the latest version of the API, I did not notice the issue. I have attached a screenshot as well, for your reference.

Please make sure that you are using latest version of the API and in a case if you still face any issue, please share information regarding your working environment i.e OS, Application Type, Target Framework etc, so that we can try to replicate the issue in our environment and address it accordingly.


Best Regards,
Hello and thank you for your respond.

It seems that the version in use in my environment does not support such operation. I have tested the same case using the latest Aspose.Pdf.dll following the link you provided and all worked correctly and as expected.

Thank you very much for your assistance. It was really helpful to understand what is going on.

Hi,


Thanks for your feedback. We are pleased that your issue has been resolved. Please keep using our API and in case of any further assistance, please feel free to contact us.


Best Regards,