Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a image under the text as a pdf background using iText?

I need some sample code to insert a image as a pdf background, is there any this kind of sample code ? and I have wrote the text well, then i need to insert a image under the text.

like image 329
MemoryLeak Avatar asked Nov 25 '09 04:11

MemoryLeak


People also ask

Is iText PDF free?

This license is a commercial license. You have to pay for it. To answer your question: iText can be used for free in situations where you also distribute your software for free. As soon as you want to use iText in a closed source, proprietary environment, you have to pay for your use of iText.

What is iText format?

iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis Java and Eclipse knowledge. iText has a hierarchical structure.


1 Answers

I think you are looking for water marking the pages in a PDF file.. check the below code. You could also use the Watermarker class.

PdfReader reader = new PdfReader("text.pdf");
  int n = reader.getNumberOfPages();

  // Create a stamper that will copy the document to a new file
  PdfStamper stamp = new PdfStamper(reader, 
    new FileOutputStream("text1.pdf"));
  int i = 1;
  PdfContentByte under;
  PdfContentByte over;

  Image img = Image.getInstance("watermark.jpg");
  BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
    BaseFont.WINANSI, BaseFont.EMBEDDED);

  img.setAbsolutePosition(200, 400);

  while (i < n) 
  {
    // Watermark under the existing page
    under = stamp.getUnderContent(i);
    under.addImage(img);

    // Text over the existing page
    over = stamp.getOverContent(i);
    over.beginText();
    over.setFontAndSize(bf, 18);
    over.showText("page " + i);
    over.endText();

    i++;
  }

  stamp.close();

Regards,
Abdel Olakara

like image 117
Abdel Raoof Olakara Avatar answered Nov 01 '22 17:11

Abdel Raoof Olakara