Telerik.Windows.Documents.Fixed.Exceptions.NotSupportedImageFormatException: 'Not supported image format.'

0 Answers 131 Views
General Discussions PdfProcessing SpreadProcessing SpreadStreamProcessing WordsProcessing ZipLibrary
Vaibhav
Top achievements
Rank 1
Iron
Iron
Vaibhav asked on 03 Jan 2025, 08:47 AM
i am getting the Telerik.Windows.Documents.Fixed.Exceptions.NotSupportedImageFormatException: 'Not supported image format.'  this exception in below code can anyone have a solution for that 


// Load the existing PDF from the byte array using PdfFormatProvider
using (MemoryStream inputStream = new MemoryStream(inDocByte))
{
    // Create PdfFormatProvider and load the existing document
    PdfFormatProvider provider = new PdfFormatProvider();
    RadFixedDocument document = provider.Import(inputStream);

    int pageNo = (signInfo.DigitalSignPage() == 0 ? 1 : document.Pages.Count);

    // Path to the image (ensure this is correct)
    System.Drawing.Image signatureImage = null;
    if (signInfo.DigitalSignMode() == 2 && HttpContext.Current.Session["imagesyncfusion"] != null)
    {
        signatureImage = (System.Drawing.Image)HttpContext.Current.Session["imagesyncfusion"];
    }
    // Check if digital signature is needed
    if (signInfo.UseDigitalSign())
    {
        if ((signInfo.DigitalSignMode() == 1) || (signInfo.DigitalSignMode() == 2))
        {
            double signaturePositionLeft = (double)signInfo.DigitalSignLeftInPoints();
            double signaturePositionTop = (double)signInfo.DigitalSignTopInPoints();
            double signatureFieldWidth = (double)signInfo.DigitalSignWidthInPoints();
            double signatureFieldHeight = (double)signInfo.DigitalSignHeightInPoints();
            RadFixedPage radFixedPage = document.Pages[pageNo - 1];

            if (!string.IsNullOrEmpty(customSignatureDetails))
            {
                // Movable signature case: Adjust the signature field based on custom details
                signatureFieldWidth = signaturePositionLeft + (double)signInfo.DigitalSignWidthInPoints();
                signatureFieldHeight = signaturePositionTop + (double)signInfo.DigitalSignHeightInPoints();

                string[] separator = new string[] { "!_!" };
                string[] offsetArray = customSignatureDetails.Split(separator, StringSplitOptions.RemoveEmptyEntries);

                if (offsetArray.Length >= 9)
                {
                    float offsetLeft = returnFloatWithCultureValue(offsetArray[0]);
                    float offsetTop = returnFloatWithCultureValue(offsetArray[1]);
                    float docHeight = returnFloatWithCultureValue(offsetArray[2]);
                    float greenDivHeight = returnFloatWithCultureValue(offsetArray[3]);
                    float greenDivWidth = returnFloatWithCultureValue(offsetArray[4]);
                    float dpi_x = returnFloatWithCultureValue(offsetArray[6]);
                    float dpi_y = returnFloatWithCultureValue(offsetArray[7]);
                    pageNo = int.Parse(offsetArray[8]);

                    // Calculate positions for drawing the text div;
                    signaturePositionLeft = (offsetLeft / dpi_x) * 72;
                    signaturePositionTop = (offsetTop / dpi_y) * 72;
                    signatureFieldWidth = (greenDivWidth / dpi_y) * 72;
                    signatureFieldHeight = (greenDivHeight / dpi_x) * 72;
                }
            }
            else
            {
                // Non-movable case: Place the signature field in a fixed location
                radFixedPage = document.Pages[pageNo - 1];
                Tuple<double, double> k = null;

                if (signatureImage != null)
                {
                    k = calculateAspectRatioFit(signatureImage.Width, signatureImage.Height, signatureFieldWidth, signatureFieldHeight);
                    signatureFieldWidth = k.Item1;
                    signatureFieldHeight = k.Item2;
                }

                signaturePositionLeft = radFixedPage.Size.Height - (double)(signatureFieldWidth + signatureFieldHeight + 1);
                signaturePositionTop = signaturePositionTop + 1; // +1 For border size
                                                                 //pdfForm.FormSource.Size = new Telerik.Documents.Primitives.Size(signatureFieldWidth, signatureFieldHeight);
            }

            // Create a new form to place the signature field
            Form pdfForm = new Form();
            pdfForm.FormSource = new FormSource();
            FixedContentEditor editor = new FixedContentEditor(pdfForm.FormSource);
            pdfForm.FormSource.Size = new Telerik.Documents.Primitives.Size(signatureFieldWidth, signatureFieldHeight);

            // Drawing the image at the starting position (left side of the signature field)
            imageWidth = signatureFieldWidth / 2;  // Half the width for the image
            double imageHeight = signatureFieldHeight;  // Use full height of the signature field

            if (signatureImage != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    signatureImage.Save(ms, signatureImage.RawFormat);
                    ms.Position = 0;
                    editor.DrawImage(ms, new Telerik.Documents.Primitives.Size(imageWidth, imageHeight));
                }
            }

            // Prepare text to draw beside the signature image
            string textToDraw = $"Digitally signed by {certificate.FriendlyName} \nDate: {DateTime.Now:yyyy.MM.dd HH:mm:ss zzz} \nReason: {pdfSignature.Signature.Properties.Reason} \nLocation: {pdfSignature.Signature.Properties.Location}";

            // Set text width and calculate font size
            double textWidth = signatureFieldWidth - imageWidth;
            double fontSize = CalculateFontSize(signatureFieldWidth - 23, signatureFieldHeight, textToDraw);

            // Define the position for the text and draw it
            Telerik.Windows.Documents.Fixed.Model.Editing.Block textBlock = new Telerik.Windows.Documents.Fixed.Model.Editing.Block();
            double textPositionLeft = signaturePositionLeft;  // Adding a 5px gap after the image
            textBlock.TextProperties.FontSize = fontSize;
            textBlock.InsertText($"Digitally signed by {certificate.FriendlyName}");
            textBlock.InsertText($" Date: {DateTime.Now:yyyy.MM.dd HH:mm:ss zzz}");
            textBlock.InsertText($" Reason: {pdfSignature.Signature.Properties.Reason}");
            textBlock.InsertText($" Location: {pdfSignature.Signature.Properties.Location}");
            
            Rect boundingRect;
            if (signatureImage != null)
            {
                boundingRect = new Rect(imageWidth, 5, textWidth, signatureFieldHeight);
            }
            else
            {
                boundingRect = new Rect(textPositionLeft, 5, textWidth, signatureFieldHeight);
            }
            textBlock.Draw(editor, boundingRect);
            //editor.DrawBlock(textBlock);
            // Create the SignatureWidget and position it on the PDF page
            SignatureWidget signatureWidget = pdfSignature.Widgets.AddWidget();
            signatureWidget.Content.NormalContentSource = pdfForm.FormSource;
            signatureWidget.Rect = new Rect(signaturePositionLeft, 0, signatureFieldWidth, signatureFieldHeight);
            signatureWidget.RecalculateContent();

            // Add signature widget to the page and draw the content
            RadFixedPage pdfPage = document.Pages[pageNo - 1];
            FixedContentEditor pageEditor = new FixedContentEditor(pdfPage);
            pageEditor.Position.Translate(signaturePositionLeft , signaturePositionTop + 258.3824795800782);
            pageEditor.DrawForm(pdfForm.FormSource);

            // Add the signature field to the document's AcroForm
            document.AcroForm.FormFields.Add(pdfSignature);

        }

    }
    // Export the document to a byte array and return
    using (MemoryStream memoryStream = new MemoryStream())
    {

        Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
        Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;
        PdfExportSettings settings = new PdfExportSettings() { ImageQuality = ImageQuality.High };
        //PdfFormatProvider provider = new PdfFormatProvider();
        provider.ExportSettings = settings;
        provider.Export(document, memoryStream);
        outDocPdf = memoryStream.ToArray();
    }
