The iText PDF Java Library PRO

Three days ago I had a meeting with the operation manager of one of the israeli training centers I am working with. The meeting started with a short overview of our great cooperation and concluded with their complaint about the huge number of slides I prepare for each training. They pointed at the logistic problem involved with editing the PDF slides adding their logo to each one of them. On my end it was clear that I don’t have the time resources for doing that for them. On the other hand, it was clear that I must find a solution.

Following that meeting I started searching the web for open source Java libraries I can use in order to develop a simple Java application that will automatically manipulate the PDF files and add their logo to each one of them. One of the Java libraries I found was the iText Java library. I remembered that Jasper Reports open source project uses that library as well. I chose to give it a try.

I found the iText PDF Java library amazingly easy to use and within less than 60 minutes I already had a working application for doing the required work.

Should you ever have a need in manipulating PDF files I strongly recommend you to try the iText PDF Java library. It rocks! You can download it for free at www.itextpdf.com where you can also find a detailed API documentation.

The following code is the small application I developed for doing the PDF files manipulation in an automatic way. It goes over all PDF files located within a specific folder and adds a logo image to each one of them. If you ever have a need for doing a simlar manipulation I believe you can find this code useful on your end.

package com.xperato.utils;

import java.io.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfContentByte;

public class AddLogoApplication
{
 public static void main(String args[]) throws Exception
 {
  if(args.length==0)
  {
   System.out.println(“args[0] is the folder where all original PDF files are saved”);
   System.out.println(“args[1] is the name of the image file”);
   System.out.println(“args[2] is the prefix we should add to each PDF filename”);
   return;
  }
  String[] files = new File(args[0]).list();
  for(int i=0; i<files.length; i++)
  {
   if(files[i].endsWith(“.pdf”))
   {
    createNewFileWithLogo(files[i],Image.getInstance(args[1]),args[2],args[0]);  
   }  
  }
 }
 public static void createNewFileWithLogo(String fileName, Image img, String prefix, String folder) throws Exception
 { 
  PdfReader original = new PdfReader(folder+File.separator+fileName);
  PdfStamper stamper = new PdfStamper(original,new FileOutputStream(folder+File.separator+prefix+fileName));
  img.setAbsolutePosition(10,20);
  PdfContentByte layer;
  int total = original.getNumberOfPages() + 1;
  for (int i = 1; i < total; i++)
  {
   layer = stamper.getOverContent(i);
   layer.addImage(img);
  }
  stamper.close();
 }
}

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences