Parag Solanki
Parag Solanki

Reputation: 3

PDF file with watermark image created using iText java

While sending a pdf file to the printer it gives an error like "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."

I'm creating a PDF file and also adding a watermark image to it using the text java.

If remove the watermark image from the PDF file then it's working fine.

Don't know what the exact problem with watermark image? Please help.

Below is the code snippet :

PdfReader pdfReader = new PdfReader(finalPath);
int noOfPages = pdfReader.getNumberOfPages();

PdfStamper stamp = new PdfStamper(pdfReader, new FileOutputStream(fileNameAfterWatermark));
int i = 0;
PdfContentByte underContent;    
PdfGState gs;
while (i < noOfPages) {
    i++;
    underContent = stamp.getUnderContent(i);
    gs = new PdfGState();
    gs.setFillOpacity(0.3f);                
    gs.setStrokeOpacity(0.3f);              
    Rectangle  pagesize = pdfReader.getPageSize(i);
    int pageRotation = pdfReader.getPageRotation(i);
    float  x = (pagesize.getLeft() + pagesize.getRight()) / 2 ;
    float  y = (pagesize.getTop() + pagesize.getBottom()) / 2 ;
    if(pageRotation != 0){

         x = (pagesize.getHeight()) / 2;
         y = (pagesize.getWidth()) / 2;

         y = y - 80;

    }
    float w = image.getScaledWidth();
    float h = image.getScaledHeight();
    float scaleMultiplicationFactor = 1.25f;
    float image_width = (w * (scaleMultiplicationFactor));
    float image_height = (h * (scaleMultiplicationFactor));
    float x_co_ordinate = x - (image_width / 2 );
    float y_co_ordinate = y - (image_height / 2);
    int fontSize = 180;

    underContent.saveState();
    underContent.setGState(gs);
    underContent.beginText();
    underContent.setFontAndSize(bf, fontSize);
    underContent.setColorFill(Color.LIGHT_GRAY);
    underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
    underContent.endText();
    underContent.restoreState();

}

stamp.close();
pdfReader.close();

Upvotes: 0

Views: 1547

Answers (1)

mkl
mkl

Reputation: 95918

You add your watermark content to the UnderContent like this:

underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.endText();
underContent.restoreState();

I.e. you add your (bitmap?) image to it inside a text object. This is not valid, text objects may not contain external objects or inline image objects. Add the image outside the text object:

underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.endText();
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.restoreState();

That being said, you don't add anything inside that text object. Thus, you can reduce the code to:

underContent.saveState();
underContent.setGState(gs);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.restoreState();

Furthermore, you add that content to the UnderContent. Thus, the transparency you set in the PdfGState merely makes the image paler. If you could make the original bitmap as pale as eventually desired, you do not need to use that PdfGState at all. In some profiles of PDF transparency is forbidden, so getting rid of it may also be advantageous...

Upvotes: 1

Related Questions