8

I want to add an image to a specific position inside an existing PDF file using iText7.
In a different project using iTextSharp, the code was very simple:

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(new Uri(fullPathSignature));
// Set img size and location on page
//-------------------------------------
// item.Width, item.Height
img.ScaleAbsolute(120, 62);

// left: item.X bottom: item.Y
img.SetAbsolutePosition(25, 25);
//-------------------------------------

//Add it to page 1 of the document,
PdfContentByte cb = stamper.GetOverContent(1);
cb.AddImage(img);

But I do not find the correct way to do it with iText7.
I have a PdfReader and a PdfWriter but where can I find the PdfStamper in iText7?
Or maybe there is a different way to add an image to an existing PDF file in iText7?
(I can't use iTextSharp in current project)

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
Mina Shtraicher
  • 99
  • 1
  • 1
  • 5

1 Answers1

35

In iText7, there is no PdfStamper anymore. PdfDocument is responsible for modifying the contents of the document.

To add an image to a page, the easiest way is to use Document class from layout module. With that you almost don't have to care about anything.

To add an image to a specific page at a specific position, you need the following code:

// Modify PDF located at "source" and save to "target"
PdfDocument pdfDocument = new PdfDocument(new PdfReader(source), new PdfWriter(target));
// Document to add layout elements: paragraphs, images etc
Document document = new Document(pdfDocument);

// Load image from disk
ImageData imageData = ImageDataFactory.Create(imageSource);
// Create layout image object and provide parameters. Page number = 1
Image image = new Image(imageData).ScaleAbsolute(100, 200).SetFixedPosition(1, 25, 25);
// This adds the image to the page
document.Add(image);

// Don't forget to close the document.
// When you use Document, you should close it rather than PdfDocument instance
document.Close();
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • Thanks Alexey, I tried your suggestion and I get a NullReferenceException: object reference not set to an instance of an object. – Mina Shtraicher Jan 14 '18 at 16:31
  • @MinaShtraicher that's very strange because the solution is very simple and worked for me. There is literally no place for `NullReferenceException`. Are you sure you are passing correct `imageSource`. Which operation causes the exception? – Alexey Subach Jan 14 '18 at 16:39
  • When I get to "document.Add(image);" I get object reference not set to an instance of an object. My code: iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(reader, writer); iText.Layout.Document document = new iText.Layout.Document(pdf); float x = 25; float y = 25; float w = 120; float h = 62; ImageData imageData = ImageDataFactory.Create(new Uri(fullPathSignature)); iText.Layout.Element.Image image = new iText.Layout.Element.Image(imageData).ScaleAbsolute(w, h).SetFixedPosition(1, x, y); document.Add(image); – Mina Shtraicher Jan 14 '18 at 16:40
  • The imageSource is valid. It worked for the code I wrote in iTextSharp in the other project. – Mina Shtraicher Jan 14 '18 at 16:44
  • 1
    Can you share the PDF you are trying to modify? – Alexey Subach Jan 14 '18 at 17:06
  • Sorry, I found some previous code I forgot to comment which closed the document. It's working fine now. :) – Mina Shtraicher Jan 14 '18 at 17:09
  • This solution works for most image types but will not work with PNG files with transparency – douglas rouse Oct 07 '19 at 14:47
  • Image image = new Image(imageData), I cannot do this it says Image is abstract did they changeany thing in recent – Gajendran Mohan May 06 '20 at 14:25
  • I tried it with the "@" sign and the full path to an image, like so "@"C:\IdioSynchronizer\Development\axXAndSpaceLogo.jpg"", and it failed. Replacing "Imagesource" with that was the only change I made. I get, "Cannot draw elements on already flushed pages" and then the generated PDF won't load. – B. Clay Shannon-B. Crow Raven Jun 23 '20 at 01:29
  • Dude you're a legend thank you ! – Murchiad Sep 16 '22 at 03:48