Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to adding a user's signature to a PDF document online?

I need to add a user's signature (like your signature on a credit card, for example) to a PDF document online. How can I do it? What is the best practice for this kind of tasks?

like image 339
misho Avatar asked Feb 24 '23 02:02

misho


1 Answers

You can use iTextSharp to do it. Something like this will work for you:

PdfReader pdfReader = null;
PdfStamper pdfStamper = null;

// Open the PDF file to be signed
pdfReader = new PdfReader(this.signFile.FullName);

// Output stream to write the stamped PDF to
using (FileStream outStream = new FileStream(targetFilename, FileMode.Create))
{
  try
  {
    // Stamper to stamp the PDF with a signature
    pdfStamper = new PdfStamper(pdfReader, outStream);

    // Load signature image
    iTextSharp.text.Image sigImg = iTextSharp.text.Image.GetInstance(SIGNATURE_IMAGE_PATH);

    // Scale image to fit
    sigImg.ScaleToFit(MAX_WIDTH, MAX_HEIGHT);

    // Set signature position on page
    sigImg.SetAbsolutePosition(POS_X, POS_X);

    // Add signatures to desired page
    PdfContentByte over = pdfStamper.GetOverContent(PAGE_NUM_TO_SIGN);
    over.AddImage(sigImg);
  }
  finally
  {
    // Clean up
    if (pdfStamper != null)
      pdfStamper.Close();

    if (pdfReader != null)
      pdfReader.Close();
  }
}
like image 179
Cocowalla Avatar answered Mar 30 '23 01:03

Cocowalla