java写代码加水印 java添加代码( 三 )


代码如下:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class Textwatermark {
public static void main(String[] args) {
//创建PdfDocument对象
PdfDocument pdf = new PdfDocument();
//加载示例文档
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//获取第一页
PdfPageBase page = pdf.getPages().get(0);
//调用insertWatermark方法插入文本水印
insertWatermark(page, "E-ICEBLUE");
//保存文档
pdf.saveToFile("out/textWaterMark.pdf");
}
static void insertWatermark(PdfPageBase page, String watermark) {
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.3F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(1);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}
}
希望对您有帮助 。
【java写代码加水印 java添加代码】java写代码加水印的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java添加代码、java写代码加水印的信息别忘了在本站进行查找喔 。

推荐阅读