当前位置:   article > 正文

java使用spire.office.free给office添加水印

spire.office.free

    Spire.Office for Java 是一套企业级的涵盖 E-iceblue 所有 Java组件的集合,它包括以下产品的最新版本:Spire.Doc for Java, Spire.XLS for JavaSpire.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

 

  1. package com.feng.util;
  2. import com.feng.bean.ResultCode;
  3. import com.feng.error.MyException;
  4. import com.spire.doc.*;
  5. import com.spire.doc.FileFormat;
  6. import com.spire.doc.documents.WatermarkLayout;
  7. import java.awt.*;
  8. import java.awt.geom.Rectangle2D;
  9. import java.awt.image.BufferedImage;
  10. import java.io.IOException;
  11. import com.spire.presentation.IAutoShape;
  12. import com.spire.presentation.PortionEx;
  13. import com.spire.presentation.Presentation;
  14. import com.spire.presentation.ShapeType;
  15. import com.spire.presentation.drawing.FillFormatType;
  16. import com.spire.xls.ExcelVersion;
  17. import com.spire.xls.ViewMode;
  18. import com.spire.xls.Workbook;
  19. import com.spire.xls.Worksheet;
  20. import lombok.extern.slf4j.Slf4j;
  21. import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
  22. /**
  23. * Spire给office添加水印
  24. * @Author: feng
  25. * @Date: 2020/6/28 2:35 下午
  26. */
  27. @Slf4j
  28. public class SpireWaterMarkUtils {
  29. public static void main(String[] args) throws Exception {
  30. //word加水印
  31. setWordWaterMark("d:/tmp/tmp/2.doc", "d:/tmp/tmp/3.doc", "张三|159****1234");
  32. setWordWaterMark("d:/tmp/tmp/2.docx", "d:/tmp/tmp/3.docx", "张三|159****1234");
  33. //xls加水印
  34. setExcelWaterMark("d:/tmp/tmp/2.xls", "d:/tmp/tmp/3.xls", "张三|159****1234");
  35. setExcelWaterMark("d:/tmp/tmp/2.xlsx", "d:/tmp/tmp/3.xlsx", "张三|159****1234");
  36. //ppt加水印
  37. setPPTWaterMark("d:/tmp/tmp/2.ppt", "d:/tmp/tmp/3.ppt", "张三|159****1234");
  38. setPPTWaterMark("d:/tmp/tmp/2.pptx", "d:/tmp/tmp/3.pptx", "张三|159****1234");
  39. setPPTWaterMark("d:/tmp/tmp/22.ppt", "d:/tmp/tmp/33.ppt", "张三|159****1234");
  40. }
  41. /**
  42. * word文字水印 (doc,docx)
  43. *
  44. * @param inputPath
  45. * @param outPath
  46. * @param markStr
  47. */
  48. public static void setWordWaterMark(String inputPath, String outPath, String markStr) {
  49. Document document = new Document();
  50. document.loadFromFile(inputPath);
  51. TextWatermark txtWatermark = new TextWatermark();
  52. txtWatermark.setText(markStr);
  53. txtWatermark.setFontSize(40);
  54. txtWatermark.setColor(Color.GRAY);
  55. txtWatermark.setLayout(WatermarkLayout.Diagonal);
  56. document.getSections().get(0).getDocument().setWatermark(txtWatermark);
  57. document.saveToFile(outPath, FileFormat.Auto);
  58. }
  59. /**
  60. * excel设置水印
  61. * Excel 水印在正常模式下不可见,仅在页面布局模式或打印预览模式可见。
  62. *
  63. * @param inputPath
  64. * @param outPath
  65. * @param markStr
  66. */
  67. public static void setExcelWaterMark(String inputPath, String outPath, String markStr) {
  68. //加载示例文档
  69. Workbook workbook = new Workbook();
  70. workbook.loadFromFile(inputPath);
  71. //设置文本和字体大小
  72. Font font = new Font("宋体", Font.PLAIN, 40);
  73. for (Object object : (Iterable) workbook.getWorksheets()) {
  74. Worksheet sheet = (Worksheet) object;
  75. //调用DrawText() 方法插入图片
  76. BufferedImage imgWtrmrk = drawText(markStr, font, Color.GRAY, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());
  77. //将图片设置为页眉
  78. sheet.getPageSetup().setLeftHeaderImage(imgWtrmrk);
  79. sheet.getPageSetup().setLeftHeader("&G");
  80. //将显示模式设置为Layout
  81. sheet.setViewMode(ViewMode.Layout);
  82. }
  83. //保存文档
  84. if (inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length()).equalsIgnoreCase("xls")) {
  85. workbook.saveToFile(outPath, ExcelVersion.Version97to2003);
  86. } else {
  87. workbook.saveToFile(outPath, ExcelVersion.Version2010);
  88. }
  89. }
  90. /**
  91. * PPT设置水印
  92. * 限制,10页以内可用
  93. *
  94. * @param path
  95. * @param targetpath
  96. * @param markStr
  97. * @throws IOException
  98. */
  99. public static void setPPTWaterMark(String path, String targetpath, String markStr) throws Exception {
  100. //创建presentation对象并加载示例文档
  101. Presentation presentation = new Presentation();
  102. presentation.loadFromFile(path);
  103. //限制,10页以内可用,超过10页的有水印
  104. if (presentation.getSlides().size() > 10) {
  105. throw new MyException(ResultCode.PARAM_MISS, "文档大于10页!");
  106. }
  107. //设置文本水印的宽和高
  108. int width = 600;
  109. int height = 300;
  110. //定义一个长方形区域
  111. Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
  112. (presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);
  113. //添加一个shape到定义区域
  114. for (int i = 0; i < presentation.getSlides().size(); i++) {
  115. IAutoShape shape = presentation.getSlides().get(i).getShapes().appendShape(ShapeType.RECTANGLE, rect);
  116. //设置shape样式
  117. shape.getFill().setFillType(FillFormatType.NONE);
  118. shape.getShapeStyle().getLineColor().setColor(Color.white);
  119. shape.setRotation(-45);
  120. shape.getLocking().setSelectionProtection(true);
  121. shape.getLine().setFillType(FillFormatType.NONE);
  122. //添加文本到shape
  123. shape.getTextFrame().setText(markStr);
  124. PortionEx textRange = shape.getTextFrame().getTextRange();
  125. //设置文本水印样式
  126. textRange.getFill().setFillType(FillFormatType.SOLID);
  127. textRange.getFill().getSolidColor().setColor(Color.GRAY);
  128. textRange.setFontHeight(50);
  129. }
  130. //保存文档
  131. if (path.substring(path.lastIndexOf(".") + 1, path.length()).equalsIgnoreCase("ppt")) {
  132. presentation.saveToFile(targetpath, com.spire.presentation.FileFormat.PPT);
  133. } else {
  134. presentation.saveToFile(targetpath, com.spire.presentation.FileFormat.PPTX_2010);
  135. }
  136. }
  137. private static BufferedImage drawText(String text, Font font, Color textColor, Color backColor, double height, double width) {
  138. //定义图片宽度和高度
  139. BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);
  140. Graphics2D loGraphic = img.createGraphics();
  141. //获取文本size
  142. FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);
  143. int liStrWidth = loFontMetrics.stringWidth(text);
  144. int liStrHeight = loFontMetrics.getHeight();
  145. //文本显示样式及位置
  146. loGraphic.setColor(backColor);
  147. loGraphic.fillRect(0, 0, (int) width, (int) height);
  148. loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
  149. loGraphic.rotate(Math.toRadians(-45));
  150. loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);
  151. loGraphic.setFont(font);
  152. loGraphic.setColor(textColor);
  153. loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
  154. loGraphic.dispose();
  155. return img;
  156. }
  157. }

spire.office.free下载地址:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html

 

spire.office付费使用,spire.office.free有水印

需要无水印的请跳转:java给图片、word、ppt、excel、pdf添加水印

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/728408
推荐阅读
相关标签
  

闽ICP备14008679号