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

丶灬走出姿态 提交于 2020-01-02 13:33:13

问题


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):

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:

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:

  • http://itext.2136553.n4.nabble.com/rotate-a-watermark-td2155042.html

回答1:


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://developers.itextpdf.com/apis under the links for version 5 and version 7.



来源:https://stackoverflow.com/questions/52748248/how-to-rotate-watermark-text-in-pdf-using-itext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!