package com.example.demo; import com.aspose.words.*; import com.aspose.words.Shape; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.awt.*; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws Exception { String fileName = "样例1"; // 模拟获取前端传输过来的base64 // 打开现有的Word文档 Document doc = new Document(GetWebProjectRealPathTool.getRootPath() + "/"+"ccmuJsfw.docx"); // 创建FindReplaceOptions对象 FindReplaceOptions options = new FindReplaceOptions(); options.setReplacingCallback(new IReplacingCallback() { @Override public int replacing(ReplacingArgs e) throws Exception { // 创建一个DocumentBuilder并将其光标移至匹配节点的位置 DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument()); builder.moveTo(e.getMatchNode()); String html = new String(Files.readAllBytes(Paths.get(GetWebProjectRealPathTool.getRootPath() + "/"+fileName+".txt")), StandardCharsets.UTF_8); builder.insertHtml(html); // 告诉引擎我们已经替换了文本,跳过默认的替换动作 return ReplaceAction.SKIP; } }); // 执行查找和替换 doc.getRange().replace("{{协议信息}}", "", options); insertWatermarkText(doc, "CCMU"); doc.save(GetWebProjectRealPathTool.getRootPath() + "/"+"output"+fileName+".docx"); // // Document doc = new Document("path/to/your/document.docx"); } private static void insertWatermarkText(Document doc, String watermarkText) throws Exception { // Create a watermark shape. Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); // Set various properties for the shape. watermark.getTextPath().setText(watermarkText); watermark.getTextPath().setFontFamily("Arial"); watermark.setWidth(300); watermark.setHeight(80); watermark.setRotation(-40); // Rotate the text watermark.getFill().setColor(Color.GRAY); // Try other color combinations if you want watermark.setStrokeColor(Color.GRAY); // Place the watermark in the page center. watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE); watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE); watermark.setWrapType(WrapType.NONE); watermark.setVerticalAlignment(VerticalAlignment.CENTER); watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); // Create a new paragraph and append the watermark to this paragraph. Paragraph watermarkPara = new Paragraph(doc); watermarkPara.appendChild(watermark); // Insert the watermark into all headers of each section. for (Section sect : doc.getSections()) { // There could be up to three different headers in each section, so we insert the watermark into all of them. insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST); insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN); } } private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception { HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType); if (header == null) { // There is no header of the specified type in the current section, create it. header = new HeaderFooter(sect.getDocument(), headerType); sect.getHeadersFooters().add(header); } // Insert a clone of the watermark into the header. header.appendChild(watermarkPara.deepClone(true)); } }