.NET8 PdfRenderer Load method from stream results in exception 'Specified method is not supported.'

Please Visual Studio project file below and a code example.

Any help would be appreciated to explain why the exception is being raised and any remedial action.


<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>

    <TargetFramework>net8.0</TargetFramework>

    <Nullable>enable</Nullable>

    <ImplicitUsings>enable</ImplicitUsings>

  </PropertyGroup>

  <ItemGroup>

    <PackageReference Include="SkiaSharp" Version="2.88.5" />

    <PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="22.2.12" />

    <PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows" Version="22.2.12" />

  </ItemGroup>

</Project>


Code

 var fileStream = await httpClient.GetStreamAsync("data/Input.pdf");

 PdfRenderer pdfExportImage = new PdfRenderer();

 //Loads the PDF document

pdfExportImage.Load(fileStream); //  results in exception Specified method is not supported.



3 Replies 1 reply marked as answer

GV Gokulprasath Venkatachalam Syncfusion Team September 11, 2023 01:17 PM UTC

Hi Russell,


We see that you are using a Load function in the ExportAsImage operation, which is invalid. This is an usage issue.


We also believe that you were utilizing our previous PDF Viewer component. To develop a web service and a PDF viewer component, we've prepared documentation. Please locate the documentation for our PDF Viewer component in Blazor Server and Blazor WebAssembly.


Blazor Server - https://blazor.syncfusion.com/documentation/pdfviewer/getting-started/server-side-application

Blazor WASM - https://blazor.syncfusion.com/documentation/pdfviewer/getting-started/web-assembly-application


To eliminate the need for a web service we've introduced a new PDF Viewer (NextGen) component that no longer relies on this service dependency. For additional information, please consult the documentation:


Blazor Server - https://blazor.syncfusion.com/documentation/pdfviewer-2/getting-started/server-side-application

Blazor WASM - https://blazor.syncfusion.com/documentation/pdfviewer-2/getting-started/web-assembly-application


If we have misunderstood your requirements inadvertently, please provide further details for clarification.


Regards,

Gokulprasath



RA Russell Antony Sherlock September 13, 2023 09:04 AM UTC

Hi Gokulprasath


Thank you for your reply, I am not trying to view a PDF file.


I am attempting to convert a PDF document into an image document (e.g. a BitMap object such as SkBitMap). A Stack Overflow article ( https://ej2.syncfusion.com/aspnetcore/documentation/pdfviewer/how-to/export-as-image#exportasimageint-pageindex ) suggested using your PdfRenderer.

I then intend to process the resulting image (BitMap) in various ways to extract text from various pre-defined sections via OCR and also create a low res thumbprint of the image.

I am using the current DotNet 8 Preview. Can you tell me which Nuget packages I should be using if the ones that I am using are wrong?

Do you have any further links that would help?


Regards

Russ



VV Visvesvar Venkatesan Syncfusion Team September 13, 2023 12:12 PM UTC

Hi Russell,


We kindly ask that you consider employing an alternative approach, as the current method is encountering difficulties. You have the option to either load data as a byte array or directly via file upload.


Code snippet : 

 byte[] bytes = System.IO.File.ReadAllBytes("data/Input.pdf");

        var fileStream = new MemoryStream(bytes);

        PdfRenderer pdfExportImage = new PdfRenderer();


        //Loads the PDF document


        pdfExportImage.Load(fileStream);

        SKBitmap skBitmap = pdfExportImage.ExportAsImage(0);

        byte[] imageBytes;

        using (SKImage img = SKImage.FromBitmap(skBitmap))

        using (SKData encodedImg = img.Encode())

        using (Stream stream = encodedImg.AsStream())

        {

            using (var memoryStream = new MemoryStream())

            {

                stream.CopyTo(memoryStream);

                imageBytes = memoryStream.ToArray();

            }

        }

        var imageBase64 = Convert.ToBase64String(imageBytes);

        var imageUrl = $"data:image/png;base64,{imageBase64}";

        <img src="@(imageUrl)" alt="Image" />



You can also load the file directly using the below line.

pdfExportImage.Load("data/Input.pdf");



Sample:  https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1-12485050.zip


Kindly try this and let us know if you have any concerns.


Marked as answer
Loader.
Up arrow icon