How to add png image next to the text in the bound of signature filed both should be in the signature field in the exsting pdf

1 Answer 38 Views
General Discussions PdfProcessing SpreadProcessing SpreadStreamProcessing WordsProcessing ZipLibrary
Vaibhav
Top achievements
Rank 1
Iron
Iron
Vaibhav asked on 28 Nov 2024, 04:55 AM

Hi,

1) i want to add and image and text in the signature field with specified size and i want to do that in the existing pdf with signature field ?
2) how to add any type of image [jpg, jpeg , png, etc.] in extsting pdf 
 static byte[] GenerateSignedPdf(Cert objCert)
 {
     // Dimensions and positions for the signature field
     int signatureFieldWidth = 200;
     int signatureFieldHeight = 50;
     int signaturePositionLeft = 10;
     int signaturePositionTop = 10;

     // Load the certificate
     X509Certificate2 certificate = new X509Certificate2(objCert.CertArray, objCert.Password);

     // Create a SignatureField and assign the digital signature to it
     SignatureField pdfSignature = new SignatureField("SignatureField");
     pdfSignature.Signature = new Telerik.Windows.Documents.Fixed.Model.DigitalSignatures.Signature(certificate);

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

     // Draw the text with certificate holder's name and current date
     string textToDraw = $"{certificate.GetNameInfo(X509NameType.SimpleName, false)} {DateTime.Now:yyyy.MM.dd HH:mm}";
     editor.DrawText(textToDraw, new Telerik.Documents.Primitives.Size(5, 5)); // Adjust position as needed

     // Path to the image (ensure this is correct)
     string imagePath = "C:/Vaibhav/ProjectVaibhav/PdfProcessing_InsertImageInExistingPdf/image.png"; // Change path as needed

     // Ensure the image exists and can be accessed
     if (File.Exists(imagePath))
     {
         using (var imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
         {
             var imageSource = new ImageSource(imageStream);
             editor.DrawImage(imageSource, new Telerik.Documents.Primitives.Size(100, 50)); // Adjust image size and position as needed
         }
     }
     else
     {
         throw new FileNotFoundException("Image file not found at: " + imagePath);
     }

     // 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, signaturePositionTop, signatureFieldWidth, signatureFieldHeight);
     signatureWidget.RecalculateContent();

     // Create a RadFixedDocument and add a page
     RadFixedDocument document = new RadFixedDocument();
     RadFixedPage pdfPage = document.Pages.AddPage();
     pdfPage.Annotations.Add(signatureWidget);

     // Add content from the form to the page at the specified position
     FixedContentEditor pageEditor = new FixedContentEditor(pdfPage);
     pageEditor.Position.Translate(signaturePositionLeft, signaturePositionTop);
     pageEditor.DrawForm(pdfForm.FormSource);

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

     // Use MemoryStream to capture the PDF output and return as a byte array
     using (MemoryStream memoryStream = new MemoryStream())
     {
         var pdfFormatProvider = new PdfFormatProvider();
         pdfFormatProvider.Export(document, memoryStream);
         return memoryStream.ToArray(); // Return the byte array of the PDF
     }
 }

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 02 Dec 2024, 02:35 PM

Hello Vaibhav,

My name is Yoan from the Document Processing team and I will get straight to answering your questions:

1) It is indeed possible to create a signature field with text and images by using the PdfProcessing library. I can see you are already utilizing the Signing a Document example which uses the FixedContentEditor for drawing/inserting the context onto the page. To add an image you can continue using the editor and its DrawImage method, but you need to make sure the pdfForm.FormSource.Size has enough space to fit the image as well. If the image is out of bounds or too big it will not be displayed.

I am attaching for your disposal a sample project that implements the shared code snippet but with minor modifications - the editor must be moved before inserting the image (otherwise the image will be drawn on top of the text) and the pdfForm.FormSource.Size must be resized to fit the image.

I am also sharing some articles on this topic which you might find useful:

2) There are several ways to insert images with PdfProcessing:

  • Drawing them with the FixedContentEditor by manually specifying their size and position - recommended for this scenario as this editor is already being utilized.
    RadFixedDocument document = new RadFixedDocument();
    RadFixedPage pdfPage = document.Pages.AddPage();
    FixedContentEditor editor = new FixedContentEditor(pdfPage); // Can also use formSource
    editor.DrawImage(new FileStream(Path.Combine("..\\..\\..\\sample.jpg"), FileMode.Open), imageWidth, imageHeight);
RadFixedDocument document = new RadFixedDocument();

var radFixedDocumentEditor = new RadFixedDocumentEditor(document);

ImageSource imageSource = new ImageSource(new FileStream("image.jpeg", FileMode.Open));
radFixedDocumentEditor.InsertImageInline(imageSource, new Size(100, 100));
  • Creating an Image instance and inserting it in the RadFixedPage.Content collection:
RadFixedDocument fixedDocument = new RadFixedDocument(); 
RadFixedPage fixedPage = fixedDocument.Pages.AddPage(); 
 
Image image = new Image(); 
string imageFilePath = "ProgressNinjas.png"; 
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open); 
ImageSource imageSrc = new ImageSource(fileStream); 
image.ImageSource = imageSrc; 
image.Width = 200; 
image.Height = 200; 
image.AlphaConstant = 0.5;  
SimplePosition simplePosition = new SimplePosition(); 
simplePosition.Translate(200, 300); 
image.Position = simplePosition; 
fixedPage.Content.Add(image); 

NOTE: Using both editors at the same time is not recommended as it can cause conflicts and unexpected results.

I hope this helps.

Regards,
Yoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

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