java给pdf添加水印
public static void insertWatermark(PdfPageBase page) {
Dimension2D dimension2D = new Dimension();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 14), true);
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 3, page.getCanvas().getClientSize().getHeight() / 3);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.4F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 3, (float) brush.getSize().getHeight() / 3);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString("陕西省市场监督管理局", font1, 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);
}
public static void addAdImagesToPdf4Ypggsp(String pdfPath, String adPath, String outputPath, boolean isShiPin) {
PdfDocument doc = new PdfDocument();
doc.loadFromFile(pdfPath);
PdfPageBase page = null;
PdfImage image = PdfImage.fromFile(adPath);
if (isShiPin) {
page = doc.getPages().insert(doc.getPages().getCount());
float width = image.getWidth();
float height = image.getHeight();
if (width > 1024 || height > 768) {
page.getCanvas().drawImage(image, 1, 1, image.getWidth() * 0.3f, image.getHeight() * 0.3f);
}else{
page.getCanvas().drawImage(image, 1, 1, image.getWidth() * 0.8f, image.getHeight() * 0.8f);
}
} else {
page = doc.getPages().get(doc.getPages().getCount() - 1);
page.getCanvas().drawImage(image, 100, 200, 300, 230);
}
insertWatermark(doc.getPages().get(0));
insertWatermark(doc.getPages().get(1));
File f = new File(outputPath);
if (f.exists()) {
f.delete();
}
doc.saveToFile(outputPath);
doc.close();
}
public static boolean addImagesToPdfPage4Ggsp(String pdfPath, String fzImg, boolean isYp, String yzImg, String ldspDate, String pzwh, String yxqz, String outputPath, String fontPath) {
return addImagesToPdfPage4Ggsp(pdfPath, fzImg, isYp, yzImg, ldspDate, pzwh, yxqz, outputPath, fontPath,0,0);
}
public static boolean addImagesToPdfPage4Ggsp(String pdfPath, String fzImg, boolean isYp, String yzImg, String ldspDate, String pzwh, String yxqz, String outputPath, String fontPath,int x_plu,int y_plu) {
PdfDocument doc = new PdfDocument();
try {
doc.loadFromFile(pdfPath);
} catch (Exception e) {
System.out.println("文件不存在异常。。");
e.printStackTrace();
doc.close();
return false;
}
PdfPageBase secondPage = doc.getPages().get(doc.getPages().getCount() - 1);
PdfPageBase page = doc.getPages().get(0);
PdfImage image = PdfImage.fromFile(fzImg);
PdfImage image2 = PdfImage.fromFile(yzImg);
if (isYp) {
page.getCanvas().drawImage(image, 140+x_plu, 580+y_plu, image.getWidth() * 0.5f, image.getHeight() * 0.5f);
} else {
page.getCanvas().drawImage(image, 140+x_plu, 580+y_plu, image.getWidth() * 0.9f, image.getHeight() * 0.9f);
}
page.getCanvas().drawImage(image2, 343+x_plu, 550+y_plu, 120, 120);
secondPage.getCanvas().drawImage(image2, 240+x_plu, 300+y_plu, 120, 120);
File f = new File(outputPath);
if (f.exists()) {
f.delete();
}
doc.saveToFile(outputPath);
doc.close();
return true;
}
public static void main(String[] args) {
PdfImageHelper.addAdImagesToPdf4Ypggsp("f:\\市场局广告测试盖章\\2019092510033100383.pdf", "f:\\市场局广告测试盖章\\1.jpg", "f:\\市场局广告测试盖章\\2019092510033100383X.pdf", true);
PdfImageHelper.addImagesToPdfPage4Ggsp("f:\\市场局广告测试盖章\\2019092510033100383.pdf",
"f:\\市场局广告测试盖章\\f2.png", true, "f:\\市场局广告测试盖章\\yz.png", "",
"", "", "f:\\市场局广告测试盖章\\2019092510033100383X.pdf", "f:\\市场局广告测试盖章\\Deng.ttf",0,0);
}