Insert an image into an existing pdf document using iText Sharp

imageitextsharp

I need to insert an image into an existing pdf at a specific location. I tried the answer at this question. But whatever different ways I do the image is being inserted at (0,0) position (bottom left corner). I tried another approach where instead of using stream I used Document class in iTextSharp as shown here. Now I am able to place the image at the desired position but this method is creating a new document with just this image. Most of the articles I searched are using PdfReader and PdfStamper so I think this is the recommended way. Any help is appreciated. Posting below code for both the methods I tried.

PdfStamper method

   private void AddImage(string filePath)
    {
        string imageURL = @"ImagePath\Image.jpg";

        using (Stream inputPdfStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        using (Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read))
        using (Stream outputPdfStream = new FileStream(@"ResultingPdfPath\Abcd.pdf", FileMode.Create, FileAccess.ReadWrite))
        {
            Image image = Image.GetInstance(inputImageStream);
            image.ScaleToFit(100, 100);

            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);

            PdfContentByte content = stamper.GetUnderContent(1);
            image.SetAbsolutePosition(100f, 150f);
            content.AddImage(image);

            stamper.Close();

            reader.Close();
        }
    }

Document class method

    private void TestMessage(string filePath)
    {
        string imageURL = @"ImagePath\Image.jpg";

        Document doc = new Document(PageSize.A4);

        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Open));

        doc.Open();

        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
        jpg.ScaleToFit(140f, 120f);
        jpg.SetAbsolutePosition(100, 100);
        jpg.SpacingBefore = 10f;
        jpg.SpacingAfter = 1f;
        jpg.Alignment = Element.ALIGN_LEFT;
        doc.Add(jpg);
        doc.Close();
    }

Let me know if you need further information.

Best Answer

I adapted your method to accept variable out paths and positions and tested it with iTextSharp 5.5.7 like this:

[TestFixture]
class TestInsertImage
{
    /// iText stamp image on top not always working
    /// http://stackoverflow.com/questions/33898280/itext-stamp-image-on-top-not-always-working
    /// 
    [Test]
    public void AddStampToTestPdf()
    {
        Directory.CreateDirectory(@"C:\Temp\test-results\content\");

        AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-100-150.pdf", 100f, 150f);
        AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-150-100.pdf", 150f, 100f);
    }

    private void AddImage(string filePath, string outPath, float x, float y)
    {
        string imageURL = @"c:\Repo\GitHub\testarea\itext5\src\test\resources\mkl\testarea\itext5\layer\Willi-1.jpg";

        using (Stream inputPdfStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        using (Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read))
        using (Stream outputPdfStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite))
        {
            Image image = Image.GetInstance(inputImageStream);
            image.ScaleToFit(100, 100);

            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);

            PdfContentByte content = stamper.GetUnderContent(1);
            image.SetAbsolutePosition(x, y);
            content.AddImage(image);

            stamper.Close();

            reader.Close();
        }
    }
}

The results are included below.

As you see, the positioning information clearly are respected, and the image is definitely not always at the bottom left corner.

If this indeed does not work for the OP, he is keeping information from us required to help him.

Multipage-stamp-Image-100-150.pdf

At 100, 150

Created using

AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-100-150.pdf", 100f, 150f);

Multipage-stamp-Image-150-100.pdf

At 150, 100 Created using:

AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-150-100.pdf", 150f, 100f);
Related Topic