private static void addWatermarkText(PDDocument doc, PDPage page, PDFont font, String text)
            throws IOException
    {
        try (PDPageContentStream cs
                = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true))
        {
            float fontHeight = 100; // arbitrary for short text
            float width = page.getMediaBox().getWidth();
            float height = page.getMediaBox().getHeight();
            float stringWidth = font.getStringWidth(text) / 1000 * fontHeight;
            float diagonalLength = (float) Math.sqrt(width * width + height * height);
            float angle = (float) Math.atan2(height, width);
            float x = (diagonalLength - stringWidth) / 2; // "horizontal" position in rotated world
            float y = -fontHeight / 4; // 4 is a trial-and-error thing, this lowers the text a bit
            cs.transform(Matrix.getRotateInstance(angle, 0, 0));
            cs.setFont(font, fontHeight);
            // cs.setRenderingMode(RenderingMode.STROKE) // for "hollow" effect
            PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
            gs.setNonStrokingAlphaConstant(0.2f);
            gs.setStrokingAlphaConstant(0.2f);
            gs.setBlendMode(BlendMode.MULTIPLY);
            gs.setLineWidth(3f);
            cs.setGraphicsStateParameters(gs);
            // some API weirdness here. When int, range is 0..255.
            // when float, this would be 0..1f
            cs.setNonStrokingColor(255, 0, 0);
            cs.setStrokingColor(255, 0, 0);
            cs.beginText();
            cs.newLineAtOffset(x, y);
            cs.showText(text);
            cs.endText();
        }
    }
private static void addWatermarkText(PDDocument doc, PDPage page, PDFont font, String text)
            throws IOException
    {
        try (PDPageContentStream cs
                = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true))
        {
            float fontHeight = 100;
            float width = page.getMediaBox().getWidth();
            float height = page.getMediaBox().getHeight();
            float stringWidth = font.getStringWidth(text) / 1000 * fontHeight;
            float diagonalLength = (float) Math.sqrt(width * width + height * height);
            float angle = (float) Math.atan2(height, width);
            float x = (diagonalLength - stringWidth) / 2;
            float y = -fontHeight / 4;
            cs.transform(Matrix.getRotateInstance(angle, 0, 0));
            cs.setFont(font, fontHeight);
           
            PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
            gs.setNonStrokingAlphaConstant(0.2f);
            gs.setStrokingAlphaConstant(0.2f);
            gs.setBlendMode(BlendMode.MULTIPLY);
            gs.setLineWidth(3f);
            cs.setGraphicsStateParameters(gs);
           
           
            cs.setNonStrokingColor(255, 0, 0);
            cs.setStrokingColor(255, 0, 0);
            cs.beginText();
            cs.newLineAtOffset(x, y);
            cs.showText(text);
            cs.endText();
        }
    }
private static void addwatermarktext(pddocument doc, pdpage page, pdfont font, string text) throws ioexception { try (pdpagecontentstream cs = new pdpagecontentstream(doc, page, pdpagecontentstream.appendmode.append, true, true)) { float fontheight = 100; float width = page.getmediabox().getwidth(); float height = page.getmediabox().getheight(); float stringwidth = font.getstringwidth(text) / 1000 * fontheight; float diagonallength = (float) math.sqrt(width * width + height * height); float angle = (float) math.atan2(height, width); float x = (diagonallength - stringwidth) / 2; float y = -fontheight / 4; cs.transform(matrix.getrotateinstance(angle, 0, 0)); cs.setfont(font, fontheight); pdextendedgraphicsstate gs = new pdextendedgraphicsstate(); gs.setnonstrokingalphaconstant(0.2f); gs.setstrokingalphaconstant(0.2f); gs.setblendmode(blendmode.multiply); gs.setlinewidth(3f); cs.setgraphicsstateparameters(gs); cs.setnonstrokingcolor(255, 0, 0); cs.setstrokingcolor(255, 0, 0); cs.begintext(); cs.newlineatoffset(x, y); cs.showtext(text); cs.endtext(); } }