spire.doc.free word 添加水印

1:引入jar 

<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
 
<repositories>
<!-- 无法引入spire.doc.free 时是因为maven的配置文件settings.xml配了阿里云镜像加速,这时我们需暂时将settings.xml中的镜像给剪切掉-->
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
 
2:代码
 
import com.FileWatermarkUtil;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
 
/**
* 下载模板
* @param response
* @throws IOException
*/
@GetMapping("/download/template1")
public void downloadTemplate1(HttpServletResponse response) throws IOException {
String filename ="123.doc";
Resource resource1 = new ClassPathResource("tempExcel" +System.getProperty("file.separator")+filename);
InputStream fileinputStream = resource1.getInputStream();
Document document = new Document(fileinputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 添加水印
FileWatermarkUtil.wordzpdf(document,"测试数据",300,100,50,-35);
FileWatermarkUtil.wordzpdf(document,"版权所有",150,50,100,100);
// 将数据写入到 outputStream 流中
if(filename.indexOf(".docx")!=-1||filename.indexOf(".DOCX")!=-1){
document.saveToFile(outputStream, FileFormat.Docx);
}
if(filename.indexOf(".doc")!=-1||filename.indexOf(".DOC")!=-1){
document.saveToFile(outputStream, FileFormat.Doc);
}
//设置响应头信息
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream; charset=UTF-8");
StringBuffer contentDisposition = new StringBuffer("attachment; filename="+ URLEncoder.encode(filename,"UTF-8"));
response.setHeader("Content-disposition", contentDisposition.toString());
OutputStream out = response.getOutputStream();
outputStream.writeTo(out);
fileinputStream.close();
out.close();
outputStream.close();
document.close();
}
 
工具类
import com.spire.doc.Document;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

/**
* 添加水印工具类
*/
public class FileWatermarkUtil {

//word 添加水印
//document  对象
//text  水印内容
//width 文字宽度
//height  文字长度
//ksheight  离边框开始高度
//kswidth  离边框开始宽度
public static void wordzpdf(Document document,String text ,Integer width,Integer height,Integer ksheight,Integer kswidth){
//添加艺术字形状并设置其大小
ShapeObject shape = new ShapeObject(document, ShapeType.Text_Plain_Text);
shape.setWidth(width);
shape.setHeight(height);
//设置图案文本,位置和样式
shape.setVerticalPosition(40);
shape.setHorizontalPosition(20);
shape.setRotation(315);
shape.getWordArt().setText(text);
shape.setFillColor(new Color(201, 196, 196, 201));
shape.setLineStyle(ShapeLineStyle.Single);
shape.setStrokeColor(new Color(201, 196, 196, 201));
shape.setStrokeWeight(1);
Section section;
HeaderFooter header;
for (int n = 0; n < document.getSections().getCount(); n++) {
section = document.getSections().get(n);
//获取该节的页眉
header = section.getHeadersFooters().getHeader();
Paragraph paragraph;
for (int i = 0; i < 2; i++) {
//添加段落到页眉中
paragraph = header.addParagraph();
for (int j = 0; j < 2; j++) {
//复制艺术字形状并其添加到多个位置
shape = (ShapeObject) shape.deepClone();
shape.setVerticalPosition(ksheight + 370 * i);//两行水印之间 行于行之间高度
shape.setHorizontalPosition(kswidth + 260 * j);//一行水印中水印之间的宽度
paragraph.getChildObjects().add(shape);
}
}
}
}
}
 

 

 

 
posted @ 2024-06-28 11:22  雪儿蛇王  阅读(5)  评论(0编辑  收藏  举报