赞
踩
Spire.Office for Java 是一套企业级的涵盖 E-iceblue 所有 Java组件的集合,它包括以下产品的最新版本:Spire.Doc for Java, Spire.XLS for Java,Spire.Presentation for Java, Spire.PDF for Java 和 Spire.Barcode for Java。
使用Spire.Office for Java,开发人员可以创建多种多样的用于处理办公文档的Java应用程序。对文档的处理包括打开、创建、修改、转换、打印 MS Word、Excel、PowerPoint ®、PDF 和一维、二维条码。
作为一个独立的办公文档组件,Spire.Office 的运行环境无需安装 Microsoft Office、Adobe Acrobat、以及其他第三方软件。基于安全性、稳定性、可扩展性、效率及价格方面的考虑,Spire.Office 已经成为微软办公套件最有力的替代品。
Spire.Office for Java 支持 32 位和 64 位操作系统,支持 Windows 系统、Linux 系统、Unix 系统和 Mac OS 系统。官网地址:https://www.e-iceblue.cn/Introduce/Spire-Office-JAVA.html
- package com.feng.util;
-
- import com.feng.bean.ResultCode;
- import com.feng.error.MyException;
- import com.spire.doc.*;
- import com.spire.doc.FileFormat;
- import com.spire.doc.documents.WatermarkLayout;
-
- import java.awt.*;
- import java.awt.geom.Rectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
-
- import com.spire.presentation.IAutoShape;
- import com.spire.presentation.PortionEx;
- import com.spire.presentation.Presentation;
- import com.spire.presentation.ShapeType;
- import com.spire.presentation.drawing.FillFormatType;
- import com.spire.xls.ExcelVersion;
- import com.spire.xls.ViewMode;
- import com.spire.xls.Workbook;
- import com.spire.xls.Worksheet;
- import lombok.extern.slf4j.Slf4j;
-
-
- import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
-
- /**
- * Spire给office添加水印
- * @Author: feng
- * @Date: 2020/6/28 2:35 下午
- */
- @Slf4j
- public class SpireWaterMarkUtils {
-
- public static void main(String[] args) throws Exception {
- //word加水印
- setWordWaterMark("d:/tmp/tmp/2.doc", "d:/tmp/tmp/3.doc", "张三|159****1234");
- setWordWaterMark("d:/tmp/tmp/2.docx", "d:/tmp/tmp/3.docx", "张三|159****1234");
- //xls加水印
- setExcelWaterMark("d:/tmp/tmp/2.xls", "d:/tmp/tmp/3.xls", "张三|159****1234");
- setExcelWaterMark("d:/tmp/tmp/2.xlsx", "d:/tmp/tmp/3.xlsx", "张三|159****1234");
- //ppt加水印
- setPPTWaterMark("d:/tmp/tmp/2.ppt", "d:/tmp/tmp/3.ppt", "张三|159****1234");
- setPPTWaterMark("d:/tmp/tmp/2.pptx", "d:/tmp/tmp/3.pptx", "张三|159****1234");
- setPPTWaterMark("d:/tmp/tmp/22.ppt", "d:/tmp/tmp/33.ppt", "张三|159****1234");
- }
-
-
- /**
- * word文字水印 (doc,docx)
- *
- * @param inputPath
- * @param outPath
- * @param markStr
- */
- public static void setWordWaterMark(String inputPath, String outPath, String markStr) {
- Document document = new Document();
- document.loadFromFile(inputPath);
- TextWatermark txtWatermark = new TextWatermark();
- txtWatermark.setText(markStr);
- txtWatermark.setFontSize(40);
- txtWatermark.setColor(Color.GRAY);
- txtWatermark.setLayout(WatermarkLayout.Diagonal);
- document.getSections().get(0).getDocument().setWatermark(txtWatermark);
- document.saveToFile(outPath, FileFormat.Auto);
- }
-
- /**
- * excel设置水印
- * Excel 水印在正常模式下不可见,仅在页面布局模式或打印预览模式可见。
- *
- * @param inputPath
- * @param outPath
- * @param markStr
- */
- public static void setExcelWaterMark(String inputPath, String outPath, String markStr) {
- //加载示例文档
- Workbook workbook = new Workbook();
- workbook.loadFromFile(inputPath);
- //设置文本和字体大小
- Font font = new Font("宋体", Font.PLAIN, 40);
- for (Object object : (Iterable) workbook.getWorksheets()) {
- Worksheet sheet = (Worksheet) object;
- //调用DrawText() 方法插入图片
- BufferedImage imgWtrmrk = drawText(markStr, font, Color.GRAY, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());
-
- //将图片设置为页眉
- sheet.getPageSetup().setLeftHeaderImage(imgWtrmrk);
- sheet.getPageSetup().setLeftHeader("&G");
-
- //将显示模式设置为Layout
- sheet.setViewMode(ViewMode.Layout);
- }
- //保存文档
- if (inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length()).equalsIgnoreCase("xls")) {
- workbook.saveToFile(outPath, ExcelVersion.Version97to2003);
- } else {
- workbook.saveToFile(outPath, ExcelVersion.Version2010);
- }
- }
-
- /**
- * PPT设置水印
- * 限制,10页以内可用
- *
- * @param path
- * @param targetpath
- * @param markStr
- * @throws IOException
- */
- public static void setPPTWaterMark(String path, String targetpath, String markStr) throws Exception {
- //创建presentation对象并加载示例文档
- Presentation presentation = new Presentation();
- presentation.loadFromFile(path);
- //限制,10页以内可用,超过10页的有水印
- if (presentation.getSlides().size() > 10) {
- throw new MyException(ResultCode.PARAM_MISS, "文档大于10页!");
- }
- //设置文本水印的宽和高
- int width = 600;
- int height = 300;
-
- //定义一个长方形区域
- Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
- (presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);
-
- //添加一个shape到定义区域
- for (int i = 0; i < presentation.getSlides().size(); i++) {
- IAutoShape shape = presentation.getSlides().get(i).getShapes().appendShape(ShapeType.RECTANGLE, rect);
-
- //设置shape样式
- shape.getFill().setFillType(FillFormatType.NONE);
- shape.getShapeStyle().getLineColor().setColor(Color.white);
- shape.setRotation(-45);
- shape.getLocking().setSelectionProtection(true);
- shape.getLine().setFillType(FillFormatType.NONE);
-
- //添加文本到shape
- shape.getTextFrame().setText(markStr);
- PortionEx textRange = shape.getTextFrame().getTextRange();
-
- //设置文本水印样式
- textRange.getFill().setFillType(FillFormatType.SOLID);
- textRange.getFill().getSolidColor().setColor(Color.GRAY);
- textRange.setFontHeight(50);
- }
-
- //保存文档
- if (path.substring(path.lastIndexOf(".") + 1, path.length()).equalsIgnoreCase("ppt")) {
- presentation.saveToFile(targetpath, com.spire.presentation.FileFormat.PPT);
- } else {
- presentation.saveToFile(targetpath, com.spire.presentation.FileFormat.PPTX_2010);
- }
- }
-
- private static BufferedImage drawText(String text, Font font, Color textColor, Color backColor, double height, double width) {
- //定义图片宽度和高度
- BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);
- Graphics2D loGraphic = img.createGraphics();
-
- //获取文本size
- FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);
- int liStrWidth = loFontMetrics.stringWidth(text);
- int liStrHeight = loFontMetrics.getHeight();
-
- //文本显示样式及位置
- loGraphic.setColor(backColor);
- loGraphic.fillRect(0, 0, (int) width, (int) height);
- loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
- loGraphic.rotate(Math.toRadians(-45));
-
- loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);
- loGraphic.setFont(font);
- loGraphic.setColor(textColor);
- loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
- loGraphic.dispose();
- return img;
- }
- }
spire.office.free下载地址:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
spire.office付费使用,spire.office.free有水印
需要无水印的请跳转:java给图片、word、ppt、excel、pdf添加水印
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。