3

i am trying to add an image using itextsharp but not having any luck there are a ton of tutorials for adding an image to a new pdf doc but not and existing pdf so the .add menthod is not avaivlable

i am tring to do use the stamper write method to add image and i dont get any errors but no image shows up

PdfReader reader = new PdfReader(pdfIn); //get pdf

        if (File.Exists(pdfOut)) File.Delete(pdfOut); //reset 
        FileStream fs = new FileStream(pdfOut, FileMode.Create);

        PdfStamper stamper = new PdfStamper(reader, fs);
        try
        {
            //  Convert base64string to bytes array
            Byte[] bytes = Convert.FromBase64String(base64decode);
            iTextSharp.text.Image sigimage = iTextSharp.text.Image.GetInstance(bytes);//
            sigimage.SetAbsolutePosition(10, 10);
            sigimage.ScaleToFit(140f, 120f);
            stamper.Writer.Add(sigimage);
        }catch (DocumentException dex){//log exception here
        }catch (IOException ioex){//log exception here
        }



        AcroFields fields = stamper.AcroFields;
        //repeat for each pdf form fill field       
        fields.SetField("agencyName", name.Value);


        stamper.FormFlattening = true; // set to true to lock pdf from being editable
        stamper.Writer.CloseStream = true;
        stamper.Close();
        reader.Close();
        fs.Close();

2 Answers 2

5

I think you try the following adding it to bytes

        PdfReader reader = new PdfReader(pdfIn)
        FileStream fs = new FileStream(pdfOut, FileMode.Create);
        var stamper = new PdfStamper(reader, fs);
        var pdfContentByte = stamper.GetOverContent(1);
        iTextSharp.text.Image sigimage = iTextSharp.text.Image.GetInstance(bytes);
        sigimage.SetAbsolutePosition(100, 100);
        pdfContentByte.AddImage(sigimage);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you thank you thank you! that was the problem not using pdfContentByte
@Coldtold can you please explain,how to insert into new page?(via stamper). So take existing pdf and insert there an image in new page(specially for image)!? thanks!
2

using following code you can able to add image to each page in an existing pdf file. ( I use this code for desktop application)

string FileLocation = @"C:\\test\\pdfFileName.pdf"; // file path of pdf file
var uri = new Uri(@"pack://application:,,,/projrct_name;component/View/Icons/funnelGreen.png"); // use image from project/application folder (this image will insert to pdf)
var resourceStream = Application.GetResourceStream(uri).Stream; 
PdfReader pdfReader = new PdfReader(FileLocation);
PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf", "(tempFile).pdf"), FileMode.Create));iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream), System.Drawing.Imaging.ImageFormat.Png);
img.SetAbsolutePosition(125, 350); // set the position in the document where you want the watermark to appear.
img.ScalePercent(35f);// not neccessory, use if you want to adjust image
img.ScaleToFit(140f, 100f); // not neccessory, use if you want to adjust image 
PdfContentByte waterMark;

for (int page = 1; page <= pdfReader.NumberOfPages; page++) // for loop will add image to each page. Based on the condition you can add image to single page also 
{
        waterMark = stamp.GetOverContent(page);
    waterMark.AddImage(img);
}

stamp.FormFlattening = true;
stamp.Close();// closing the pdfStamper, the order of closing must be important
pdfReader.Close();

File.Delete(FileLocation);
File.Move(FileLocation.Replace(".pdf", "(tempFile).pdf"), FileLocation);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.