• java 实现给PDF、PPT添加水印


    1. java实现PDF添加水印

    pom.xml引入依赖         


                com.itextpdf
                itextpdf
                5.5.13.3
            
     

    1. import com.itextpdf.text.Element;
    2. import com.itextpdf.text.pdf.BaseFont;
    3. import com.itextpdf.text.pdf.PdfContentByte;
    4. import com.itextpdf.text.pdf.PdfGState;
    5. import com.itextpdf.text.pdf.PdfReader;
    6. import com.itextpdf.text.pdf.PdfStamper;
    7. import java.io.FileOutputStream;
    8. /**
    9. * PDF文件水印添加
    10. * @author jia
    11. *
    12. */
    13. public class PDFWaterMarkUtil {
    14. /**
    15. *
    16. * @param srcPath 源文件路径
    17. * @param destPath 目的文件路径
    18. * @param word 添加水印(不支持汉字)
    19. * @throws Exception
    20. */
    21. public static void addPDFWaterMark(String srcPath, String destPath, String word)
    22. throws Exception {
    23. PdfReader reader = new PdfReader(srcPath);
    24. PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPath));
    25. //创建字体,第一个参数是字体路径
    26. BaseFont base = BaseFont.createFont();
    27. //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
    28. PdfGState gs = new PdfGState();
    29. gs.setFillOpacity(0.2f);//图片水印透明度
    30. //gs.setStrokeOpacity(0.4f);//设置笔触字体不透明度
    31. PdfContentByte content = null;
    32. int total = reader.getNumberOfPages();//pdf文件页数
    33. for (int i=0; i
    34. float x = reader.getPageSize(i+1).getWidth();//页宽度
    35. float y = reader.getPageSize(i+1).getHeight();//页高度
    36. content = stamper.getOverContent(i+1);
    37. content.setGState(gs);
    38. content.beginText();//开始写入
    39. content.setFontAndSize(base, 20);//字体大小
    40. //每页3行,一行3个
    41. for (int j=0; j<3; j++) {
    42. for (int k=0; k<3; k++) {
    43. //showTextAligned 方法的参数(文字对齐方式,位置内容,输出水印X轴位置,Y轴位置,旋转角度)
    44. content.showTextAligned(Element.ALIGN_CENTER, word, x/3*j+100, y/3*k+100, 45);
    45. }
    46. }
    47. content.endText();//结束写入
    48. }
    49. //关闭流
    50. stamper.close();
    51. reader.close();
    52. }
    53. public static void main(String[] args) {
    54. // 获取指定路径的pdf
    55. try {
    56. addPDFWaterMark("H:\\test.pdf" , "H:\\example_water.pdf" , "jia");
    57. } catch (Exception e) {
    58. // TODO Auto-generated catch block
    59. e.printStackTrace();
    60. }
    61. }
    62. }

    2.java实现PPT添加水印

    pom.xml引入poi



        org.apache.poi
        poi-ooxml
        4.1.2

     

    1. import java.awt.Color;
    2. import java.awt.geom.Rectangle2D;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import org.apache.poi.sl.usermodel.TextShape.TextDirection;
    7. import org.apache.poi.sl.usermodel.VerticalAlignment;
    8. import org.apache.poi.xslf.usermodel.XMLSlideShow;
    9. import org.apache.poi.xslf.usermodel.XSLFSlide;
    10. import org.apache.poi.xslf.usermodel.XSLFTextBox;
    11. public class PPTWaterMarkUtil {
    12. public static void setPPTWaterMark(String path,String targetpath, String markStr) throws IOException {
    13. XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(path));
    14. double x = slideShow.getPageSize().getWidth();
    15. double y = slideShow.getPageSize().getHeight();
    16. for (XSLFSlide slide : slideShow.getSlides()) {
    17. for (int j=0; j< 3; j++) {
    18. // for (int k=0; k< 2; k++) {
    19. //
    20. XSLFTextBox textBox = slide.createTextBox();
    21. textBox.setTextDirection(TextDirection.VERTICAL_270);//设置文本框文字方向
    22. textBox.setVerticalAlignment(VerticalAlignment.MIDDLE);
    23. textBox.setText(markStr);
    24. textBox.setAnchor(new Rectangle2D.Double(x/3*j+90,y/2, 25, 160)); // 设置水印文本框的位置和大小
    25. textBox.setRotation(45); // 设置水印文本框的旋转角度
    26. // textBox.setFillColor(new Color(0, 0, 0, 128)); // 设置水印文本框的填充颜色
    27. textBox.setLineColor(new Color(0, 0, 0, 128));
    28. // textBox.setLineWidth(1);
    29. // }
    30. }
    31. }
    32. FileOutputStream out = new FileOutputStream(targetpath);
    33. slideShow.write(out);
    34. out.close();
    35. }
    36. public static void main(String[] args) {
    37. try {
    38. setPPTWaterMark("H:/waterppt.pptx", "H:/watermark0.pptx", "Hello World!");
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. }
    43. }

    3.效果

     

  • 相关阅读:
    Python 编程规范和软件开发目录规范的重要性
    网络安全(黑客)从零开始的自学指南(第二章)
    将Visual Studio Code配置成好用的Python IDE
    20230925 比赛总结
    Docker原生网络、自定义网络、Docker容器通信、跨主机容器网络
    基于QT5与opencascdae7.4的简易模型浏览器
    算法题练习——NC15 求二叉树的层序遍历、NC88 寻找第K大
    C++文件的操作
    【AD9361 数字接口CMOS &LVDS】A CMOS
    数据融合的并行计算
  • 原文地址:https://blog.csdn.net/weixin_38319647/article/details/139961256