I am trying to convert all the below required files into pdf format and output will be stored in outputStream that I have destored at Finally stage when the request has been finished…Please find the below code :
if (inputStream == null)
{
throw new ArgumentNullException(nameof(inputStream));
}
fileExt = FormatFileExtension(fileExt);
outputStream = new MemoryStream();
if (pdfExtensions.Contains(fileExt, StringComparer.InvariantCultureIgnoreCase))
{
var pdfStream = new MemoryStream();
inputStream.CopyTo(pdfStream);
pdfStream.Position = 0;
return pdfStream;
}
switch (fileExt.ToUpper())
{
case "DOC":
case "DOCX":
case "TXT":
var wordDoc = new Aspose.Words.Document(inputStream);
wordDoc.Save(outputStream, Aspose.Words.SaveFormat.Pdf);
break;
case "PNG":
case "BMP":
case "JPG":
case "JPEG":
var pdf = new Aspose.Pdf.Document();
var image = new Image() { ImageStream = inputStream };
pdf.Pages.Add().Paragraphs.Add(image);
pdf.Save(outputStream);
break;
default:
throw new NotSupportedException("Unsupported file format");
}
outputStream.Position = 0;
return outputStream;
public void DestroyMemoryStream()
{
if (outputStream != null)
{
outputStream.Dispose();
outputStream = null; // Clear reference for garbage collection
}
}
But still it not release the memory after the coversion.
Currently Uses below package:
Aspose.PDF.Drawing: 24.8.0
Aspose.Words: 24.8.0