使用apose实现doc转pdf或者pdf转doc,同时支持目录检测
package com.word;import java.io.File; import java.io.IOException;import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch;/*** word转pdf的工具,效果较好,使用该工具需要将dll下面的jacob-1.20-x64.dll复制到jdb/bin目录下面,运行环境应该在windows下,没有页码不对问题。* * @time 2021年11月17日下午2:18:33*/ public class Word2PdfUtil {static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。static final int wdFormatPDF = 17;// word转PDF 格式public static void main(String[] args) throws IOException {String source1 = "C:\\aa.docx";String target1 = "C:\\bb.pdf";Word2PdfUtil.word2pdf(source1, target1);}/*** * @param source* word路径* @param target* 生成的pdf路径* @return*/public static boolean word2pdf(String source, String target) {System.out.println("Word转PDF开始启动...");long start = System.currentTimeMillis();ActiveXComponent app = null;try {app = new ActiveXComponent("Word.Application");app.setProperty("Visible", false);Dispatch docs = app.getProperty("Documents").toDispatch();System.out.println("打开文档:" + source);Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();System.out.println("转换文档到PDF:" + target);File tofile = new File(target);if (tofile.exists()) {tofile.delete();}Dispatch.call(doc, "SaveAs", target, wdFormatPDF);Dispatch.call(doc, "Close", false);long end = System.currentTimeMillis();System.out.println("转换完成,用时:" + (end - start) + "ms");return true;} catch (Exception e) {System.out.println("Word转PDF出错:" + e.getMessage());return false;} finally {if (app != null) {app.invoke("Quit", wdDoNotSaveChanges);}}}}
提取pdf目录:
package com.pdf.search;import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.pdfbox.io.RandomAccessBuffer; import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode; import org.apache.pdfbox.text.PDFTextStripper;public class PdfMulu {public static void main(String[] args) throws Exception {long old = System.currentTimeMillis();try {String srcFile = "D://功能说明 (1).pdf";List<String> pages = readPDF(srcFile);List<String> titles = new ArrayList<>();boolean flag = false;String reg = "^(?=.*目)(?=.*录).+";Pattern p = Pattern.compile(reg);for (String page : pages) {Matcher m = p.matcher(page);if (m.find()&&!flag) {flag = true;System.out.println("开始目录检测:");titles = getTitles(page,titles);}else{titles = getTitles(page,titles);}// String [] arr = page.split("\\s+"); // if(arr.length>0){ // System.out.println(arr[arr.length-1]); // }} // System.out.println(titles);//查找目录内容在第几页或者第几页有没有目录文字File pdfFile = new File(srcFile);byte[] pdfData = new byte[(int) pdfFile.length()];FileInputStream inputStream = null;try {inputStream = new FileInputStream(pdfFile);inputStream.read(pdfData);} catch (IOException e) {throw e;} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {}}}for(String title :titles){String ptitle = title.split("%%%%%%")[0].replaceAll("\\s*","");String pnum = title.split("%%%%%%")[1].trim(); // if(Integer.parseInt(pnum)<113){ // continue; // } // System.out.println(ptitle+":"+pnum);if(!PdfKeywordFinder.getPageNums(pdfData,ptitle,Integer.parseInt(pnum))){System.out.println("目录:"+title.split("%%%%%%")[0]+";显示页码为:"+Integer.parseInt(pnum)+",与实际不符。");}}} catch (Exception e) {e.printStackTrace();}long now = System.currentTimeMillis();System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");}public static List<String> readPDF(String file) throws IOException {List result = new ArrayList();FileInputStream is = new FileInputStream(file);PDFParser parser = new PDFParser(new RandomAccessBuffer(is));parser.parse();PDDocument doc = parser.getPDDocument();PDFTextStripper textStripper =new PDFTextStripper();for(int i=1;i<=doc.getNumberOfPages();i++){textStripper.setStartPage(i);textStripper.setEndPage(i);textStripper.setSortByPosition(true);//按顺序行读String s=textStripper.getText(doc);result.add(s);}doc.close();return result;}public static List<String> getTitles (String content,List<String> titles){String[] lines = content.split("\r\n");String reg= "(.*)(\\,{7,1000}|\\.{7,1000})(.*){1,1000}";Pattern p = Pattern.compile(reg);for (int i = 0; i < lines.length; i++) {String str= lines[i];Matcher m = p.matcher(str);if( m.find()){String ma = m.group().trim();if(str.split("(\\.{7,1000})").length>1){String[] title = ma.split("(\\.{7,1000})");titles.add(title[0]+"%%%%%%"+title[1]); // System.out.println(title[0]+"$$$"+title[1]);}}}return titles;}public void printBookmarks(PDOutlineNode bookmark,String indentation) throws IOException{PDOutlineItem current = bookmark.getFirstChild();while(current != null){int pages = 0;if(current.getDestination() instanceof PDPageDestination){PDPageDestination pd = (PDPageDestination) current.getDestination();pages = pd.retrievePageNumber();}if (current.getAction() instanceof PDActionGoTo) {PDActionGoTo gta = (PDActionGoTo) current.getAction();if (gta.getDestination() instanceof PDPageDestination) {PDPageDestination pd = (PDPageDestination) gta.getDestination();pages = pd.retrievePageNumber();}}if (pages == 0) {System.out.println(indentation+current.getTitle());}else{System.out.println(indentation+current.getTitle()+"------"+pages);}printBookmarks( current, indentation);current = current.getNextSibling();}}//获取页码public static void getPageNumbers(HashMap<String, Object> bookmark) {if (bookmark == null) return;if ("GoTo".equals(bookmark.get("Action"))) {String page = (String) bookmark.get("Page");if (page != null) {page = page.trim();int idx = page.indexOf(' ');int pageNum;if (idx < 0) {pageNum = Integer.parseInt(page);System.out.println("pageNum :" + pageNum);} else {pageNum = Integer.parseInt(page.substring(0, idx));System.out.println("pageNum:" + pageNum);}}@SuppressWarnings("unchecked")ArrayList<HashMap<String, Object>> kids = (ArrayList<HashMap<String, Object>>) bookmark.get("Kids");if (kids == null) return;for (Iterator<HashMap<String, Object>> i = kids.iterator(); i.hasNext(); ) {getPageNumbers(i.next());}}} }
下载地址:使用apose实现doc转pdf或者pdf转doc,含license.xml文件,还有word、pdf目录检测等功能。-Java文档类资源-CSDN下载
使用apose实现doc转pdf或者pdf转doc,同时支持目录检测相关推荐
- Java实现doc或xls转PDF
1.添加jar包:aspose-words-15.8.0-jdk16.jar 2.实现工具类: package com.example.demo.config;import java.io.*; im ...
- 【script】python实现多线程将doc、docx转pdf,doc转docx
摘要 本文讲述通过python实现多线程将当前目录下的 .doc文档转.pdf..docx文档转.pdf,以及.doc文档转.docx(在python中,如需对word文档进行读写,只能读写docx格 ...
- 和doc的区别怎么转换手机_如何把doc文件转换成PDF格式?高手告诉你应该这么做...
视频步骤: 知乎视频www.zhihu.com 有时候老师让交的实验报告文件格式必须为PDF格式,因为PDF与不同的操作系统和软件版本具有很好的兼容性,所以大多数人都喜欢将Word转换成PDF.但有 ...
- 常用文件格式转换大全: PDF转Word,WORD转PDF转换器,DOCX转DOC转换
为什么80%的码农都做不了架构师?>>> 常用文件格式转换大全: Diywz - PDF转Word,WORD转PDF转换器,DOCX转DOC转换器 常用的文件格式互转工具,实 ...
- react在线浏览doc_如何把doc文件转换成PDF格式?教你一招秒实现
有时候老师让交的实验报告文件格式必须为PDF格式,因为PDF与不同的操作系统和软件版本具有很好的兼容性,所以大多数人都喜欢将Word转换成PDF.但有的同学的Word版本不能直接将doc文件转换成PD ...
- python读取 xls,xlsx,csv,doc,docx,pdf 格式的文件
前言 利用python读取 xls,xlsx,csv,doc,docx,pdf 格式的文件 python版本3.10.4 读取xls pip install xlrd==2.0.1 wps的.et文件 ...
- Java实现根据Word模板填充表格数据(poi方式),以及doc和docx转PDF,最全最详细版本,解决外部引用jar在linux上报ClassNotFound的问题。
Java实现根据Word模板填充表格数据(poi方式),以及doc和docx转PDF,最全最详细版本,解决外部引用jar在linux上报ClassNotFound的问题. 适用场景: 1.固定格式的W ...
- 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859 本文出自[我是干勾鱼的博客] 这里讲一下使用Lucene对doc. ...
- 参考file-convert-util工具,实现doc,docx,html,md,pdf,png转换
参考 https://gitee.com/zhengqingya/file-convert-util 项目,打包到本地仓库实现doc,docx,html,md,pdf,png转换 https://gi ...
最新文章
- 聊聊Mysql的那些破事儿
- SAP SD基础知识之组织架构设计-Shipping Point篇
- httpclient 学习
- Django 笔记4 -- 模板
- boost::mp11::mp_transform_if相关用法的测试程序
- ORACLE TEXT DATASTORE PREFERENCE(五)
- (2) freemarker入门案例2
- dojo中的dojo/dom-class
- python 编程效率_如何有效提升数据分析效率?五大Python技巧
- 如何引用传递参数从一个函数中得到多个返回值
- Elasticsearch 2.3.x 拼音分词 analysis-lc-pinyin
- Spring的注入方式中,官方推荐哪种方式
- DSP31段调音教程及调音MP3
- mysql增加约束sql语句_sql语句添加约束
- 如何把苹果手机iPhone/iPad无线投屏到MAC电脑计算机上面?
- Origin: 软件申请+续期指引
- 高效能人士的七个习惯读后感与总结概括-(第三章)
- 海量实时广告流平台(DSP广告系统)架构设计与实践
- 库房--库存信息增加导出Excel表格功能
- linux卸载内核网卡驱动,ubuntu 16.04 卸载旧网卡noveau装新的网卡驱动