给微软office文档添加水印

给微软office文档添加水印

水印的工具

  1. Apache POI:一个java的库,Apache POI - the Java API for Microsoft Documents

  2. Open XML SDK:C#库,GitHub - OfficeDev/Open-XML-SDK: Open XML SDK by Microsoft

  3. Free Spire.Office for Javahttps://www.e-iceblue.cn/Introduce/Free-Spire-Office-JAVA.html

img

以下主要使用Apache POI

EXCEL

想要给excel添加水印只能以图片的方式插入,有两种方法

一种是直接贴图,但这样会把文字内容遮住,并且影响文档编辑

img

import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
public class my{
    public static void main(String[] args)throws IOException{
        //读入xlsx
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\项目文件\\test.xlsx")));
        //读取图片
        InputStream is = new FileInputStream("C:\\Users\\DELL\\Desktop\\项目文件\\watermark.png");
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = excel.addPicture(bytes, excel.PICTURE_TYPE_PNG);
        is.close();

        for (int i = 0; i < excel.getNumberOfSheets(); i++) {
            XSSFSheet xssfSheet = excel.getSheetAt(i);
            XSSFDrawing drawing = xssfSheet.createDrawingPatriarch();
            for (int xCount=0;xCount<10;++xCount)
            {
                for (int yCount=0;yCount<10;++yCount)
                {
                    // 创建水印图片位置
                    int xIndexInteger = xCount * 5 + xCount * 5;
                    int yIndexInteger = yCount * 5 + yCount * 5;
                    XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, xIndexInteger,yIndexInteger, xIndexInteger + 5, yIndexInteger + 5);
                    XSSFPicture pict = drawing.createPicture(anchor, pictureIdx);
                    pict.resize();
                }
            }
        }
        //保存新文件
        String file = "C:\\Users\\DELL\\Desktop\\mark2test33.xlsx";
        OutputStream fileOut = new FileOutputStream(file);
        excel.write(fileOut);

    }
} 

另一种是设置成背景图,虽然不影响编辑,但这样不能调节背景图的样式(技术要求中的倾斜度,布局等)

只能在生成图片时进行倾斜等操作,然后再嵌入成背景

img

import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;

public class my {

    public static void main(String[] args) throws IOException {
        //读入xlsx
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\项目文件\\test.xlsx")));
        //读取图片
        InputStream is = new FileInputStream("C:\\Users\\DELL\\Desktop\\项目文件\\watermark.png");
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = excel.addPicture(bytes, excel.PICTURE_TYPE_PNG);
        is.close();

        //添加sheet到图片的关系
        for (int i = 0; i < excel.getNumberOfSheets(); i++) {
            XSSFSheet xssfSheet = excel.getSheetAt(i);
            String rID=xssfSheet.addRelation(null,XSSFRelation.IMAGES,excel.getAllPictures().get((pictureIdx))).getRelationship().getId();
            xssfSheet.getCTWorksheet().addNewPicture().setId(rID);
        }
        //保存新文件
        String file = "C:\\Users\\DELL\\Desktop\\mark2test22.xlsx";
        OutputStream fileOut = new FileOutputStream(file);       
        excel.write(fileOut);
    }
}

生成图片再嵌入

所以可以生成图片时进行倾斜等操作,然后再嵌入成背景
img

