Use Aspose APIs in multithreading environment to process different file formats

Hi Aspose,
I have created a utility to convert documents from .doc, .docx, .xls, .xlsx, .ppt, .pptx, .jpg and .png formats to PDF format. i have used Aspose dlls version as follows for the PDF conversion. The conversion is working fine for single file scenarios, but it fails intermittently when concurrent instance were created to convert various file types. The same code works fine for same set of file for few hits and fails for other time. Exception received are “Unable to read beyond end of Stream”, “Unknown File Format”, “Parameters are not Valid”. Please help us to resolve this issue.

Aspose.cell.dll (17.12.0.0)
Aspose.Imaging.dll (17.12.0.0)
Aspose.Pdf.dll (18.1.0.0)
Aspose.Slide.dll (17.12.1.0)
Aspose.Words.dll (18.1.0.0)

Code used for file conversion :
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, relativeUrl);
if (fInfo != null)
{
using (var ms = new MemoryStream())
{

                            fInfo.Stream.CopyTo(ms);
                            fileExtension = Path.GetExtension(file.ServerRelativeUrl); //added by AshishK as part of SDC5816 to fetch the extension of the loaded file

                            MemoryStream pdfMemoryStream = new MemoryStream(); //create an output memory stream to hold the converted data as part of SDC5816

                            //start of file conversion code added by AshishK as part of SDC5816

                            if (fileExtension.Equals(".doc", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".docx", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".docm", StringComparison.InvariantCultureIgnoreCase)) //check if the file extension is of doc/docx/docm file
                            {
                                Aspose.Words.License license = new Aspose.Words.License();
                                license.SetLicense("ASPOSE - XX-XXX-XXXXX -XX-XXX-XXXXXX.lic");

                               

                                Aspose.Words.Document doc = new Aspose.Words.Document(ms); //get the loaded file from memory stream
                                doc.Save(ms, Aspose.Words.SaveFormat.Pdf); //convert the file format to pdf

                                

                                ms.Seek(0, SeekOrigin.Begin); //set the position of memory stream object to start reading the data

                                Aspose.Words.Saving.PdfSaveOptions pdfOptions = new Aspose.Words.Saving.PdfSaveOptions();
                                pdfOptions.PageIndex = 0;
                                pdfOptions.PageCount = Int32.MaxValue; //save document with all pages
                                pdfOptions.PreserveFormFields = true;
                                doc.Save(pdfMemoryStream, pdfOptions); //save converted file to output memory stream

                                
                            }
                            else if (fileExtension.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".bmp", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".gif", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".tif", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".tiff", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".png", StringComparison.InvariantCultureIgnoreCase)) //check if the file extension is of jpg/jpeg/bmp/gif/tiff/png file
                            {
                               
                                Aspose.Imaging.License pdfLicense = new Aspose.Imaging.License();
                                pdfLicense.SetLicense("ASPOSE - XX-XXX-XXXXX -XX-XXX-XXXXXX.lic");

                                

                                Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); //Instantiate Document Object

                                Page page = doc.Pages.Add(); //Add a page to pages collection of document 

                                Aspose.Pdf.Image image = new Aspose.Pdf.Image(); //create an image object

                                Bitmap b = new Bitmap(ms); //pass input memory stream to bitmap object

                                if (b.Width > page.PageInfo.Width) //check if the bitmap object is wider than the page width
                                {
                                    page.PageInfo.IsLandscape = true;
                                    page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
                                }
                                else
                                {
                                    page.PageInfo.IsLandscape = false;
                                }

                                //set margins of the page

                                page.PageInfo.Margin.Bottom = 0;
                                page.PageInfo.Margin.Top = 0;
                                page.PageInfo.Margin.Left = 0;
                                page.PageInfo.Margin.Right = 0;

                                page.Paragraphs.Add(image); //Add the image to the paragraph section of the page

                                image.ImageStream = ms; //set the image file stream

                                Logger.WriteComment("Loaded the " + fileExtension + " file successfully in the aspose object", DOTNET, SiteRegion);

                                doc.Save(pdfMemoryStream, Aspose.Pdf.SaveFormat.Pdf); //save converted file to output memory stream

                                }
                            else if (fileExtension.Equals(".txt", StringComparison.InvariantCultureIgnoreCase)) //check if the file extension is of text file
                            {
                                Aspose.Words.License license = new Aspose.Words.License();
                                license.SetLicense("ASPOSE - XX-XXX-XXXXX -XX-XXX-XXXXXX.lic");

                                

                                Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();

                                loadOptions.LoadFormat = Aspose.Words.LoadFormat.Text;

                                Aspose.Words.Document doc = new Aspose.Words.Document(ms, loadOptions);

                                doc.Save(ms, Aspose.Words.SaveFormat.Pdf); //save converted file to output memory stream

                                

                                ms.Seek(0, SeekOrigin.Begin); //set the position of memory stream object to start reading the data

                                Aspose.Words.Saving.PdfSaveOptions pdfOptions = new Aspose.Words.Saving.PdfSaveOptions();

                                pdfOptions.PageIndex = 0;
                                pdfOptions.PageCount = Int32.MaxValue; //save document with all pages
                                pdfOptions.PreserveFormFields = true;

                                doc.Save(pdfMemoryStream, pdfOptions); //save converted file to output memory stream

                                Logger.WriteComment(fileExtension + " File Converted to PDF succesfully and passed to the memory stream", DOTNET, SiteRegion);
                            }
                            else if (fileExtension.Equals(".xls", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".xlsx", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".xlsm", StringComparison.InvariantCultureIgnoreCase)) //check if the file extension is of xls/xlsx/xlsm files
                            {
                                Aspose.Cells.License excelLicense = new Aspose.Cells.License();
                                excelLicense.SetLicense("ASPOSE - XX-XXX-XXXXX -XX-XXX-XXXXXX.lic");

                               

                                Workbook workbook = new Workbook(ms); //Instantiate Workbook object
                                workbook.Save(ms, Aspose.Cells.SaveFormat.Pdf); //convert the file format to pdf

                                Logger.WriteComment("Loaded the " + fileExtension + " file successfully in the aspose object", DOTNET, SiteRegion);

                                ms.Seek(0, SeekOrigin.Begin); //set the position of memory stream object to start for reading the data

                                Aspose.Cells.PdfSaveOptions pdfOptions = new Aspose.Cells.PdfSaveOptions();

                                pdfOptions.CalculateFormula = true; //execute workbook formulas before pdf generation
                                pdfOptions.OnePagePerSheet = true; //save converted data to one page of pdf per excel sheet

                                workbook.Save(pdfMemoryStream, pdfOptions); //save converted file to output memory stream

                                Logger.WriteComment(fileExtension + " File Converted to PDF succesfully and passed to the memory stream", DOTNET, SiteRegion);
                            }
                            else if (fileExtension.Equals(".ppt", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".pptx", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".pps", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".ppsx", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".ppsm", StringComparison.InvariantCultureIgnoreCase) || fileExtension.Equals(".pptm", StringComparison.InvariantCultureIgnoreCase)) //check if the file extension is of ppt/pptx/pps/ppsx/ppsm/pptm files
                            {
                                Aspose.Slides.License pptLicense = new Aspose.Slides.License();
                                pptLicense.SetLicense("ASPOSE - XX-XXX-XXXXX -XX-XXX-XXXXXX.lic");

                              

                                Presentation presentation = new Presentation(ms); //Instantiate Presentation object
                                presentation.Save(pdfMemoryStream, Aspose.Slides.Export.SaveFormat.Pdf); //convert the file format to pdf

                               

                                ms.Seek(0, SeekOrigin.Begin); //set the position of memory stream object to start for reading the data

                                
                            }
                            else if (fileExtension.Equals(".pdf")) //check if the file extension is of pdf file
                            {
                                pdfMemoryStream = ms; //pass the input stream to the output stream without conversion

                             
                            }
                            



                            responseData = pdfMemoryStream.ToArray(); 

                            pdfMemoryStream.Close(); //close the  output memory stream once the data has been passed to the byte object

                            
                        }
                        string encoded = Convert.ToBase64String(responseData);
                        return encoded;
                    }

