46

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);

johnny
  • 19,272
  • 52
  • 157
  • 259
  • This also may be of use: http://stackoverflow.com/questions/7115242/insert-an-image-in-pdf-using-itextsharp – Joe User Apr 03 '12 at 21:44

5 Answers5

81

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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 Feb 25 '09 at 16:13
  • The reason is because the document has digital signatures which are corrupted by the process. – johnny Feb 25 '09 at 16:50
  • 4
    Digital signature guarantees that the document hasn't been tampered with. You cannot add contents to it without resigning the document. – Darin Dimitrov Feb 25 '09 at 18:28
  • What about using PdfSignatureAppearance? – johnny Feb 25 '09 at 18:29
  • This seems to work with append: var stamper = new PdfStamper(reader, outputPdfStream,'1',true); – johnny Feb 26 '09 at 02:46
  • 1
    Wouldn't you want to put your stamper in the using clause? – Mike Cole Jun 07 '13 at 02:20
  • image added in document bt it overlap on text/table in pdf, how to solve tht? help me – Hiral Jun 07 '13 at 12:44
  • @Hiral You can adjust the position by changing `image.SetAbsolutePosition(100, 100)`. These values are the position relative to the bottom left corner of the page, i.e. `image.SetAbsolutePosition(0, 0)` would place it in the bottom left corner. – FarFigNewton Apr 13 '16 at 20:53
  • Does adding the image to pdfContentByte place it in the output stream? – Tim Aug 18 '16 at 20:21
  • Thanks, worked for me. For people who are looking to add images on multiple pages, you can use this to get total number of pages: `int pageCount = pdfReader.NumberOfPages();` Get bytes of next page by using this line: `var pdfContentByte = stamper.GetOverContent(pageNumber);` Hope it helps – Saad Khan Nov 30 '19 at 13:21
1

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

Gmane iTex Mailing List Post

John B
  • 20,062
  • 35
  • 120
  • 170
1

I could solve my problem by simply adding following lines to my signing code to add image

 var image = iTextSharp.text.Image.GetInstance(@"C:\Users\sushil\Documents\sansign.jpg");
appearance.Acro6Layers = true;
appearance.SignatureGraphic = image;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;

As I was signing document with visible digital signature , now I can have both image and digital signature properties side by side

goto
  • 7,908
  • 10
  • 48
  • 58
sushil.agarwal
  • 151
  • 1
  • 9
  • The problem of the op was to add an image to an existing, already signed pdf, not to set the image of a signature visualisation. – mkl Feb 26 '17 at 15:07
  • i think adding any thing to signed document invalidates the digital signed document. please correct me if i am wrong – sushil.agarwal Feb 27 '17 at 07:08
  • For details cf. http://stackoverflow.com/documentation/pdf/5161/integrated-pdf-signatures/18524/allowed-and-disallowed-changes-to-a-signed-document#t=20170227100646546925 – mkl Feb 27 '17 at 10:07
0

in the .net core6 that uses DDD try this declare class in Infrastructure Layer

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public async Task<string> SignatureToPdf(string pathPdfFile, string 
pathSignatureImage, string pathOutputName)
    {
        var webRootPath = hostingEnvironment.ContentRootPath;
        if (!File.Exists(Path.Combine(webRootPath, pathPdfFile))) return 
null;

        await using Stream inputPdfStream = 
            new FileStream(Path.Combine(webRootPath, pathPdfFile), 
FileMode.Open, FileAccess.Read, FileShare.Read);
        await using Stream inputImageStream = 
            new FileStream(Path.Combine(webRootPath, pathSignatureImage), FileMode.Open, FileAccess.Read, FileShare.Read);
        await using Stream outputPdfStream = 
            new FileStream(Path.Combine(webRootPath, pathOutputName), 
FileMode.Create, FileAccess.Write, FileShare.None);

        var reader = new PdfReader(inputPdfStream);
        var stamper = new PdfStamper(reader, outputPdfStream);
        var pdfContentByte = stamper.GetOverContent(1);

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

        return "ok";

    }
-1

pdftk can do this. It's not a library but you can easily call it from your code as a .exe.

See stamp and background commands: http://www.pdflabs.com/docs/pdftk-man-page/

ref: How to do mail merge on top of a PDF?

Community
  • 1
  • 1
Douglas Anderson
  • 4,652
  • 10
  • 40
  • 49