/** * Inserts a watermark into a document. you need saved the doc by yourself. * * @param doc The input document file. * @param watermarkText Text of the watermark. */ publicstaticvoidinsertWatermarkText(Document doc, String watermarkText) { // 居中 insertWatermarkText(doc, watermarkText, newFunction<Shape, Object>() { @Override public Object apply(Shape watermark) { // Place the watermark in the page center. watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE); watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE); watermark.setWrapType(WrapType.NONE); // TOP_BOTTOM : 将所设置位置的内容往上下顶出去 watermark.setVerticalAlignment(VerticalAlignment.CENTER); watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); returnnull; } }); // 顶部 insertWatermarkText(doc, watermarkText, newFunction<Shape, Object>() { @Override public Object apply(Shape watermark) { watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN); watermark.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN); watermark.setWrapType(WrapType.NONE); // 我们需要自定义距离顶部的高度 // watermark.setVerticalAlignment(VerticalAlignment.TOP); watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); // 设置距离顶部的高度 watermark.setTop(160); returnnull; } }); // 底部 insertWatermarkText(doc, watermarkText, newFunction<Shape, Object>() { @Override public Object apply(Shape watermark) { watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN); watermark.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN); watermark.setWrapType(WrapType.NONE); // 我们需要自定义距离顶部的高度 // watermark.setVerticalAlignment(VerticalAlignment.BOTTOM); watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); // 设置距离顶部的高度 watermark.setTop(480); returnnull; } }); } privatestaticvoidinsertWatermarkText(Document doc, String watermarkText, Function<Shape, Object> watermaskPositionConfigFunc) { // Create a watermark shape. This will be a WordArt shape. // You are free to try other shape types as watermarks. Shapewatermark=newShape(doc, ShapeType.TEXT_PLAIN_TEXT); // Set up the text of the watermark. watermark.getTextPath().setText(watermarkText); // Set up the text of the watermark. // 这里设置为宋体可以保证在转换为PDF时中文不是乱码. watermark.getTextPath().setFontFamily("宋体");//Arial; try { // 水印大小 watermark.setWidth(200); watermark.setHeight(50); } catch (Exception e) { thrownewRuntimeException(e); } // Text will be directed from the bottom-left to the top-right corner. // 左下到右上 watermark.setRotation(-40); // Remove the following two lines if you need a solid black text. finalStringcolorStr="E0E0E0"; watermark.getFill().setColor(newjava.awt.Color(Integer.parseInt(colorStr, 16))); // Try Color.lightGray to get more Word-style watermark watermark.setStrokeColor(newjava.awt.Color(Integer.parseInt(colorStr, 16))); // Try Color.lightGray to get more Word-style watermark // Place the watermark in the special location of page . watermaskPositionConfigFunc.apply(watermark); // Create a new paragraph and append the watermark to this paragraph. ParagraphwatermarkPara=newParagraph(doc); watermarkPara.appendChild(watermark); // Insert the watermark into all headers of each document section. for (Section sect : doc.getSections()) { // There could be up to three different headers in each section, since we want // the watermark to appear on all pages, insert into all headers. insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); // 以下注释掉不影响效果, 未作深入研究, 时间有限. // insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST); // insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN); } // 参考下API : https://apireference.aspose.com/java/words/com.aspose.words/ShapeBase //watermark.setZOrder(-100); } privatestaticvoidinsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) { HeaderFooterheader= sect.getHeadersFooters().getByHeaderFooterType(headerType); if (header == null) { // There is no header of the specified type in the current section, create it. header = newHeaderFooter(sect.getDocument(), headerType); sect.getHeadersFooters().add(header); } // Insert a clone of the watermark into the header. try { header.appendChild(watermarkPara.deepClone(true)); } catch (Exception e) { thrownewRuntimeException(e); } }