当前位置:网站首页>Word plus watermark
Word plus watermark
2022-04-23 08:25:00 【beinlife】
word Add watermark
-
dependent jar package :jacob-1.17-x64.rar
- Pay attention to distinguish between 32 Bit and 64 System
- Copy jacob-1.17-M4-x64.dll Put it in java jdk bin Under the table of contents 、 System catalog /WINDOWS/system32 Under the table of contents
The code is as follows :
public class TestWaterWord {
public TestWaterWord() {}
private static TestWaterWord instance;
private Dispatch aDoc = null;// Used to store a document : For example, when adding a new document, it returns , New document
private Dispatch activeWindow = null;// Currently active window
private Dispatch docSelection = null;// Store the currently selected document
private Dispatch wordDocs = null;// Store all documents
private String fileName;
private ActiveXComponent wordApp;//Word object
public final static synchronized TestWaterWord getInstance() {
if (instance == null){
instance = new TestWaterWord();
}
return instance;
}
/*** initialization Word object */
public boolean initWordApp() {
boolean retFlag = false;
// initialization com Threads 【 It's equivalent to opening the refrigerator door , Prepare the amplification box 】
// After use, call realease Method to close the thread 【 It's equivalent to closing the refrigerator door 】
ComThread.InitSTA();
wordApp = new ActiveXComponent("Word.Application");// initialization word Applications , The initialization table is :Excel.Application
try {
wordApp.setProperty("Visible", new Variant(false));// Configuration to start word Whether to display execution or implicit execution
wordDocs = wordApp.getProperty("Documents").toDispatch();// obtain word All document objects
retFlag = true;
} catch (Exception e) {
retFlag = false;
e.printStackTrace();
}
return retFlag;
}
/** Open an existing document */
public void openDocument(String docPath) {
if (this.aDoc != null) {
this.closeDocument();
}
aDoc = Dispatch.call(wordDocs,"Open",new Variant(docPath)).toDispatch();//docPath The detailed address of the document to be opened
docSelection = Dispatch.get(wordApp, "Selection").toDispatch();// Get the document object , And back to
}
/** Get the active form object */
public void getActiveWindow() {
activeWindow = wordApp.getProperty("ActiveWindow").toDispatch();// Get the active form object
}
/*** Create a new word file */
public void createNewDocument() {
aDoc = Dispatch.call(wordDocs, "Add").toDispatch();// Create a new word file , And back to
docSelection = Dispatch.get(wordApp, "Selection").toDispatch();// Get the document object , And back to
}
/*** Save and close the current word file */
public void closeDocument() {
if (aDoc != null) {
Dispatch.call(aDoc, "Save");// preservation
Dispatch.call(aDoc, "Close", new Variant(0));// close
aDoc = null;
}
}
/*** close Word resources */
public void closeWordObj() {
wordApp.invoke("Quit", new Variant[] {});// close word file
ComThread.Release();// Release com Threads . according to jacob Help document for ,com Thread recycling is not supported by java Garbage collector for disposal
}
/** * Insert a picture * @param pages Total number of pages * @param imgPath Picture path * @param left From the upper left corner * @param top From the upper corner */
public void setImages(int pages,String imgPath,int left,int top){
for(int i = 0; i < pages;i++){
Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch();
Dispatch inLineShapes = Dispatch.get(selection, "InLineShapes").toDispatch();
Dispatch picture = Dispatch.call(inLineShapes, "AddPicture", imgPath).toDispatch();
// Select the image
Dispatch.call(picture, "Select");
// Set the width and height
Dispatch.put(picture, "Width", new Variant(100));
Dispatch.put(picture, "Height", new Variant(100));
// Set the offset position of the picture relative to the upper left corner
selection = Dispatch.get(wordApp, "Selection").toDispatch();
Dispatch shapeRange = Dispatch.get(selection, "ShapeRange").toDispatch();
Dispatch.put(shapeRange, "Left", new Variant(left));
Dispatch.put(shapeRange, "Top", new Variant(top));
// Turn to the next page
Dispatch browser = Dispatch.get(wordApp, "Browser").toDispatch();
Dispatch.call(browser, "Next");
}
}
/** * Set header * @param waterMarkStr */
public void setHeader(String headerStr) {
Dispatch activePane = Dispatch.get(activeWindow, "ActivePane").toDispatch();// Active pane
Dispatch view = Dispatch.get(activePane, "View").toDispatch();// Window object
Dispatch.put(view, "SeekView", new Variant(9));// Open the header , The value is 9, The footer value is 10
Dispatch headerfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();// Get header and footer
Dispatch range = Dispatch.get(headerfooter, "Range").toDispatch();// Header assignment
Dispatch.put(range, "Text", new Variant(headerStr));
Dispatch font = Dispatch.get(range, "Font").toDispatch();// Set the font
Dispatch.put(font, "Name", new Variant(" Microsoft YaHei "));
Dispatch.put(font, "Bold", new Variant(true));
Dispatch.put(font, "Size", 20);
Dispatch.put(font, "Color", Integer.valueOf("0000FF",16).toString());// The color is 16 Write backwards , And then go 10 Base number
Dispatch.put(view, "SeekView", new Variant(0)); //0 Represents the recovery view ;
}
/** Document setting image watermark ,waterPic Watermark image path */
public void setWaterPic(String waterPic) {
Dispatch activePane = Dispatch.get(activeWindow, "ActivePane").toDispatch();// Active pane
Dispatch view = Dispatch.get(activePane, "View").toDispatch();// Window object
Dispatch.put(view, "SeekView", new Variant(9));// Open the header , The value is 9, The footer value is 10
Dispatch headerfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();// Get header and footer
Dispatch shapes = Dispatch.get(headerfooter, "Shapes").toDispatch();// Gets the watermark graphic object
// call shapes Object's AddPicture Method to insert a picture into the current document
Dispatch picture = Dispatch.call(shapes,"AddPicture",waterPic).toDispatch();
Dispatch.call(picture, "Select");// Select current word Document image watermark
Dispatch.put(picture, "Left", new Variant(120));// Set picture watermark parameters
Dispatch.put(picture, "Top", new Variant(240));
Dispatch.put(picture, "LockAspectRatio", new Boolean(true));// Keep the length width ratio unchanged when resizing
Dispatch.put(picture, "Width", new Variant(120));
Dispatch.put(picture, "Height", new Variant(90));
Dispatch.put(view, "SeekView", new Variant(0));// Close header ,0 Represents the recovery view ;
}
/** Text watermark settings -- The essence is to set the header , Turn text into WordArt pictures */
public void setWaterMark(String waterMarkStr) {
Dispatch activePane = Dispatch.get(activeWindow, "ActivePane").toDispatch();// Active pane
Dispatch view = Dispatch.get(activePane, "View").toDispatch();// Window object
Dispatch.put(view, "SeekView", new Variant(9));// Open the header , The value is 9, The footer value is 10
Dispatch headerfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();// Get header and footer
Dispatch shapes = Dispatch.get(headerfooter, "Shapes").toDispatch();// Gets the watermark graphic object
/** Insert text , And turn it into a picture : * Action object 、 Method 、 WordArt format [0 white 、1 Black, lower left, upper right ,2 Up and down in the dark 、3 Black small and medium-sized 、4 Black vertical row 、5 Black in the middle and down ] * Watermark content 、 typeface 、 font size 、 Whether the font is bold 、 Whether the font is italic * The left margin 、 From the above */
Dispatch selection = Dispatch.call(shapes, "AddTextEffect",new Variant(0),
waterMarkStr, " Microsoft YaHei ", new Variant(10),new Variant(true), new Variant(false),
new Variant(150),new Variant(250)).toDispatch();
// Select the current document watermark
Dispatch.call(selection, "Select");
Dispatch shapeRange = Dispatch.get(docSelection, "ShapeRange").toDispatch();
/** * Set watermark transparency and color */
Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject6");
Dispatch textEffect = Dispatch.get(shapeRange, "TextEffect").toDispatch();
Dispatch.put(textEffect, "NormalizedHeight", new Boolean(false));
Dispatch line = Dispatch.get(shapeRange, "Line").toDispatch();
Dispatch.put(line, "Visible", new Boolean(false));
Dispatch fill = Dispatch.get(shapeRange, "Fill").toDispatch();
Dispatch.put(fill, "Visible", new Boolean(true));
Dispatch.put(fill, "Transparency", new Variant(0.1));// Set watermark transparency
Dispatch foreColor = Dispatch.get(fill, "ForeColor").toDispatch();
Dispatch.put(foreColor, "RGB", new Variant(255));// Set watermark color
Dispatch.call(fill, "Solid");
/** * Set the watermark rotation angle 、 Watermark size */
Dispatch.put(shapeRange, "Rotation", new Variant(0));// Rotation Angle
Dispatch.put(shapeRange, "LockAspectRatio", new Boolean(true));// Keep the length width ratio unchanged when resizing
Dispatch.put(shapeRange, "Height", new Variant(10));// high
Dispatch.put(shapeRange, "Width", new Variant(40));// wide
Dispatch.put(shapeRange, "Left", new Variant(160));
Dispatch.put(shapeRange, "Top", new Variant(270));
Dispatch.put(view, "SeekView", new Variant(0));//0 Represents the recovery view ;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
// Testing capabilities
public static void main(String[] argv) {
TestWaterWord d = TestWaterWord.getInstance();
try {
if (d.initWordApp()) {
d.openDocument("C:/Users/solexit06/Desktop/testWater.docx");
d.getActiveWindow();
String imgPath="C:/Users/solexit06/Desktop/Border.png";
d.setWaterPic(imgPath);// Pictures on page
Date date=Calendar.getInstance().getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String waterDate=sdf.format(date);
d.setWaterMark(waterDate);// date
d.setHeader("**** Technology ");// header
d.closeDocument();
} else{
System.out.println(" initialization Word Failed to read and write object !");
}
} catch (Exception e) {
System.out.println(e);
}finally{
d.closeWordObj();
}
}
}
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230734168776.html
边栏推荐
- DOM学习笔记---遍历页面所有元素节点
- Transformer XL: attention language modelsbbeyond a fixed length context paper summary
- Protobuf简介
- 耳穴诊疗随笔0421
- SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
- C language learning record -- use and analysis of string function (2)
- CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
- Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
- LeetCode中等题之旋转函数
- 使用JWT生成与解析Token
猜你喜欢

输入/输出系统

WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code

excle加水印

396. Rotate Function

Qt读写XML文件

Qt利用QtXlsx操作excel文件

A simple theme of Typecho with beautiful appearance_ Scarfskin source code download

ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建

一个必看的微信小程序开发指南1-基础知识了解

freertos学习02-队列 stream buffer message buffer
随机推荐
stm32以及freertos 堆栈解析
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
Common regular expressions
Rearranging log files for leetcode simple question
vslam PPT
总线结构概述
Rotation function of leetcode medium problem
【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
input元素添加监听事件
Campus transfer second-hand market source code download
2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
LeetCode简单题之三除数
QFileDialog select multiple files or folders
刨析——浏览器如何工作
dried food! Point based: differentiable Poisson solver
Idea: export Yapi interface using easyyapi plug-in
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
耳穴诊疗随笔0421
[effective go Chinese translation] part I