views:

7567

answers:

4

I have an existing PDF and I can use FdFWriter to input to text boxes. It works well. Now I have an image. I have read the documentation and looked at many examples but they all create new documents and insert an image. I want to take an existing PDF and insert an image into either an image field or as the icon image of a button. I have tried but it corrupts the document.

I need to be able to take an existing document and put an image on it. I do not want to open, read, replace, and delete the original. This original changes and the name "original" only means the source file in this context. There are many PDF files like this that need an image.

Thank you for any help.

Edit - I am very thankful for the code below. It works great, but the problem for me is that the existing PDF has digital signatures on it. When the document is copied like this (into result.pdf) those signatures, while still present, have a different byte count or other item that is corrupted. This means the signatures, while they show up on result.pdf, have an icon next to them that state "invalid signature."

In case it matters I am using a Topaz signature pad to create my signatures, which has it's own security. Merely copying the PDF will not corrupt it but the process below will.

I am trying to put the image on the existing document, not a copy of it, which in this case matters.

Also, by signature, I mean handwritten, not pin numbers.

Thank you again.

EDIT - Can PdfSignatureAppearance be used for this?

EDIT - I seem to be able to do it with:

var stamper = new PdfStamper(reader, outputPdfStream,'1',true);

+3  A: 

If you want to change the contents of an existing PDF file and add extra content such as watermarks, pagenumbers, extra headers, PdfStamper is the object you need. I have successfully used the following code to insert an image into an existing pdf file to a given absolute position:

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

class Program
{
    static void Main(string[] args)
    {
        using (Stream inputPdfStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream inputImageStream = new FileStream("some_image.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        {
            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);
            var pdfContentByte = stamper.GetOverContent(1);

            Image image = Image.GetInstance(inputImageStream);
            image.SetAbsolutePosition(100, 100);
            pdfContentByte.AddImage(image);
            stamper.Close();
        }
    }
}

When you insert the image you have the possibility to resize it. You can take a look at transformation matrix in the iTextSharp documentation.

Darin Dimitrov
Is there a way to do it where it places the image on the original instead of result.pdf? Thank you for this answer.
johnny
The reason is because the document has digital signatures which are corrupted by the process.
johnny
Digital signature guarantees that the document hasn't been tampered with. You cannot add contents to it without resigning the document.
Darin Dimitrov
What about using PdfSignatureAppearance?
johnny
This seems to work with append:var stamper = new PdfStamper(reader, outputPdfStream,'1',true);
johnny
thanks for saving me x hours trying to work this out myself.
Merritt
A: 

hi i am using iTexhsharp to creat pdf kindly anybody would tell me how to find wheather a string containg img or not and count and also ho to add it to in Cell.Element

manoj
A: 

check here, i am seeing one article named "How to export a image into pdf using itextsharp dll"

http://netprogramminghelp.com/asp.net

peter
A: 

Here is a similar example whichi inserts an image on the page using the stamper:

Gmane iTex Mailing List Post

SkippyFire