/// <summary> /// The below method is an example from https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/AddWatermarkText.java?revision=1873147&view=markup /// </summary> /// <param name="doc"></param> /// <param name="page"></param> /// <param name="font"></param> /// <param name="text"></param> static void addWatermarkText(PDDocument doc, PDPage page, PDFont font, string text) { using (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)System.Math.Sqrt(width * width + height * height); float angle = (float)System.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(new Float(0.2f)); gs.setStrokingAlphaConstant(new Float(0.2f)); gs.setBlendMode(BlendMode.MULTIPLY); gs.setLineWidth(new Float(3f)); cs.setGraphicsStateParameters(gs); cs.setNonStrokingColor(Color.red); cs.setStrokingColor(Color.red); cs.beginText(); cs.newLineAtOffset(x, y); cs.showText(text); cs.endText(); } }