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
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.
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.
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 |
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
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.
.
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:
Can you advise further please?
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.
Are you able to provide an update please?
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.
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.
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); |