@prakashn08,

Thank you for details. Please share some sample input files with us as well. This will help us in reproducing actual issue.

Test Jpg.png (20.8 KB)
Sample Docs.zip (105.7 KB)
Hi iKram,

I have attached sample input files with this thread. This issue occurs intermittently. Please let me know if any other information is required.

@prakashn08,

We are looking into the issue. We will update you soon about our findings.

@prakashn08,

We have further looked into the issue. We have performed several tests on the sample input files shared by you. We are unable to reproduce the issue. Please create a sample project demonstrating the issue and share it with us along with input file & other details. We will look into it and update you about our findings.

Furthermore while investigation we have noticed that you have used pdfOptions.OnePagePerSheet = true. This may cause some issue. For example there are 100 or 1000 of pages in a worksheet which means all of them on single page in PDF file. It will consume a large amount of memory and CPU. You may try to avoid such settings.

Hi iKram,

I have tried removing pdfOptions.OnePagePerSheet = true from excel conversion, but still we are facing same issue.PFA for the console utility used for testing this scenario.
Please check and let us know.

Note : License file has been removed from the utility.

Regards,
Prakash

@prakashn08,

Thank you for details. Found no attachment with your reply. Please forward the sample project. You may share it through DropBox or Google Drive link with us for downloading the attachment.

FileConversionUsingAspose.zip (106.5 KB)
Hi iKram,

Attachments was missed because of size issue, i have removed ddls from the utility please re-add all above mentioned dlls and test it.

@prakashn08,

Thank you for sharing the demo project. We will investigate the issue and update you about our findings.

@prakashn08,

We have investigated the issue. We are unable to reproduce the issue at our end except while using Aspose.PDF. We are able to reproduce “Parameters are not Valid” exception using the sample project and input files shared by you. We have updated your code to fix the issue. We have devised a sample application w.r.t Aspose.PDF. The sample application is attached for your kind reference.

Furthermore we have noticed that you have tried to set the license multiple times in your code like for each thread. You must not do this. License should be set once and in the beginning. You also need to make sure that you use separate Document instances per thread.

Multi_thread_sample_application.zip (361.3 KB)

Dear Ikram

We having problems with aspose.pdf and multi threading. Could you send us the example “Multi_thread_sample_application.zip” or a similar example.

Thanks a lot.

@marchuber

Thank you for contacting support.

You may download the sample project devised by us, as a ZIP archive from this Google Drive link for your kind reference. Feel free to get back to us if you need any further assistance.