Search code examples
javapdfitextwatermark

How to rotate watermark (text) in PDF using iText?


I'm using iText for stamping a watermark (text: "SuperEasy You Done") on PDF files as described in How to watermark PDFs using text or images? (TransparentWatermark2.java). See project source code on GitHub.

Now an example of the PDF I'm getting is this one (the rest of the document is omitted):

enter image description here

As you can see the watermark is centered and horizontal.

I'd like to keep it centered in the middle of the page, but rotate it "45" degrees, so it rotates anticlockwise. Something like this:

enter image description here

This is the code for stamping the watermark on a given byte array (pdf documents only for me right now)

/**
 * Returns the same document with the watermark stamped on it.
 * @param documentBytes Byte array of the pdf which is going to be returned with the watermark
 * @return byte[] with the same byte array provided but now with the watermark stamped on it.
 * @throws IOException If any IO exception occurs while adding the watermark
 * @throws DocumentException If any DocumentException exception occurs while adding the watermark
 */
private byte[] getDocumentWithWaterMark(byte[] documentBytes) throws IOException, DocumentException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // pdf
    PdfReader reader = new PdfReader(documentBytes);
    int n = reader.getNumberOfPages();
    PdfStamper stamper = new PdfStamper(reader, outputStream);
    // text watermark
    Font font = new Font(Font.HELVETICA, 60);
    Phrase phrase = new Phrase("SuperEasy You Done", font);
    // transparency
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(0.06f);
    // properties
    PdfContentByte over;
    Rectangle pagesize;
    float x, y;
    // loop over every page (in case more than one page)
    for (int i = 1; i <= n; i++) {
        pagesize = reader.getPageSizeWithRotation(i);
        x = (pagesize.getLeft() + pagesize.getRight()) / 2;
        y = (pagesize.getTop() + pagesize.getBottom()) / 2;
        over = stamper.getOverContent(i);
        over.saveState();
        over.setGState(gs1);
        // add text
        ColumnText.showTextAligned(over, Element.ALIGN_CENTER, phrase, x, y, 0);
        over.restoreState();
    }
    stamper.close();
    reader.close();
    return outputStream.toByteArray();
}

PS: I read this, but it didn't help:


Solution

  • You just need to specify the desired rotation angle as the 6th parameter in this line:

    ColumnText.showTextAligned(over, Element.ALIGN_CENTER, phrase, x, y, 0); // rotate 0 grades in this case
    

    If the specified value is positive ( > 0) the rotation is anticlockwise, otherwise (< 0) the rotation is clockwise.

    In this particular case, for rotating the watermark 45 degrees anticlockwise you just need to write the previous line like this:

    ColumnText.showTextAligned(over, Element.ALIGN_CENTER, phrase, x, y, 45f); // 45f means rotate the watermark 45 degrees anticlockwise
    

    By applying this same principle we can achieve any rotation in any direction.


    The whole documentation is here: https://itextpdf.com/en/resources/api-documentation under the links for version 5 and version 7.