img

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class my {
    public static class WaterMark
    {
        public String text;
        public int width=400;
        public int height=400;
        public String color;
        public String font;
        public int fontSize=10;
        public double Xshear;
        public double Yshear;
        WaterMark(String text,String color,String font,double Xshear,double Yshear){
            this.text=text;
            this.color=color;
            this.font=font;
            this.Xshear=Xshear;
            this.Yshear=Yshear;
        }
        public BufferedImage CreaterWaterMark() {
            BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            Graphics2D g=image.createGraphics();
            //图片透明
//            image=g.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.TRANSLUCENT);
//            g.dispose();
//            g=image.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,width,height);
            g.setColor(new Color(Integer.parseInt(color,16)));
            g.setFont(new Font(font,Font.BOLD,fontSize));
            g.shear(Xshear,Yshear);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(text,20,100);
            g.dispose();
            return image;
        }
    }
    public static void main(String[] args) throws IOException {
        //读入xlsx
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\项目文件\\test.xlsx")));
        //读取图片
//        InputStream is = new FileInputStream("C:\\Users\\DELL\\Desktop\\项目文件\\watermark.png");
        WaterMark waterMark=new WaterMark("水印展示","00FFFF","宋体",0.1,-0.28);
        BufferedImage image=waterMark.CreaterWaterMark();
        ByteArrayOutputStream is=new ByteArrayOutputStream();
        ImageIO.write(image,"png",new FileOutputStream("C:\\Users\\DELL\\Desktop\\1.png"));
        ImageIO.write(image,"png",is);
        //byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = excel.addPicture(is.toByteArray(), excel.PICTURE_TYPE_PNG);
        is.close();

        //添加sheet到图片的关系
        for (int i = 0; i < excel.getNumberOfSheets(); i++) {
            XSSFSheet xssfSheet = excel.getSheetAt(i);
            String rID=xssfSheet.addRelation(null,XSSFRelation.IMAGES,excel.getAllPictures().get((pictureIdx))).getRelationship().getId();
            xssfSheet.getCTWorksheet().addNewPicture().setId(rID);
        }
        //保存新文件
        String file = "C:\\Users\\DELL\\Desktop\\mark2test22.xlsx";
        OutputStream fileOut = new FileOutputStream(file);
        excel.write(fileOut);
    }
}

老版本excel

老版本的excel似乎只能以插入图片的形式添加水印,不能加入背景
尝试Free Spire.Office for Java这个库,似乎也不能加入背景,并且库有某种冲突暂时不能解决
所以目前只能生成透明图片加入,不影响阅读,但影响编辑
img

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Picture;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class my {
    public static class WaterMark
    {
        public String text;
        public int width=400;
        public int height=400;
        public String color;
        public String font;
        public int fontSize=42;
        public double Xshear;
        public double Yshear;
        WaterMark(String text,String color,String font,double Xshear,double Yshear){
            this.text=text;
            this.color=color;
            this.font=font;
            this.Xshear=Xshear;
            this.Yshear=Yshear;
        }
        public BufferedImage CreaterWaterMark() {
            BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            Graphics2D g=image.createGraphics();
            //图片透明
            image=g.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.TRANSLUCENT);
            g=image.createGraphics();
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.1f));
            g.setColor(Color.WHITE);
            g.fillRect(0,0,width,height);
            g.setColor(new Color(Integer.parseInt(color,16)));
            g.setFont(new Font(font,Font.BOLD,fontSize));
            g.shear(Xshear,Yshear);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(text,20,100);
            g.dispose();
            return image;
        }
    }
    public static void main(String[] args) throws IOException {
        //读入xlsx
        HSSFWorkbook excel = new HSSFWorkbook(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\测试用.xls")));
        //读取图片
//        InputStream is = new FileInputStream("C:\\Users\\DELL\\Desktop\\项目文件\\watermark.png");
        WaterMark waterMark=new WaterMark("水印展示","000000","宋体",0.1,-0.28);
        BufferedImage image=waterMark.CreaterWaterMark();
        ByteArrayOutputStream is=new ByteArrayOutputStream();
        ImageIO.write(image,"png",new FileOutputStream("C:\\Users\\DELL\\Desktop\\watermark.png"));
        ImageIO.write(image,"png",is);
        //byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = excel.addPicture(is.toByteArray(), excel.PICTURE_TYPE_PNG);
        is.close();
        CreationHelper helper = excel.getCreationHelper();
        //添加sheet到图片的关系
        for (int i = 0; i < excel.getNumberOfSheets(); i++) {
            HSSFSheet hssfSheet = excel.getSheetAt(i);
            Drawing drawing =hssfSheet.createDrawingPatriarch();
            //add a picture shape
            ClientAnchor anchor = helper.createClientAnchor();

            anchor.setCol1(3);
            anchor.setRow1(2);
            anchor.setDx1(6);
            Picture pict = drawing.createPicture(anchor, pictureIdx);
            pict.resize();
        }
        //保存新文件
        String file = "C:\\Users\\DELL\\Desktop\\测试生成.xls";
        OutputStream fileOut = new FileOutputStream(file);
        excel.write(fileOut);
    }
}

