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;
}