Yoan
Telerik team
commented on 03 Jan 2025, 01:36 PM

Hello Vaibhav,

Thank you for reaching out. I am sorry to hear you are experiencing unexpected results. 

The exception you are encountering is usually thrown when exporting a PDF document that contains an image with an invalid or not yet supported format by the library. I tried to verify if that is the case by simplifying the shared code snippet into a project on my end and creating an Image instance ("signatureImage") from a file with a "PDF" extension. The result is as expected - the same "NotSupportedImageFormatException" is thrown due to the image being in a PDF format.

This leads me to believe that the same happens on your end where the "signatureImage" is created (possibly with an invalid or unsupported format) with the data that comes from the HttpContext:

signatureImage = (System.Drawing.Image)HttpContext.Current.Session["imagesyncfusion"];

However, I am unable to confirm this assumption without having access to the actual content being obtained from the HttpContext.Current.Session["imagesyncfusion"] instance. With that in mind would you be able to share with us the exact HttpContext data used to create the "signatureImage" on your end? Having it at my disposal will help me observe and investigate the same results on my end and will really speed up the investigation process. Any additional resource like a simplified runnable project is always welcome.

That said, please remember that this is a public Q&A forum, and any resources shared here will be publicly accessible. Ensure that no sensitive or confidential information is included if you wouldn't want it to be made public.

I am also attaching my project for your disposal just in case you would like to test it and examine it for yourself. Do not hesitate to let me know if you have any questions or if I am misunderstanding the scenario and doing something different.

