package com.htl.test;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestDocPage {
/* 转PDF格式值 */
private static final int wdFormatPDF = 17;
/**
* Word文档转换
*
* @param inputFile
* @param pdfFile
*/
public static boolean word2PDF(String inputFile, String pdfFile) {
ComThread.InitMTA(true);
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
Dispatch wordFile = null;
try {
app = new ActiveXComponent("Word.Application");// 创建一个word对象
app.setProperty("Visible", new Variant(false)); // 不可见打开word
app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch docs = app.getProperty("Documents").toDispatch();// 获取文挡属性
wordFile = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inputFile, new Variant(true), new Variant(false)}, new int[1]).toDispatch();
Dispatch paragraphs = Dispatch.get(wordFile, "Paragraphs").toDispatch();
int paraCount = Dispatch.get(paragraphs, "Count").getInt();
System.out.println("打开Word文档 >>> " + inputFile);
// Object[]第三个参数是表示“是否只读方式打开”
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
// word保存为pdf格式宏,值为17
Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
//创建PdfDocument实例
//PdfDocument pdf = new PdfDocument();
//加载PDF文档
//pdf.loadFromFile("test.pdf");
for (int i = 0; i < paraCount; ++i) {
Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(i + 1)).toDispatch();
int outline = Dispatch.get(paragraph, "OutlineLevel").getInt();
if (outline <= 9) {
System.out.println("大纲等级:" + outline);
System.out.println("\n");
Dispatch paraRange = Dispatch.get(paragraph, "Range").toDispatch();
System.out.println("标题名称:" + Dispatch.get(paraRange, "Text").toString());
int pages = Integer.parseInt(Dispatch.call(paraRange, "information", 1).toString());
System.out.println("标题页码:" + pages);
System.out.println("\n");
}
}
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - start) + "ms.");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:Word文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(wordFile, "Close", new Variant(true));
Dispatch.call(doc, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
// 如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
ComThread.quitMainSTA();
}
return false;
}
public static void main(String[] args) throws Exception {
String filePath = "D:\\内网通\\贺才全\\1\\贵州省政务信息化运维项目实施方案(贵州省教育厅)20230423-v4.3.docx";
ActiveXComponent word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(false));
Dispatch documents = word.getProperty("Documents").toDispatch();
Dispatch wordFile = Dispatch.invoke(documents, "Open", Dispatch.Method, new Object[]{filePath, new Variant(true), new Variant(false)}, new int[1]).toDispatch();
Dispatch paragraphs = Dispatch.get(wordFile, "Paragraphs").toDispatch();
int paraCount = Dispatch.get(paragraphs, "Count").getInt();
for (int i = 0; i < paraCount; ++i) {
Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(i + 1)).toDispatch();
int outline = Dispatch.get(paragraph, "OutlineLevel").getInt();
if (outline <= 9) {
System.out.println("大纲等级:" + outline);
System.out.println("\n");
Dispatch paraRange = Dispatch.get(paragraph, "Range").toDispatch();
System.out.println("标题名称:" + Dispatch.get(paraRange, "Text").toString());
int pages = Integer.parseInt(Dispatch.call(paraRange, "information", 1).toString());
System.out.println("标题页码:" + pages);
System.out.println("\n");
}
}
Dispatch.call(wordFile, "Close", new Variant(true));
Dispatch.call(word, "Quit");
}
}