Signing Pdf - Image Distorted

Hi,

I am using Syncfusion Pdf to add digital signature to a pdf. The image for sig is 349x223. When added to the Pdf it is distorted.

The following is the code I am using:


using Azure.Identity;

using Azure.Security.KeyVault.Certificates;

using Org.BouncyCastle.X509;

using Syncfusion.Pdf;

using Syncfusion.Pdf.Graphics;

using Syncfusion.Pdf.Parsing;

using Syncfusion.Pdf.Security;

using System.Buffers;

using System.Security.Cryptography;

using System.Security.Cryptography.Pkcs;

using System.Security.Cryptography.X509Certificates;


namespace SignPdfDemo

{

    internal class Program

    {

        static void Main(string[] args)

        {

            string fileDir = $@"C:\Users\SimonHewitt\Documents\Work Files\_SPIKES Etc\SIGN PDF IMAGE SIZES\";

            string fileName = "Original PDF.pdf";

            string pdfPath = $"{fileDir}\\{fileName}";

            string signatureFile = "testsignature.png";

            string sigFilePath = $"{fileDir}\\{signatureFile}";


            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(File.OpenRead(pdfPath));

            PdfLoadedPage? page = loadedDocument.Pages[4] as PdfLoadedPage;


            #region Configure Signature

            PdfSignature signature = new PdfSignature(loadedDocument, page, null, "Encodian");


            signature.Settings.CryptographicStandard = CryptographicStandard.CADES;

            signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256;

            signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamping.ensuredca.com"));

            signature.EnableLtv = true;


            PdfBitmap signatureImage = null;


            signatureImage = new PdfBitmap(File.OpenRead(sigFilePath));

            signature.Bounds = new Syncfusion.Drawing.RectangleF(new Syncfusion.Drawing.PointF(0, 0), signatureImage.PhysicalDimension);

            signature.Appearance.Normal.Graphics.DrawImage(signatureImage, signature.Bounds);

            signature.Location = new Syncfusion.Drawing.PointF(306, 429);

            #endregion



            // Add additional signature configuration

            string outputFile = $"{Guid.NewGuid()}.pdf";

            string outputFilePath = $@"{fileDir}{outputFile}";


            using (FileStream filestream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))

            {

                loadedDocument.Save(filestream);

                loadedDocument.Close(true);

            }

        }

    }

}


Can you help at all?

Thanks

Simon


Attachment: Inputs_5e3554b7.zip

11 Replies

IJ Irfana Jaffer Sadhik Syncfusion Team June 22, 2023 10:51 AM UTC

We have checked the reported issue with the provided details on our end (Please find the output attached). The image has been drawn with its mentioned physical dimension on our end.  Can you please confirm whether you have reported the below image is blurred/not clearly placed on your end? 


Please find the runnable sample below:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/NetCoreSample2083740542

We request you to share additional details about the issue like issue screenshots/steps to replicate the issue on our end. so that we can assist you further in this. 




UN Unknown June 22, 2023 10:56 AM UTC

The original image looks like this when viewed in WIndows Photos:


When placed in the pdf it looks like this:


As you can see it appears larger in the PDF than in Photos, and the text is pixellated.





IJ Irfana Jaffer Sadhik Syncfusion Team June 22, 2023 12:15 PM UTC

The reason for this distortion in the image is that it is drawn in its original size, which happens to be larger than the page width. We suggest reducing the size of the image to address this issue. For your convenience, we have included the modified sample and its corresponding output.

modified code:

 FileStream input = new FileStream("../../../Data/Original PDF.pdf", FileMode.Open, FileAccess.ReadWrite);


            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(input);


            var page = loadedDocument.Pages[4] as PdfLoadedPage;




            #region Configure Signature


            PdfSignature signature = new PdfSignature(loadedDocument, page, null, "Encodian");




            signature.Settings.CryptographicStandard = CryptographicStandard.CADES;


            signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256;


            signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamping.ensuredca.com"));


            signature.EnableLtv = true;




            PdfBitmap signatureImage = null;


            FileStream imageStream = new FileStream("../../../Data/testsignature.png", FileMode.Open, FileAccess.ReadWrite);


            signatureImage = new PdfBitmap(imageStream);


            signature.Bounds = new Syncfusion.Drawing.RectangleF(new Syncfusion.Drawing.PointF(0, 0), new Syncfusion.Drawing.SizeF(150,150));


            signature.Appearance.Normal.Graphics.DrawImage(signatureImage, signature.Bounds);


            signature.Location = new Syncfusion.Drawing.PointF(306, 429);


            #endregion





