Free Support Forum - aspose.com

Error while calling Aspose PDF service

We are using Aspose.PDF 18.9.1 in our application. We have document upload functionality for which we use Aspose.Pdf.Devices for generating pdf thumbnail. We have few documents which are failing inconsistently while processing. It fails only sometime and same document processes successfully the other times.
Please can you check logs from your end and let us know the reason for the failure.
We are seeing below error in our logs

2019-11-22 10:57:39.0433||ERROR|FA.TEN.WebAPI.Controllers.DocumentsController|Failed to upload 55369888_1_pkg_092019062707 not corrupt.pdf for order 55284276, work queue doc id 113600393 System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap…ctor(Int32 width, Int32 height, PixelFormat format)
at . ()
at Aspose.Pdf.Devices.PngDevice.Process(Page page, Stream output)
at FA.TEN.ApplicationCore.Helpers.Documents.PdfDocumentHelper.GetPdfPageImage(Stream sourcePdfStream, Int32 page, PdfPageImageFormat format, Int32 resolutionScale) in D:\AgentPool\Agent1_work\928\s\FA.TEN\FA.TEN.ApplicationCore\Helpers\Documents\PdfDocumentHelper.cs:line 88

Code snippet at our end:
public static byte[] GetPdfPageImage(
Stream sourcePdfStream,
int page = 1,
PdfPageImageFormat format = PdfPageImageFormat.Png,
int resolutionScale = 100)
{
using (var document = new Document(sourcePdfStream))
{
if (document.IsEncrypted)
{
throw new InvalidOperationException(
“Cannot generate page image for an encrypted PDF”);
}

            if (document.Pages.Count < page || page < 1)
            {
                throw new ArgumentOutOfRangeException(
                    nameof(page),
                    $"Page must be between 1 and page count ({document.Pages.Count})");
            }

            document.RemoveMetadata();

            var targetResolution = new Resolution(resolutionScale);
            var targetDevice = GetDevice(format, targetResolution);

            var pdfPage = document.Pages[page];


}

private static PageDevice GetDevice(PdfPageImageFormat format, Resolution resolution)
{
return format == PdfPageImageFormat.Png
? new PngDevice(resolution)
: (PageDevice)new JpegDevice(resolution);
}

@balagur

Would you kindly try using Aspose.PDF for .NET 19.11 in your environment and in case issue still persists, please share your sample PDF document with us so that we can test the scenario in our environment and address it accordingly.