java通过spire.doc实现word增加水印功能

代码示例展示了如何利用Spire.OfficeFree.jar包中的Spire.Doc类库,动态为Word文档添加水印,包括设置文档只读密码,添加艺术字水印,调整位置和颜色,并保存为新的Docx文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的:

    动态给word文档添加水印,生成相应格式。基于Spire为免费版,以及Spire.Doc的运行无需安装Microsoft Word。而且,它可以将Microsoft Word文档创建功能集成到开发者的任何.NET应用程序。

涉及jar包:

        spire-office-free.jar

直接上代码:

// 以模板形式打开word文档
String filename = “word水印模板.docx”;
File  createTempFile = createTempFile(filename);//创建临时路径
String wordPath = createTempFile.getPath();
String wordPath = createTempFile.getPath();
String fileName = createTempFile.getName();
String oldFilePath = wordPath.substring(0, wordPath.lastIndexOf(fileName));
String name = fileName .substring(0,fileName.lastIndexOf("."));
String savePath = oldFilePath + name + suffix_name + ".docx";
File toFile = new File(savePath);
if (toFile.exists()) {
	toFile.delete();
}
Document document = new Document(wordPath); // 源文件路径
Section section1 = document.getSections().get(0);
//设置文档只读密码
if(PubEnumsConstant.IS_READ_0.equals(is_read)){		        	document.protect(ProtectionType.Allow_Only_Reading,
RandomStringUtils.randomAlphanumeric(10));
}
// 添加艺术字并设置大小
ShapeObject shape = new ShapeObject(document, ShapeType.Text_Plain_Text);// 该字体为中空字体,
for (int n = 0; n < document.getSections().getCount(); n++) {
	section = document.getSections().get(n);
// 获取section的页眉
HeaderFooter header = section.getHeadersFooters().getHeader();
Paragraph paragraph;
if (header.getParagraphs().getCount() > 0) {
// 如果页眉有段落,取它第一个段落
paragraph = header.getParagraphs().get(0);
} else {
// 否则新增加一个段落到页眉
paragraph = header.addParagraph();
}
for (int i = 0; i < 4; i++) {// 列
for (int j = 0; j < 5; j++) {// 行
	// 复制艺术字并设置多行多列位置
	shape = (ShapeObject) shape.deepClone();
	shape.setWidth(160);// 字体宽度
	shape.setHeight(10;// 字体高度
	shape.setRotation(330);// 旋转
	shape.getWordArt().setFontFamily("宋体");// 水印字体
	shape.getWordArt().setText(“中银金融租赁有限公司”);// 水印
	shape.setFillColor(new Color(128, 128, 128));// 中心填充颜色
	shape.setStrokeColor(new Color(204, 204, 204));// 边缘填充颜色
	shape.setStrokeWeight(0.5);// 字体宽度(<=1)
	shape.setLineStyle(ShapeLineStyle.Single);// 格式
//设置循环次数,几行几列
shape.setVerticalPosition(50 + 150 * i);//50代表行的起始位置
shape.setHorizontalPosition(20 + 160 * j);//20代表列的起始位置
	paragraph.getChildObjects().add(shape);
	}
}

	}
	// 另存为docx文件
	document.saveToFile(savePath, FileFormat.Docx);//savePath不可与原文件为同一个路径。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
/**
	 * Description: 创建要生成的文件<br>
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 * @author: jichen<br>
	 * @Date:2021-6-22
	 */
	private File createTempFile(String filename) throws IOException {
		File file = new File(TEM_DIR + filename);
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file);
			this.document.write(out);
		} finally {
			IOUtils.closeQuietly(out);
		}
		return file;
	}

注:文档源路径与加水印文档不可为同一路径,否则不生效。可通过设置各自属性实现可配置话加水印功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值