Use JpegDevice for creating Pdf pages

We have been using PDFs with pages created by a JpegDevice, but after updating to the latest package we suddenly got exceptions using that code.
The exception is: Exception: System.NotImplementedException: Not supported image type

When testing we noticed that this error started appearing since version 24.4.0, versions before that date work without problem.

We can rewrite it to use a different device like PngDevice, but I cannot really find a reason why JpegDevice isn’t supported anymore for this usecase.

Is there a reason why JpegDevice isn’t supported anymore?
Can we expect the same results when using a different device for this?
Which devices are supported for this method? Is there a list somewhere?

Below I’ve added some small code snippits about how we use the device and create the pages.

private void DoSomething(Aspose.Pdf.Page page, Document newPdf, JpegDevice jpegDevice)
{
    using var stream = TempFileStream.Create();
    jpegDevice.Process(oldPage, stream);
    var newPage = newPdf.Pages.Add();

    // ..Do some magic

    var image = new Aspose.Pdf.Image();
    image.ImageStream = pngStream;
    newPdf.ProcessParagraphs();
}

On the created PDF we want to use the convert method like this

pdf.Convert(NewMethod);

// New method is defined like this
private string GetHocr(System.Drawing.Image img) { ... }

@Azure

It seems that you are encountering an issue with the JpegDevice after updating to version 24.4.0, which is resulting in a System.NotImplementedException indicating that the image type is not supported. This change in behavior could be due to updates in the library that may have altered how certain image types are handled.

Reasons for the Issue

  1. Library Updates: The latest version may have introduced changes that affect the support for certain image types or the way devices process images. It’s not uncommon for updates to modify existing functionality, which could lead to exceptions if the previous behavior is no longer supported.

  2. Supported Devices: While the JpegDevice is designed to convert PDF pages to JPEG images, there are other devices available for image conversion, such as PngDevice, BmpDevice, GifDevice, and EmfDevice. Each of these devices has its own characteristics and may be more suitable depending on your specific requirements.

Alternative Solutions

  • Switching to PngDevice: Since you mentioned that you can rewrite your code to use PngDevice, this is a viable option. The PngDevice should provide similar functionality for converting PDF pages to images, albeit in PNG format instead of JPEG. You can expect good results, but keep in mind that the output format will differ.

  • Code Example: Here’s a brief example of how you might modify your existing code to use PngDevice instead of JpegDevice:

private void DoSomething(Aspose.Pdf.Page page, Document newPdf, PngDevice pngDevice)
{
    using var stream = TempFileStream.Create();
    pngDevice.Process(oldPage, stream);
    var newPage = newPdf.Pages.Add();

    // ..Do some magic

    var image = new Aspose.Pdf.Image();
    image.ImageStream = stream; // Use the stream from PngDevice
    newPage.Paragraphs.Add(image);
    newPdf.ProcessParagraphs();
}

Supported Devices

For a comprehensive list of supported devices and their functionalities, you can refer to the official documentation. This will provide you with detailed information on how to use each device and their respective capabilities.

If you continue to experience issues or need further clarification, I recommend checking the release notes for version 24.4.0 or reaching out to the support team for more specific guidance regarding the changes made in that version.

Summary

  • The JpegDevice may have undergone changes in the latest version, leading to the exception you are seeing.
  • Switching to PngDevice is a good alternative, and you can expect similar results.
  • For a complete list of supported devices, please refer to the official documentation.

If you have any further questions or need additional assistance, feel free to ask!

Hi, my main question would be why isn’t JpegDevice supported for this flow anymore?

@Azure

This does not happen for all, but for some specific documents?
Please attach at least one of this document (better several).
JpegDevice is the used variant, it is not excluded from the library.

This happens for me on all pdf files. I’ve created a small console app as a repro.

// See https://aka.ms/new-console-template for more information

using Aspose.Pdf.Devices;

Console.WriteLine("Hello, World!");

var pdf = new Aspose.Pdf.Document("57113a0e-91c4-469f-b0ac-7f5e3644bed2.pdf");
var newPdf = new Aspose.Pdf.Document();

var jpegDevice = new JpegDevice(new Resolution(150), 90);

for (int i = 1; i <= pdf.Pages.Count; i++)
{
    using var stream = new FileStream(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
    jpegDevice.Process(pdf.Pages[i], stream);

    var newPage = newPdf.Pages.Add();
    var image = new Aspose.Pdf.Image();
    newPage.Paragraphs.Add(image);
    image.ImageStream = stream;
    newPdf.ProcessParagraphs();
}

newPdf.Convert(DoSomething);

string DoSomething(System.Drawing.Image img)
{
    return "";
}

If I run this with any PDF I get the following exception throw System.NotImplementedException: 'Not supported image type' on line newPdf.Convert(DoSomething);

If I replace new JpegDevice(new Resolution(150), 90); with new PngDevice(new Resolution(150)); it works fine.

I’ve added two PDFs that show the error for me, but like I said before it happens for all of them.
This code is run with <PackageReference Include="Aspose.PDF" Version="24.10.0" />, If I downgrade to 24.3.0 it runs fine with the JpegDevice.

76ad23be-c185-41ae-891c-92e66736a884.pdf (27.0 KB)

57113a0e-91c4-469f-b0ac-7f5e3644bed2.pdf (34.0 KB)

@Azure
In the code you provided, it is unclear how (with what parameter) you call DoSomething.
1.png (28.3 KB)

Please clarify.

The Convert method takes a CallBackGetHocr delegate Delegate Document.CallBackGetHocr | Aspose.PDF for .NET API Reference

I’m not calling it directly it gets called from inside the convert method. So I’m assuming it’s being called with the images we created inside the for loops, which are processed with the jpeg device.

@Azure
In my environment (Windows 10, .Net 6, Aspose.Pdf v24.10) this code works without exceptions

var pdf = new Aspose.Pdf.Document(dataDir + "57113a0e-91c4-469f-b0ac-7f5e3644bed2.pdf");
var newPdf = new Aspose.Pdf.Document();

var jpegDevice = new JpegDevice(new Resolution(150), 90);

for (int i = 1; i <= pdf.Pages.Count; i++)
{
    using var stream = new FileStream(Path.Combine(dataDir, Guid.NewGuid().ToString()), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
    jpegDevice.Process(pdf.Pages[i], stream);

    var newPage = newPdf.Pages.Add();
    var image = new Aspose.Pdf.Image();
    newPage.Paragraphs.Add(image);
    image.ImageStream = stream;
    newPdf.ProcessParagraphs();

    newPdf.Save(dataDir + "jpgConv_out.pdf");
}

Please check is it works in your environment.