UN Unknown June 22, 2023 12:39 PM UTC

Hi Irfana,

Thank you for this, but it is only good for this specific image/pdf. Can you offer any guidance on image sizing/quality that would be universally applicable?

Thanks,

Simon



IJ Irfana Jaffer Sadhik Syncfusion Team June 23, 2023 06:12 AM UTC

This is the actual behavior. When an image is rendered using our PDF library, it is displayed using its original resolution. This means that the image is not stretched or compressed to fit a specific size, but rather displayed at its actual size. This ensures that the image remains sharp and clear, without any distortion or loss of quality.

So Our library does not allow for adjustments to the image resolution. Instead, the image will be rendered using its original resolution. we recommend rendering the image in a manner that corresponds to the coordinates on the PDF page.



JG Jay Goodison September 18, 2023 11:33 AM UTC

Hello

I've picked up this issue for Simon;

I've tested this issue with the same image using different DPI (72, 92 and 300) which has no impact on how the image is rendered on the PDF.

I have also tested by changing the size of the image and it consistently appears to be rendered larger within the PDF than when viewed in any image viewing library.

See below:

175x112.jpg

80x51.jpg

Can you advise further please?



RB Ravikumar Baladhandapani Syncfusion Team September 21, 2023 02:47 PM UTC

Currently, we are analyzing the reported behavior with the provided details on our end and we will provide the further details on September 25th, 2023.



JG Jay Goodison replied to Ravikumar Baladhandapani September 28, 2023 05:12 PM UTC

Are you able to provide an update please?



RB Ravikumar Baladhandapani Syncfusion Team September 29, 2023 04:44 PM UTC

As previously mentioned, the image appears distorted because it is drawn in its original size, which exceeds the width of the page. Therefore, we recommend reducing the image size to fit within the page width. If the given size is smaller than the original size of the image, we will internally scale the image to fit the specified size. However, this does not allow for adjustments to the image resolution. Instead, the image will be displayed with its original resolution. This is our default behavior.



JG Jay Goodison October 12, 2023 03:09 PM UTC

The image does not exceed the width of the page, why do you think it does? You can see clearly in the screen shots provided that the image is 51 pixels wide.



KS Karmegam Seerangan Syncfusion Team October 16, 2023 03:32 PM UTC

Thank you for the update,


After examining it more, we can solve the issue of image distortion by figuring out the aspect ratio for an image within specific limits and then adding it to the PDF file. We've prepared an example for this. Please follow these steps to work out the aspect ratio for the image and then add it to the PDF to achieve the desired result.


Here's a sample to guide you: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ConsoleApp1332960780.zip




PdfBitmap signatureImage = null;

 

signatureImage = new PdfBitmap(File.OpenRead(sigFilePath));

 

signature.Bounds = new Syncfusion.Drawing.RectangleF(0, 0, 150, 130);

 

//Caculate aspect ratio and draw image

 

float imageWidth = signatureImage.PhysicalDimension.Width;

float imageHeight = signatureImage.PhysicalDimension.Height;

float x, y;

 

//calculate image aspect ratio

float widthRatio = signature.Bounds.Width / imageWidth;

float heightRatio = signature.Bounds.Height / imageHeight;

 

 

float ratio = Math.Min(widthRatio, heightRatio);

float drawWidth = imageWidth * ratio;

float drawHeight = imageHeight * ratio;

 

x = signature.Bounds.X + (signature.Bounds.Width - drawWidth) / 2;

y = signature.Bounds.Y + (signature.Bounds.Height - drawHeight) / 2;

 

signature.Appearance.Normal.Graphics.DrawImage(signatureImage, x, y, drawWidth, drawHeight);


Loader.
Up arrow icon