WORD

插入图片

插入图片,可以设定图片插入的大小,位置,旋转角度
效果如图
img

import com.microsoft.schemas.vml.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class my {
    public static class WaterMark
    {
        public String text;
        public int width=400;
        public int height=400;
        public String color;
        public String font;
        public int fontSize=10;
        public double Xshear;
        public double Yshear;
        WaterMark(String text,String color,String font,double Xshear,double Yshear){
            this.text=text;
            this.color=color;
            this.font=font;
            this.Xshear=Xshear;
            this.Yshear=Yshear;
        }
        public BufferedImage CreaterWaterMark() {
            BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            Graphics2D g=image.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,width,height);
            g.setColor(new Color(Integer.parseInt(color,16)));
            g.setFont(new Font(font,Font.BOLD,fontSize));
            g.shear(Xshear,Yshear);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(text,20,100);
            g.dispose();
            return image;
        }
    }
    public static class Style{
        public String position="absolute";
        public String width="100";
        public String height="100";
        public String rotation="315";
        public String position_horizontal="center";
        public String position_horizontal_relative="margin";
        public String position_vertical="center";
        public String position_vertical_relative="margin";

        public Style(String width,String height,String rotation,String position_horizontal,String position_vertical){
            this.width=width;
            this.height=height;
            this.rotation=rotation;
            this.position_horizontal=position_horizontal;
            this.position_vertical=position_vertical;
        }

        public String GetString(){
            return "position:"+this.position+";width:"+this.width+";height:"+this.height+";rotation:"+this.rotation+";mso-position-horizontal:"+this.position_horizontal+";mso-position-vertical:"+this.position_vertical+";mso-position-horizontal-relative:"+this.position_horizontal_relative+";mso-position-vertical-relative:"+this.position_vertical_relative;
        }
    }
    public static void main(String[] args) throws IOException, InvalidFormatException {
        //读取文档
        XWPFDocument doc=new XWPFDocument(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\测试用.docx")));
        
        //读取图片
        FileInputStream is=new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\2.jpg"));
        
        //添加图片水印
        XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
        //加入图片,得到图片的一个ID
        String pictureID = header.addPictureData(is,XWPFDocument.PICTURE_TYPE_PNG);
        if ( header.getParagraphs().size()== 0) {
            header.createParagraph();
        }
        CTP ctp = header.getParagraphArray(0).getCTP();
        CTR ctr = ctp.addNewR();
        CTPicture pict =ctr.addNewPict();
        CTGroup group = CTGroup.Factory.newInstance();
        CTShape shape = group.addNewShape();
        shape.setId("WordPictureWatermark");
        Style style=new Style("200","300","110","center","center");
        shape.setStyle(style.GetString());
        CTImageData imageData=shape.addNewImagedata();
        imageData.setId2(pictureID);
        pict.set(group);

        //保存文件
        String file = "C:\\Users\\DELL\\Desktop\\测试生成.docx";
        OutputStream fileOut = new FileOutputStream(file);
        doc.write(fileOut);
    }
}

插入文字

效果如图,可以设定文字插入的大小,位置,旋转角度,颜色,字体,简单样式
img

