1

Building a document generation system for our web app and am branding the document as required. The document is designed in powerpoint and printed to via NitroPdf. The first page is a large image essentially, with a white area in the image. I am attempting to place the branding logo in the whitespace allocated. Positioning is ok, however, my branding image is appearing behind the PDF'd document full page image.

Having googled, i can't seem to find a 'z-index' type function... would have thought i wouldn't be the only one with the issue?

Section of code adding the image is as follows:

        image.ScaleToFit(width, height);
        image.SetDpi(300, 300);

        // Position the logo.
        image.SetAbsolutePosition(fromLeft, fromBottom);

        // Add the image.
        document.Add(image);
TheITGuy
  • 722
  • 4
  • 15

2 Answers2

0

It is very strange that you would need the following line to add an image to an existing PDF:

document.Add(image);

It's as if you're using PdfWriter instead of PdfStamper, which would be very strange.

Perhaps you overlooked the documentation or maybe you didn't search StackOverflow before you started writing your code: How can I insert an image with iTextSharp in an existing PDF?

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

You may have found examples where GetUnderContent() is used. This adds content under the existing content. If you want the content to cover the existing content, you need GetOverContent() as is shown in the code sample.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi, thanks will investigate. The wrapper we use was written a number of years ago, and worked fine until now. Maybe the example used at the time referenced Document. Not sure, will investigate. Thanks for the advice. – TheITGuy Oct 31 '14 at 20:04
  • Ok, after further investigation, we are using PdfReader and PdfWriter, but for some reason using Document to add the image. Must of been an oversight - although has been working. We are basically merging various documents, and then adding required images. A quick change to the PdfContentByte.AddImage and images appear correctly layered. – TheITGuy Oct 31 '14 at 20:25
  • Using PdfWriter and Document throws away all interactive features. Please use iText correctly! – Bruno Lowagie Oct 31 '14 at 21:07
  • There are a large number of online examples using PdfWriter. Are they all using it wrong? – TheITGuy Nov 02 '14 at 19:44
  • 1
    Unfortunately, most of them are not correct, which is frustrating for both the person who wrote the official documentation (that's me) and the people following the wrong advice. Merging documents is done with `PdfCopy` or `PdfSmartCopy`, *not* with `PdfWriter`. – Bruno Lowagie Nov 03 '14 at 07:38
  • Thanks for the advice. Will investigate further. Should be simple enough to fix. – TheITGuy Nov 03 '14 at 09:01
0

Maybe it's a bit late, but I've faced with the same issue and I've solved with a workaround with Paragraphs (hereunder the code in Visual Basic):

Public Class PDF

  Public Doc As Document
  Public Writer As PdfWriter
  Public Cb As PdfContentByte

  Public Sub setFrontImage(ByVal _appendImg As String, align As Integer, x As Integer, y As Integer, ByVal w As Integer, h As Integer, _leading As Integer)

    Dim ct As New ColumnText(Cb)
    Dim ph As Phrase
    Dim ch As Chunk

    Dim p As Paragraph = new Paragraph()
    Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(_appendImg)
    image.ScaleAbsolute(w, h)
    p.Add(new Chunk(image,x,y))

    ct.SetSimpleColumn(p,x, y, w, h, _leading, align)
    ct.Go()

  End Sub
End Class

I saw you used the absolute position to put your logo up your image so am I, consider to modify the usage of Chunk with width and height if you don't need to fit it inside a restricted space.

Simone
  • 1
  • 3