NOTE: The Image class of the PdfProcessing library comes from the "Telerik.Windows.Documents.Fixed.Model.Objects" namespace as opposed to the "System.Drawing" namespace used in the code snippet which could also be the reason for conflicts and unexpected exceptions. You can see this reflected in the changes I have made in the attached project.

I remain at your disposal in the meantime. Thank you in advance for your cooperation.

Regards,

Yoan


Vaibhav
Top achievements
Rank 1
Iron
Iron
commented on 06 Jan 2025, 05:31 AM

Hello Yoan,

in the HttpContext data it was just the

System.Drawing.Image objImage1 = System.Drawing.Image.FromStream(ms);

Session["imagesyncfusion"] = objImage1;

simple image that is type System.Drawing.Image

please let me know about the issue as soon as possible

Thank you
Yoan
Telerik team
commented on 06 Jan 2025, 02:26 PM

Hello Vaibhav,

Thank you for the clarification. With all the testing I have done and the information I have at my disposal I am sharing with you my current observations on the matter.

The signatureImage instance being from the System.Drawing namespace should not be an issue because the signatureImage is later converted to MemoryStream before passing it to the FixedContentEditor:

using (MemoryStream ms = new MemoryStream())
{
    signatureImage.Save(ms, signatureImage.RawFormat);
    ms.Position = 0;
    editor.DrawImage(ms, new Telerik.Documents.Primitives.Size(100, 100));
}

I am attaching for your disposal a second project that implements the System.Drawing.Image to MemoryStream conversion that showcases how, if in the correct format (in this case "jpeg"), any image can be successfully added to the document.

With that in mind, I believe that the most likely reason for the exception is the format of the specific signatureImage data obtained from the HttpContext in that instance:

System.Drawing.Image objImage1 = System.Drawing.Image.FromStream(ms);

It is possible that the image is in some invalid or not yet supported format thus causing the exception when trying to draw it with the editor. That said, cannot verify my assumptions as I am unable to access and examine that same data on my end. 

Would it be possible for you to share that specific data used to create either the objImage1 or signatureImage instances so I can use it to reproduce the same exception on my end? Knowing the desired format/extension of the image would help me narrow down the reason for the scenario you are experiencing.

Thank you in advance. I am looking forward to your reply.

Regards,

Yoan

Vaibhav
Top achievements
Rank 1
Iron
Iron
commented on 07 Jan 2025, 06:23 AM

Hi Yoan ,

thank you for your research the format of the image is png 

Yoan
Telerik team
commented on 08 Jan 2025, 11:52 AM

Hi Vaibhav

Thank you for the additional feedback. I have updated my project accordingly to insert a sample PNG image, however, the image is successfully added to the document and I am unable to observe any unusual results. Please feel free to examine the project for yourself and let me know if I am missing something.

This leads me to believe that the issue might lie in factors other than the image format however I am not in a position to confirm this without having examined the exact resources first.

To help me achieve that would you be able to isolate the behavior you are experiencing and provide a project I can use to achieve the same exception on my end? This would also mean sharing the data used for the image creation, ideally exported to an image PNG file. This will significantly improve my assistance and allow me to get back to you with specific feedback on the issue.

Your cooperation would be much appreciated.

Best,

Yoan

n/a
Top achievements
Rank 1
Iron
Iron
Iron
commented on 03 Mar 2025, 12:04 PM

Hi Yoan,

I'm also having an issue related with "NotSupportedImageFormat". Could you please take a look? "Not supported image format." on AWS environment after upgrading Telerik UI for Blazor to 8.0 version in UI for Blazor | Telerik Forums

I
'd be appreciated. Thanks

Dess | Tech Support Engineer, Principal
Telerik team
commented on 03 Mar 2025, 01:10 PM

Hi, Astig,

Please have in mind that when using RadPdfProcessing library in a Blazor app, this means that you are using the .NET Standard version of of the library. If neither ImagePropertiesResolver and JpegImageConverter are set, an InvalidOperationException is thrown during export of document. 

In order to successfully export images different than Jpeg and Jpeg2000 and ImageQuality different than High you will need to reference the Telerik.Documents.ImageUtils assembly/NuGet package in your project.

It is important to note that For Linux use SkiaSharp.NativeAssets.* (version 2.88.8) - SkiaSharp.NativeAssets.Linux.NoDependencies. For Blazor Web Assembly use SkiaSharp.Views.Blazor and wasm-tools.

If case you are still facing any further difficulties, it would be greatly appreciated if you can share a sample document se we can make an adequate analysis of the precise case and provide further assistance. Thank you in advance.

No answers yet. Maybe you can help?

Tags
General Discussions PdfProcessing SpreadProcessing SpreadStreamProcessing WordsProcessing ZipLibrary
Asked by
Vaibhav
Top achievements
Rank 1
Iron
Iron
Share this question
or