java poi 3.10 word 加水印 代码
时间: 2023-09-22 18:12:12 浏览: 173
以下是Java POI 3.10 Word加水印代码示例:
```
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.*;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.xwpf.model.*;
import org.apache.xmlbeans.*;
public class WordWatermark {
public static void main(String[] args) throws Exception {
// 创建一个新的Word文档
XWPFDocument doc = new XWPFDocument();
// 添加文档内容
XWPFParagraph para = doc.createParagraph();
XWPFRun run = para.createRun();
run.setText("Hello World!");
// 添加水印
String watermarkText = "Confidential";
addWatermark(doc, watermarkText);
// 保存Word文档
FileOutputStream out = new FileOutputStream("test.docx");
doc.write(out);
out.close();
doc.close();
}
// 添加水印
public static void addWatermark(XWPFDocument doc, String watermarkText) throws Exception {
// 创建水印段落
XWPFParagraph watermarkPara = doc.createParagraph();
watermarkPara.setAlignment(ParagraphAlignment.CENTER);
watermarkPara.setVerticalAlignment(TextAlignment.CENTER);
// 创建水印文字
XWPFRun watermarkRun = watermarkPara.createRun();
watermarkRun.setText(watermarkText);
watermarkRun.setColor("C0C0C0");
watermarkRun.setFontSize(100);
// 获取页面大小
CTPageSz pageSize = doc.getDocument().getBody().getSectPr().getPgSz();
// 计算水印位置
int posX = (pageSize.getW().intValue() - watermarkRun.getText().length() * 50) / 2;
int posY = (pageSize.getH().intValue() - 100) / 2;
// 创建水印图片
byte[] rgb = new byte[] { (byte)192, (byte)192, (byte)192 };
XWPFRun run = doc.createParagraph().createRun();
String blipId = run.getCTR().addNewPict().addNewBlipFill().addNewBlip().getEmbed();
File imageFile = createImage(rgb);
FileInputStream fis = new FileInputStream(imageFile);
int picInd = doc.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG);
fis.close();
String picXml = getPicXml(blipId, picInd, posX, posY, 100, 100);
CTInline inline = CTInline.Factory.parse(picXml);
run.getCTR().set(inline);
// 设置水印透明度
CTPicture ctPicture = run.getCTR().getPictArray(0);
CTBlipFillProperties ctBlipFillProperties = ctPicture.getBlipFill();
CTAlphaTransform ctAlphaTransform = ctBlipFillProperties.addNewAlphaBiLevel();
ctAlphaTransform.setVal(80000);
}
// 创建水印图片
public static File createImage(byte[] rgb) throws Exception {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(new Color(rgb[0], rgb[1], rgb[2]));
graphics.fillRect(0, 0, 100, 100);
File file = File.createTempFile("watermark", ".png");
ImageIO.write(image, "png", file);
return file;
}
// 获取水印图片XML
public static String getPicXml(String blipId, int picInd, int posX, int posY, int width, int height) {
String picXml = "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
"<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
"<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
"<pic:nvPicPr>" +
"<pic:cNvPr id=\""+picInd+"\" name=\"Generated\"/>" +
"<pic:cNvPicPr/>" +
"</pic:nvPicPr>" +
"<pic:blipFill>" +
"<a:blip r:embed=\""+blipId+"\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
"<a:stretch>" +
"<a:fillRect/>" +
"</a:stretch>" +
"</pic:blipFill>" +
"<pic:spPr>" +
"<a:xfrm>" +
"<a:off x=\""+posX+"\" y=\""+posY+"\"/>" +
"<a:ext cx=\""+width+"\" cy=\""+height+"\"/>" +
"</a:xfrm>" +
"<a:prstGeom prst=\"rect\">" +
"<a:avLst/>" +
"</a:prstGeom>" +
"</pic:spPr>" +
"</pic:pic>" +
"</a:graphicData>" +
"</a:graphic>";
return picXml;
}
}
```
在示例中,我们创建了一个新的Word文档,并添加了一些文本内容。然后,我们调用了addWatermark方法来添加水印。在该方法中,我们首先创建了一个水印段落,然后创建了一个包含水印文字的运行。接下来,我们使用CTPageSz类获取页面大小,并计算出水印的位置。然后,我们创建了一个水印图片,并将其添加到Word文档中。最后,我们使用CTAlphaTransform类设置水印的透明度。
需要注意的是,水印的位置和大小可能需要根据不同的Word文档进行调整。
阅读全文