import com.microsoft.schemas.vml.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STTrueFalse;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class my {
    public static class Style{
        public String position="absolute";
        public String width="100";
        public String height="100";
        public String rotation="315";
        public String position_horizontal="center";
        public String position_horizontal_relative="margin";
        public String position_vertical="center";
        public String position_vertical_relative="margin";

        public Style(String width,String height,String rotation,String position_horizontal,String position_vertical){
            this.width=width;
            this.height=height;
            this.rotation=rotation;
            this.position_horizontal=position_horizontal;
            this.position_vertical=position_vertical;
        }

        public String GetString(){
            return "position:"+this.position+";width:"+this.width+";height:"+this.height+";rotation:"+this.rotation+";mso-position-horizontal:"+this.position_horizontal+";mso-position-vertical:"+this.position_vertical+";mso-position-horizontal-relative:"+this.position_horizontal_relative+";mso-position-vertical-relative:"+this.position_vertical_relative;
        }
    }
    public static void main(String[] args) throws IOException, InvalidFormatException {
        //读取文档
        XWPFDocument doc=new XWPFDocument(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\测试用.docx")));

        //添加水印
        XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
        if ( header.getParagraphs().size()== 0) {
            header.createParagraph();
        }
        CTP ctp = header.getParagraphArray(0).getCTP();
        CTR ctr = ctp.addNewR();
        CTPicture pict =ctr.addNewPict();
        CTGroup group = CTGroup.Factory.newInstance();
        CTShape shape = group.addNewShape();
        shape.setId("PowerPlusWaterMarkObject");
        Style style=new Style("200","300","110","center","center");
        shape.setStyle(style.GetString());
        shape.setFillcolor("#00FF00");
        shape.setSpid("_x0000_s2051");
        shape.setType("#_x0000_t136");
        shape.setStroked(STTrueFalse.FALSE); // 字体设置为实心
        CTTextPath shapeTextPath = shape.addNewTextpath();
        shapeTextPath.setStyle("font-family:" + "\"宋体\"" + ";font-size:" + "1pt"); // 设置文本字体与大小
        shapeTextPath.setString("水印");
        pict.set(group);

        //保存文件
        String file = "C:\\Users\\DELL\\Desktop\\测试生成.docx";
        OutputStream fileOut = new FileOutputStream(file);
        doc.write(fileOut);
    }
}

老版本word

Apache POI同样似乎无法完成要求
使用Free Spire.Office for Java这个库,它提供了接口
可以方便的添加图片和水印,但是水印的位置和旋转角度无法设置,只能有以下效果
img

import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException{
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\DELL\\Desktop\\测试用.doc");
        for(int i=0;i<doc.getSections().getCount();++i){
            Section section=doc.getSections().get(i);
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.setText("内部使用");
            txtWatermark.setFontName("宋体");
            txtWatermark.setFontSize(40);
            txtWatermark.setColor(Color.red);
            txtWatermark.setLayout(WatermarkLayout.Diagonal);
            //txtWatermark.setLayout(WatermarkLayout.Horizontal);
            section.getDocument().setWatermark(txtWatermark);
        }

        doc.saveToFile("C:\\Users\\DELL\\Desktop\\测试生成.doc",FileFormat.Doc );
    }
}

可以插入图片
img

import com.spire.doc.*;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException{
        //读取文档
        Document doc=new Document(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\测试用.doc")));
        //读取图片
        PictureWatermark image=new PictureWatermark("C:\\Users\\DELL\\Desktop\\2.jpg",false);
        //添加水印
        image.setScaling(100);//设置大小
        doc.setWatermark(image);
        //保存文件
        doc.saveToFile("C:\\Users\\DELL\\Desktop\\测试生成.doc", FileFormat.Doc);
    }
}

对于文字,如果想要支持旋转等操作,可以选用插入艺术字
img

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.WordArt;

