当前位置:网站首页>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
边栏推荐
猜你喜欢
Flink SQL实现流批一体
数据可视化:使用Excel制作雷达图
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
Kubernetes in browser and IDE | interactive learning platform killercoda
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
【路科V0】验证环境2——验证环境组件
Vowel substring in statistical string of leetcode simple problem
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
随机推荐
AQS & ReentrantLock 实现原理
dmp引擎工作总结(2021,光剑)
数据的删除和修改操作(mysql)
How to generate assembly file
Talk about the basic but not simple stock data
CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
vmware 搭建ES8的常见错误
剑指offer day24 数学(中等)
vslam PPT
让地球少些“碳”息 度能在路上
作文以记之 ~ 二叉树的前序遍历
Introduction to protobuf
【路科V0】验证环境2——验证环境组件
JS common array methods
DOM learning - add + - button
Overview of bus structure
vslam PPT
分组背包呀
LeetCode簡單題之計算字符串的數字和