基于Aspose依赖添加自定义文本水印——Word、Pdf、Cell

基于Aspose依赖添加自定义文本水印------Word、Pdf、Cell

  • 所需依赖
  • Word水印
  • [Pdf水印------( 注意 pdf 存在找不到字体的问题)](#Pdf水印——( 注意 pdf 存在找不到字体的问题))
  • Excel水印

所需依赖

复制代码
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-pdf</artifactId>
        <version>22.11</version>
    </dependency>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cells</artifactId>
        <version>22.12</version>
    </dependency>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-slides</artifactId>
        <version>22.11</version>
        <classifier>jdk16</classifier>
    </dependency>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words</artifactId>
        <version>22.12</version>
        <classifier>jdk17</classifier>
    </dependency>

Word水印

java 复制代码
// 先获取文件流 (这边先随意读取文件流)
InputStream in =  new FileInputStream("E:/demo/demo.docx");
// 注意document包,每种类型都有一个document
com.aspose.words.Document doc = new com.aspose.words.Document(in);
TextWatermarkOptions textWatermarkOptions = new TextWatermarkOptions();
textWatermarkOptions.setFontFamily("宋体");
textWatermarkOptions.setFontSize(24f);
textWatermarkOptions.setColor(java.awt.Color.RED);
textWatermarkOptions.setLayout(WatermarkLayout.DIAGONAL);
textWatermarkOptions.isSemitrasparent(false);
doc.getWatermark().setText("水印内容",textWatermarkOptions);
ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.save(out, com.aspose.words.SaveFormat.DOCX);
out.close();
// 输出流用于下载
return out.toByteArray();

Pdf水印------( 注意 pdf 存在找不到字体的问题)

java 复制代码
// 先获取文件流 (这边先随意读取文件流)
InputStream in =  new FileInputStream("E:/demo/demo.pdf");
// 注意document包,每种类型都有一个document
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(in);
FormattedText formattedText = new FormattedText("水印内容", java.awt.Color.RED, FontStyle.HelveticaBold, EncodingType.Identity_h, true, 24f);
for (Page page : doc.getPages()) {
   WatermarkArtifact artifact = new WatermarkArtifact();
   artifact.setText(formattedText);
   artifact.getTextState().setFont(FontRepository.findFont(getFontName("宋体"),true));
   artifact.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
   artifact.setArtifactVerticalAlignment(VerticalAlignment.Center);
   artifact.setRotation(45); // 设置旋转角度
   artifact.setOpacity(0.9); // 设置透明度
   artifact.setBackground (true);
   page.getArtifacts().add(artifact);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.save(out, com.aspose.pdf.SaveFormat.Pdf);
out.close();
return out.toByteArray();

// 注意 pdf 存在找不到字体的问题 因为字体库是以文件名查找 而不是字体名
private static String getFontName(String font){
    switch (font.trim().toLowerCase()){
        case "宋体":
            return "simsun";
        case "微软雅黑":
            return "simhei";
        default:
            return font;
    }
}

Excel水印

java 复制代码
// 先获取文件流 (这边先随意读取文件流)
InputStream in =  new FileInputStream("E:/demo/demo.xls");
// 注意document包,每种类型都有一个document
Workbook workbook = new Workbook(in);
for(Object worksheet: workbook.getWorksheets()){
	Worksheet sheet = (Worksheet) worksheet;
	int coloums = sheet.getCells().getColumns().getCount();
	int rows = sheet.getCells().getRows().getCount();
	com.aspose.cells.Shape wordart = sheet.getShapes().addTextEffect(MsoPresetTextEffect.TEXT_EFFECT_1,"水印内容",
                        "宋体",24f,true,false,
                        rows,rows/2,coloums/2,0,100,800);
	MsoFillFormat wordArtFormat = wordart.getFillFormat();
	wordArtFormat.setTransparency(0.9);
	int r= java.awt.Color.getRed();
	int g= java.awt.Color.getGreen();
	int b= java.awt.Color.getBlue();
	wordArtFormat.setForeColor(com.aspose.cells.Color.fromArgb(r,g,b));
	wordart.setHasLine(false);
	wordart.setLocked(true);
	wordart.setLockedProperty(ShapeLockType.SELECTION, true);
	wordart.setLockedProperty(ShapeLockType.SHAPE_TYPE, true);
	wordart.setLockedProperty(ShapeLockType.MOVE, true);
	wordart.setLockedProperty(ShapeLockType.RESIZE, true);
	wordart.setLockedProperty(ShapeLockType.TEXT, true);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.save(out, com.aspose.cells.SaveFormat.XLSX);
out.close();
return out.toByteArray();
相关推荐
风象南32 分钟前
Spring Boot 实现文件秒传功能
java·spring boot·后端
橘猫云计算机设计33 分钟前
基于django优秀少儿图书推荐网(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·小程序·django·毕业设计
黑猫Teng37 分钟前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
背太阳的牧羊人43 分钟前
使用 PyMuPDF(fitz)库打开 PDF 文件,并且是从内存中的字节流(BytesIO)读取 PDF 内容
数据库·pdf·文件处理·pymupdf·fitz
星河浪人43 分钟前
Spring Boot启动流程及源码实现深度解析
java·spring boot·后端
佩奇的技术笔记44 分钟前
中级:Maven面试题精讲
java·面试·maven
灯火不休ᝰ1 小时前
前端处理pdf文件流,展示pdf
前端·pdf
Lizhihao_1 小时前
JAVA-堆 和 堆排序
java·开发语言
极客先躯1 小时前
高级java每日一道面试题-2025年3月21日-微服务篇[Nacos篇]-什么是Nacos?
java·开发语言·微服务
工业互联网专业1 小时前
基于springboot+vue的动漫交流与推荐平台
java·vue.js·spring boot·毕业设计·源码·课程设计·动漫交流与推荐平台