You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
5.2 KiB
138 lines
5.2 KiB
/* |
|
* Copyright (C), 2018-2022 |
|
* Project: starter |
|
* FileName: WordHandler |
|
* Author: Louis |
|
* Date: 2022/2/23 21:47 |
|
*/ |
|
package com.fr.plugin.ifhd.utils; |
|
|
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.third.org.apache.commons.io.FileUtils; |
|
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; |
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
import org.apache.poi.xwpf.usermodel.XWPFHeader; |
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
|
import org.apache.xmlbeans.XmlObject; |
|
|
|
import javax.xml.namespace.QName; |
|
import java.io.*; |
|
import java.util.regex.Pattern; |
|
|
|
/** |
|
* <Function Description><br> |
|
* <WordHandler> |
|
* |
|
* @author Louis |
|
* @since 1.0.0 |
|
*/ |
|
public class WordHandler { |
|
public WordHandler() { |
|
} |
|
|
|
public static void main(String[] args) { |
|
try { |
|
String inputSrc = "E:\\GettingStarted.doc"; |
|
String outputSrc = "E:\\test_temp.docx"; |
|
File file = new File(inputSrc); |
|
byte[] bytes = FileUtils.readFileToByteArray(file); |
|
addWordWaterMark(bytes, outputSrc, "内部资料 严禁外传"); |
|
|
|
// String inputSrc2 = "E:\\test.doc"; |
|
// String outputSrc2 = "E:\\test_temp.doc"; |
|
// File file2 = new File(inputSrc2); |
|
// byte[] bytes2 = FileUtils.readFileToByteArray(file2); |
|
// addWordWaterMark2003(bytes2, outputSrc2, "内部资料 严禁外传"); |
|
|
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
} |
|
|
|
/** |
|
* word文件添加文字水印 |
|
* |
|
* @param fileData 原始文件字节数组 |
|
* @param outputSrc 文件输出路径 |
|
* @param waterMarkName 水印内容 |
|
*/ |
|
// private static void addWordWaterMark2003(byte[] fileData, String outputSrc, String waterMarkName) throws Exception { |
|
// InputStream input = new ByteArrayInputStream(fileData); |
|
// HWPFDocument doc = new HWPFDocument(input); |
|
// XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy(); |
|
// //添加文字水印 |
|
// headerFooterPolicy.createWatermark(waterMarkName); |
|
// HWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT); |
|
// XWPFParagraph paragraph = header.getParagraphArray(0); |
|
// paragraph.getCTP().newCursor(); |
|
// XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(new QName("urn:schemas-microsoft-com:vml", "shape")); |
|
// if (xmlobjects.length > 0) { |
|
// com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape) xmlobjects[0]; |
|
// //设置水印颜色 |
|
// ctshape.setFillcolor("#828282"); |
|
// //修改水印样式 |
|
// ctshape.setStyle(getWaterMarkStyle(ctshape.getStyle(), 23) + ";rotation:315"); |
|
// } |
|
// ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
// try { |
|
// doc.write(out); |
|
// } finally { |
|
// out.close(); |
|
// } |
|
// out.writeTo(new BufferedOutputStream(new FileOutputStream(outputSrc))); |
|
// } |
|
|
|
/** |
|
* word文件添加文字水印 |
|
* |
|
* @param fileData 原始文件字节数组 |
|
* @param outputSrc 文件输出路径 |
|
* @param waterMarkName 水印内容 |
|
*/ |
|
private static void addWordWaterMark(byte[] fileData, String outputSrc, String waterMarkName) throws Exception { |
|
InputStream input = new ByteArrayInputStream(fileData); |
|
XWPFDocument doc = new XWPFDocument(input); |
|
XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy(); |
|
//添加文字水印 |
|
headerFooterPolicy.createWatermark(waterMarkName); |
|
XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT); |
|
XWPFParagraph paragraph = header.getParagraphArray(0); |
|
paragraph.getCTP().newCursor(); |
|
XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(new QName("urn:schemas-microsoft-com:vml", "shape")); |
|
if (xmlobjects.length > 0) { |
|
com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape) xmlobjects[0]; |
|
//设置水印颜色 |
|
ctshape.setFillcolor("#828282"); |
|
//修改水印样式 |
|
ctshape.setStyle(getWaterMarkStyle(ctshape.getStyle(), 23) + ";rotation:315"); |
|
} |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
try { |
|
doc.write(out); |
|
} finally { |
|
out.close(); |
|
} |
|
out.writeTo(new BufferedOutputStream(new FileOutputStream(outputSrc))); |
|
} |
|
|
|
/** |
|
* 设置水印格式 |
|
* word |
|
* |
|
* @param styleStr |
|
* @param height |
|
* @return |
|
*/ |
|
public static String getWaterMarkStyle(String styleStr, double height) { |
|
Pattern p = Pattern.compile(";"); |
|
String[] strs = p.split(styleStr); |
|
for (String str : strs) { |
|
if (str.startsWith("height:")) { |
|
String heightStr = "height:" + height + "pt"; |
|
styleStr = styleStr.replace(str, heightStr); |
|
break; |
|
} |
|
} |
|
return styleStr; |
|
} |
|
} |