import java.awt.*;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException{
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\DELL\\Desktop\\测试用.doc");
        for(int i=0;i<doc.getSections().getCount();++i){
            Section section=doc.getSections().get(i);
            //定义水印文字的纵向坐标位置
//        float y = (float) (section1.getpagesetup().getpagesize().getheight()/3);
            //添加文字水印1
            HeaderFooter header1 = section.getHeadersFooters().getHeader();//获取页眉
            header1.getParagraphs().clear();//删除原有页眉格式的段落
            Paragraph para1= header1.addParagraph();//重新添加段落
            //添加艺术字并设置大小
            ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
            shape.setWidth(400);
            shape.setHeight(200);
            shape.setRotation(315);
            shape.setStrokeColor(Color.blue);
            shape.setFillColor(Color.blue);
            WordArt art=shape.getWordArt();
            art.setText("水印展示");
            art.setFontFamily("宋体");
            art.setSize(40);
            shape.setVerticalAlignment(ShapeVerticalAlignment.Center);
            shape.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
            para1.getChildObjects().add(shape);
        }
        doc.saveToFile("C:\\Users\\DELL\\Desktop\\测试生成.doc", FileFormat.Doc);
    }
}

PowerPoint

与EXCEL相同,只能以图片的形式插入,但是这里PowerPoint有一个母版的概念,将图片插入到母版里似乎不能删除,更改,可以达到水印的效果
img

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        //读取文档
        XMLSlideShow ppt=new XMLSlideShow(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\测试用.pptx")));

        //读取图片
        FileInputStream is=new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\2.jpg"));
        
        //添加水印
        XSLFPictureData pd=ppt.addPicture(is, PictureData.PictureType.PNG);
        Dimension pgsize=ppt.getPageSize();//获取ppt页面大小
        for(XSLFSlide slide:ppt.getSlides())
        {
            XSLFPictureShape pic = slide.getSlideMaster().createPicture(pd);
            pic.setAnchor(new Rectangle((pgsize.width-100)/2,(pgsize.height-200)/2,100,200));
            pic.setRotation(60);
        }
        
        //保存文件
        String file = "C:\\Users\\DELL\\Desktop\\测试生成.pptx";
        OutputStream fileOut = new FileOutputStream(file);
        ppt.write(fileOut);
    }
}

添加文字
支持文字颜色,大小,字体,旋转的改变
img

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.sl.usermodel.Insets2D;
import org.apache.poi.xslf.usermodel.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        //读取文档
        XMLSlideShow ppt=new XMLSlideShow(new FileInputStream(new File("C:\\Users\\DELL\\Desktop\\测试用.pptx")));

        //添加水印
        Dimension pgsize=ppt.getPageSize();//获取ppt页面大小
        for(XSLFSlide slide:ppt.getSlides())
        {
            XSLFTextBox textBox = slide.getSlideMaster().createTextBox();
            textBox.setHorizontalCentered(true);
            textBox.setInsets(new Insets2D((400-60*2)/2,0,0,0));
            textBox.setAnchor(new Rectangle((pgsize.width-400)/2,(pgsize.height-400)/2,400,400));
            textBox.setRotation(60);
            XSLFTextParagraph p=textBox.addNewTextParagraph();
            XSLFTextRun r=p.addNewTextRun();
            r.setText("水印");
            r.setFontFamily("楷体");
            r.setFontColor(Color.BLUE);
            r.setFontSize(60.);
        }

        //保存文件
        String file = "C:\\Users\\DELL\\Desktop\\测试生成.pptx";
        OutputStream fileOut = new FileOutputStream(file);
        ppt.write(fileOut);
    }
}

老版PowerPoint

