For Jpeg and PNG images it takes anywhere from 10-60 seconds to save an image as a pdf. This is unacceptable for our application. It has slowed down our processing tremendously. The code in the else statement was fast but caused a cannot access closed stream error.
Below is the code we are using:
public async Task<MemoryStream> ConvertImageToPDF(Stream streamToConvert, int intDocNo)
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
Aspose.Pdf.Image pdfImage = new Aspose.Pdf.Image();
pdfImage.FixWidth = page.PageInfo.Width;
pdfImage.FixHeight = page.PageInfo.Height;
int imagebuffersize = streamToConvert.Length >= 1048576 ? (int)((float)(streamToConvert.Length / 1048576) * 2) : 10;
streamToConvert.Position = 0;
Aspose.Imaging.Image Asposeimage = Aspose.Imaging.Image.Load(streamToConvert, new Aspose.Imaging.LoadOptions { BufferSizeHint = imagebuffersize });
if (image.FileFormat == FileFormat.Tiff)
{
//Working
stopwatch.Start();
TiffImage tiffImage = (TiffImage)image;
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.ResolutionSettings = new ResolutionSetting(tiffImage.HorizontalResolution, tiffImage.VerticalResolution);
image.Save(outStream, pdfOptions);
stopwatch.Stop();
Log.Information("save tiff image {ElapsedMilliseconds} ms", stopwatch.ElapsedMilliseconds);
}
else if (image.FileFormat == FileFormat.Jpeg)
{
stopwatch.Start();
var saveOptions = new Aspose.Imaging.ImageOptions.JpegOptions
{
CompressionType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.Progressive,
ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.YCbCr,
Quality = 50
};
image.Save(imageStream, saveOptions);
imageStream.Position = 0;
if (imageStream.Length > 0)
{
pdfImage.ImageStream = imageStream;
pdfImage.ImageStream.Position = 0;
page.Paragraphs.Add(pdfImage);
doc.Save(outStream);
}
else
{
Log.Information("jpeg image length was 0 after saving options");
}
stopwatch.Stop();
Log.Information("save jpeg image {ElapsedMilliseconds} ms", stopwatch.ElapsedMilliseconds);
}
else if (image.FileFormat == FileFormat.Png)
{
stopwatch.Start();
var saveOptions = new Aspose.Imaging.ImageOptions.PngOptions
{
ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha,
BitDepth = 8,
CompressionLevel = 9,
FilterType = Aspose.Imaging.FileFormats.Png.PngFilterType.Adaptive
};
image.Save(imageStream, saveOptions);
imageStream.Position = 0;
if (imageStream.Length > 0)
{
pdfImage.ImageStream = imageStream;
pdfImage.ImageStream.Position = 0;
page.Paragraphs.Add(pdfImage);
doc.Save(outStream);
}
else
{
Log.Information("png image length was 0 after saving options");
}
stopwatch.Stop();
Log.Information("save png image {ElapsedMilliseconds} ms", stopwatch.ElapsedMilliseconds);
}
else
{
//cannot access closed stream error
stopwatch.Start();
pdfImage.ImageStream = streamToConvert;
pdfImage.ImageStream.Position = 0;
page.Paragraphs.Add(pdfImage);
doc.Save(outStream);
stopwatch.Stop();
Log.Information(image.FileFormat.ToString() + " image {ElapsedMilliseconds} ms", stopwatch.ElapsedMilliseconds);
}
}