Java 实现word转PDF 并给PDF添加水印

所需要的jar在这个链接里https://blog.****.net/qq_34315636/article/details/95358305

上代码

/**
 * 
 * inPath :原文档路径   outPath: 要生产的pdf路径  imgUrl :水印图片路径
 */
public static void docToPdfWatermark(String inPath,String outPath,String imgUrl) {
    // 验证License 若不验证则转化出的pdf文档会有产品信息的水印产生
    if (getLicense()) {
       try {
         FileOutputStream os = new FileOutputStream(new File(outPath));
         Document doc = new Document(inPath); 
         Assert.assertNull(doc.getBackgroundShape());
         Shape shapeRectangle = new Shape(doc, ShapeType.RECTANGLE);
         doc.setBackgroundShape(shapeRectangle);
         //添加水印图片
         shapeRectangle.getImageData().setImage(imgUrl);
         Assert.assertTrue(doc.getBackgroundShape().hasImage());
         //降低对比度,增加亮度
         shapeRectangle.getImageData().setContrast(0.2);
         shapeRectangle.getImageData().setBrightness(0.7);
         doc.save(os, SaveFormat.PDF);
         os.close();
       } catch (Exception e) {
         e.printStackTrace();
      }
    }
    
}

下面是自己做的水印图片

Java 实现word转PDF 并给PDF添加水印

这里是生成带水印的pdf效果

Java 实现word转PDF 并给PDF添加水印