Apache POI可以实现,包括大小,位置,旋转
插入图片
img

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.sl.usermodel.PictureData;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException{
        HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl("C:\\Users\\DELL\\Desktop\\测试用.ppt"));

        HSLFPictureData pd = ppt.addPicture(new File("C:\\Users\\DELL\\Desktop\\2.jpg"), PictureData.PictureType.JPEG);

        for(HSLFSlideMaster master : ppt.getSlideMasters())
        {
            HSLFPictureShape pictNew =master.createPicture(pd);
            pictNew.setAnchor(new java.awt.Rectangle(100, 100, 300, 200));
            pictNew.setRotation(315);
            master.addShape(pictNew);
        }

        FileOutputStream out = new FileOutputStream("C:\\Users\\DELL\\Desktop\\测试生成.ppt");
        ppt.write(out);
        out.close();
    }
}

插入文字
可以改变大小,位置,颜色,字体,旋转
img

import org.apache.poi.hslf.usermodel.*;
import java.awt.*;
import java.io.*;

public class my {
    public static void main(String[] args) throws IOException{
        HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl("C:\\Users\\DELL\\Desktop\\测试用.ppt"));

        for(HSLFSlideMaster master : ppt.getSlideMasters())
        {
            HSLFTextBox txt=master.createTextBox();
            txt.setText("水印展示");
            txt.setAnchor(new java.awt.Rectangle(100, 100, 300, 200));
            //txt.setRotation(90);
            txt.setTextRotation(200.);
            HSLFTextParagraph tp=txt.getTextParagraphs().get(0);
            HSLFTextRun rt=tp.getTextRuns().get(0);
            rt.setFontColor(Color.blue);
            rt.setFontFamily("华文行楷");
            rt.setFontSize(44.);
        }

        FileOutputStream out = new FileOutputStream("C:\\Users\\DELL\\Desktop\\测试生成.ppt");
        ppt.write(out);
        out.close();
    }
}

参考

https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

https://www.jianshu.com/p/911d504193cb?u_atoken=f7eda06b-8c47-48bd-b20d-78819784e838&u_asession=01ummDbdI5ZhVuHtco2Kd04rNCk1quh2aoyIYaQ4ZYFzt_4nyx-6nw6FOs4usXbSVX0KNBwm7Lovlpxjd_P_q4JsKWYrT3W_NKPr8w6oU7K-Dduym2jZxTP5bEXXyXZaEslvTX-jMTLEIhdGFg3rxgWBkFo3NEHBv0PZUm6pbxQU&u_asig=05469NN0m4fsQ_SGn4moWfU4ovWlkzGZg9V00LpxgoNuK_Wg_dwiwIDR-mEox1O6jbRPX82t8ziU_pbLxbG_eiMTaUM4qF9Guii5AT6-NRWV8XO_cPDK4tVYXpxJn4uS4FTlYbOTh3OlfFgucbiekiL3T07AAhdTYlElq9q33oyRv9JS7q8ZD7Xtz2Ly-b0kmuyAKRFSVJkkdwVUnyHAIJzWmDAC4hDrvIG14ZxSLEBpHd0Ou03bz9VxfcI8PcSTku6FPw117USKdEPc8n7HkzU-3h9VXwMyh6PgyDIVSG1W9Zl2S9zvPM4ZeUYgtHa-zKmAxB0TMadCqhaHIZUDczZ5WZmH6xhh-aV-3VTCMa9ktFcuorl3fuDZw-F3P55rkmWspDxyAEEo4kbsryBKb9Q&u_aref=tWpHGvnd%2BBIXfMdKtkBVrETkhk4%3D

https://lantingshuxu.github.io/java/java-%E4%BD%BF%E7%94%A8POI%E5%AE%9E%E7%8E%B0%E4%B8%BAword%E6%96%87%E6%A1%A3-docx-%E6%B7%BB%E5%8A%A0%E6%B0%B4%E5%8D%B0/

https://blog.csdn.net/qq_42835445/article/details/118311354

https://www.10qianwan.com/articledetail/850224.html

https://www.jb51.net/article/205710.htm

https://blog.csdn.net/m0_66876551/article/details/124307320

https://ask.csdn.net/questions/7496162

posted @ 2022-08-05 22:25  启林O_o  阅读(1119)  评论(0编辑  收藏  举报