Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Guys,

I have a scenario where in I need to add a signature (image file) stored in db to a PDF which is also present in same db in a different table.

Now this is to be done for multiple times on a single button click.

Like , adding the same signature image to multiple PDF files selected in the grid above via a check box.

I again need to save (update )that updated PDF file with signature in the same table in same db.

I need - Saving a signature into a PDF at a particular position (in the last page) and saving it back to db.

Please help guys.


Thanks!!
Sam
Posted

1 solution

You can use iTextSharp.
Something like this might work for you:

C#
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();
  }
}


Cheers,
Edo
 
Share this answer
 
Comments
Sanman Marathe 2-May-13 10:33am    
Hi Edo,

Actually i tried itextsharp also. It is working,, but not for my scenario..

I tried with FileStream outStream = new FileStream(targetFilename, FileMode.Create)

even with FileMode.Append,FileAccess.Write

But all it does is it removes the previously written data in pdf and writes this new image only.

Actually i needed to add an image at bottom of pdf and also pertain the old data already present in pdf..


but i'll try the above code which u gave as it is again.....


Thanks!!


I had tried this code...

private void AppendPDFFile()
{
Document doc = new Document();
try
{
string pdfFilePath = Server.MapPath(".") + "/pdf/MynewPDF.pdf";

//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Append,FileAccess.Write));
doc.Open();//Open Document to write

// Now image in the pdf file
// iTextSharp.text.Image ppp = iTextSharp.text.Image.GetInstance(pdfFilePath);
string imageFilePath = Server.MapPath(".") + "/image/Desert.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);


//Resize image depend upon your need
jpg.ScaleToFit(100f, 80f);

//Give space before image
// jpg.SpacingBefore = 10f;

//Give some space after the image
// jpg.SpacingAfter = 1f;
//jpg.Alignment = Element.ALIGN_BOTTOM;


doc.Add(jpg); //add an image to the created pdf document

}
catch (DocumentException docEx)
{
//handle pdf document exception if any
}
catch (IOException ioEx)
{
// handle IO exception
}
catch (Exception ex)
{
// ahndle other exception if occurs
}
finally
{
//Close document and writer
doc.Close();

}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900