方法:1、文件轉換成pdf(采用openoffice或者jacob)
2、抓取pdf首頁圖
第一步:采用jacob:
a、下載jacob 注意區分32位,64位,否則不能用
將dll文件放在java bin目錄下即可
jar包引入項目
b、轉換pdf
package cn.bnsr.edu_yun.util;
import java.io.File;
import java.io.IOException;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestUtil {
static final int wdFormatPDF = 17;// PDF 格式
public void wordToPDF(String sfileName,String toFileName){
System.out.println("啟動Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
// doc = Dispatch.call(docs, "Open" , sourceFile).toDispatch();
doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
System.out.println("打開文檔..." + sfileName);
System.out.println("轉換文檔到PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
// Dispatch.call(doc, "SaveAs", destFile, 17);
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
toFileName, new Variant(17) }, new int[1]);
long end = System.currentTimeMillis();
System.out.println("轉換完成..用時:" + (end - start) + "ms.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文檔轉換失敗:" + e.getMessage());
} finally {
Dispatch.call(doc,"Close",false);
System.out.println("關閉文檔");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
//如果沒有這句話,winword.exe進程將不會關閉
ComThread.Release();
}
public static void main(String[] args) {
TestUtil d = new TestUtil();
d.wordToPDF("D:\\work\\1111.docx", "d:\\work\\1111.pdf");
}
}
第二步:抓取首頁圖:
package cn.bnsr.edu_yun.util;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfImageUtil {
// 將PDF格式的文件轉換為JPG格式的文件
public static void pdfToJPG(String inputFile)throws IOException {
// load a pdf from a byte buffer
File file = new File(inputFile);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
//這句代碼通道建立了map映射,如果要刪除file那么得接觸映射
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
int totalpage =pdffile.getNumPages();
for (int i = 1; i <= totalpage; i++) {
if (i == 1) {
// draw the first page to an image
// 以圖片的形式來描繪首頁
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
// generate the image
// 生成圖片
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img.getScaledInstance(rect.width, rect.height, Image.SCALE_SMOOTH), 0, 0, rect.width, rect.height,null);
FileOutputStream out = new FileOutputStream( "D:\\work\\s111"+ ".jpg"); // 輸出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG編碼
// 關閉輸出流
out.close();
break;
}
}
buf.clear();
channel.close();
raf.close();
unmap(buf);
file.delete();
}
//解除map映射
public static <T> void unmap(final Object buffer) {
AccessController.doPrivileged(new PrivilegedAction<T>(){
@Override
public T run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, new Object[0]);
cleaner.clean();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}
第三步、搞定,加入自己的業務邏輯,有問題歡迎提問。
