用java统计pdf,ppt,pptx,doc,docx文件的总页码
2014-07-08 12:10
1836 查看
(说明: 全文的path是指文件的全路径,也可以直接写文件名,看你的文件所在的位置)
一. 对于pdf文件是最好操作的,直接调用iText库的相关函数
iText是sourceforge上的一个项目,一个开源的用于生成pdf文档的java和c#类库。
代码如下:
PdfReader reader = new PdfReader(path);
int pages = reader.getNumberOfPages();
二. 对于office类文件的处理, 选择的是用POI来实现目的.
1. 先下载好POI所用到的jar包
我这边是下载了8个jar包,分别是dom4j-1.6.1.jar,geronimo-stax-api_1.0_spec-1.0.jar,ooxml-schemas-1.0.jar,openxml4j-bin-beta.jar,poi-3.5-beta6-20090622.jar,poi-ooxml-3.5-beta6-20090622.jar,poi-scratchpad-3.5-beta6-20090622.jar,xmlbeans-2.3.0.jar.
2.代码如下:
对于ppt文件,
FileInputStream fis = new FileInputStream(path);
SlideShow pptfile = new SlideShow(new HSLFSlideShow(fis));
int pages = pptfile.getSlides().length;
对于pptx文件:
XSLFSlideShow fis = new XSLFSlideShow(path);
XMLSlideShow pptxfile = new XMLSlideShow(fis);
int pages = pptxfile.getSlides().length;
对于docx文件
FileInputStream docx = new FileInputStream(path);
XWPFDocument docxfile = new XWPFDocument(docx);
int pages = docxfile.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
对于doc文件,由于很多doc文件不在SummaryInformation部分存储页码信息,导致用POI计算的总页码出问题,所以此处采用先转为pdf文件,然后计算总页码
String pdfName = docToPdf(path);
PdfReader reader = new PdfReader(pdfName);
int pages = reader.getNumberOfPages();
File file = new File(pdfName);
if(file.exists()){
file.delete();
}
3.doc转为pdf
采用jacob.jar来调用activeX控件
1)配置环境
只需要一个jar文件 --->jacob.jar,把jacob.dll文件放入到c:/windows/system32中
2)代码如下:
public String docToPdf(String filename) {
int wdDoNotSaveChanges = 0;// 不保存待定的更改
int wdFormatPDF = 17;// PDF 格式
String toFilename = filename.substring(0, filename.lastIndexOf(".")) + ".pdf";
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs,//
"Open", //
filename,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();
File tofile = new File(toFilename);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
toFilename, // FileName
wdFormatPDF);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
return toFilename;
}
一. 对于pdf文件是最好操作的,直接调用iText库的相关函数
iText是sourceforge上的一个项目,一个开源的用于生成pdf文档的java和c#类库。
代码如下:
PdfReader reader = new PdfReader(path);
int pages = reader.getNumberOfPages();
二. 对于office类文件的处理, 选择的是用POI来实现目的.
1. 先下载好POI所用到的jar包
我这边是下载了8个jar包,分别是dom4j-1.6.1.jar,geronimo-stax-api_1.0_spec-1.0.jar,ooxml-schemas-1.0.jar,openxml4j-bin-beta.jar,poi-3.5-beta6-20090622.jar,poi-ooxml-3.5-beta6-20090622.jar,poi-scratchpad-3.5-beta6-20090622.jar,xmlbeans-2.3.0.jar.
2.代码如下:
对于ppt文件,
FileInputStream fis = new FileInputStream(path);
SlideShow pptfile = new SlideShow(new HSLFSlideShow(fis));
int pages = pptfile.getSlides().length;
对于pptx文件:
XSLFSlideShow fis = new XSLFSlideShow(path);
XMLSlideShow pptxfile = new XMLSlideShow(fis);
int pages = pptxfile.getSlides().length;
对于docx文件
FileInputStream docx = new FileInputStream(path);
XWPFDocument docxfile = new XWPFDocument(docx);
int pages = docxfile.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
对于doc文件,由于很多doc文件不在SummaryInformation部分存储页码信息,导致用POI计算的总页码出问题,所以此处采用先转为pdf文件,然后计算总页码
String pdfName = docToPdf(path);
PdfReader reader = new PdfReader(pdfName);
int pages = reader.getNumberOfPages();
File file = new File(pdfName);
if(file.exists()){
file.delete();
}
3.doc转为pdf
采用jacob.jar来调用activeX控件
1)配置环境
只需要一个jar文件 --->jacob.jar,把jacob.dll文件放入到c:/windows/system32中
2)代码如下:
public String docToPdf(String filename) {
int wdDoNotSaveChanges = 0;// 不保存待定的更改
int wdFormatPDF = 17;// PDF 格式
String toFilename = filename.substring(0, filename.lastIndexOf(".")) + ".pdf";
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs,//
"Open", //
filename,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();
File tofile = new File(toFilename);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
toFilename, // FileName
wdFormatPDF);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
return toFilename;
}
相关文章推荐
- Java 使用jacob ppt文件转pptx,doc转docx;word 转html、pdf等
- 【Android】Android阅读各种格式文件(包括DOC/DOCX/WPS/XLS/XLSX/PPT/PPTX/TXT/PDF)
- Android中pdf,doc,docx,xls,xlsx,ppt,pptx等office文件预览
- java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx
- "doc", "docx", "xls", "xlsx", "ppt", "pptx",txt。等文件转化为pdf
- 使用POI将office(doc/docx/ppt/pptx/xls/xlsx)文件转html格式(附带源码)
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- PHP上传和下载与删除doc、docx、ppt、pdf文件
- 将pdf、doc、docx、ppt格式文件转化swf格式文件的方法
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Flash在线文档阅读器::pdf、doc、docx、xls、xlsx、ppt、pptx、htm、txt、rtf、epub、csv、xdoc等
- android 打开 ppt pptx doc docx xls xlsx pdf office 文档
- doc,docx,pdf,ppt等文件类型读取方法
- Flash在线文档阅读器::pdf、doc、docx、xls、xlsx、ppt、pptx、htm、txt、rtf、epub、csv、xdoc等
- Docverter – 文本文件轻松转换为 PDF,Docx 和 ePub 文件
- office doc/xls/ppt 和 docx/xlsx/pptx 区别
- centos下ppt(pptx)文件转换为pdf文件,用PHP显示内容
- office文档在线预览 (doc、docx、ppt、pptx、xls、xlsx)
- VC 读取 doc,xls,ppt,pdf等格式的文件
- lucene实现pdf,doc,xls,ppt,htm,html等格式文件的检索