`
玛儿.
  • 浏览: 13436 次
  • 性别: Icon_minigender_2
文章分类
社区版块
存档分类
最新评论

jacob操作word文档,word转txt,pdf,jpg....

阅读更多
关于jacob用法,

1、jacob的安装配置
与处理其他jar文件一样,将jacob.jar文件加载到classpath中


如果是你的系统是32位的,再将jacob-1.17-M2-x64.dll文件(复制到%JAVA_HOME%\jdk1.7.0_04\jre\bin下; 当然,如果你是64位系统,要复制jacob-1.17-M2-x86.dll文件


2、一个具体的代码示例:


[java]
package ccnu; 
import com.jacob.com.*; 
import com.jacob.activeX.*; 
import java.io.*; 
 
public class testCoding 

    /*
     *
     *
     * 程序功能:调用jacob包,在Microsoft Office 能够支持打开的文件类型中随意进行格式转换(本程序不是批量转换,一次只能转单个文件)。
     * 由于我电脑上安装的是Office 2013,所以甚至可以实现pdf与txt!用起来很方便,除了注释 代码不算长吧?
     * 
     * */ 
    public static void main(String[] args) 
    { 
        //指定被转换文件的完整路径。 我这里的意图是把word转为pdf  
        String path = new String("E:\\test.word"); 
        //根据路径创建文件对象  
        File docFile=new File(path); 
        //获取文件名(包含扩展名)  
        String filename=docFile.getName(); 
        //过滤掉文件名中的扩展名  
        int filenamelength=filename.length(); 
        int dotposition=filename.indexOf("."); 
        filename=filename.substring(0,dotposition); 
         
        //设置输出路径,一定要包含输出文件名(不含输出文件的扩展名)  
        String savepath = new String ("E:\\"+filename);   
         
        //启动Word程序  
        ActiveXComponent app = new ActiveXComponent("Word.Application");         
        //接收输入文件和输出文件的路径  
        String inFile = path; 
        String tpFile = savepath; 
        //设置word不可见  
        app.setProperty("Visible", new Variant(false)); 
        //这句不懂  
        Object docs = app.getProperty("Documents").toDispatch(); 
        //打开输入的doc文档  
        Object doc = Dispatch.invoke((Dispatch) docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); 
         
        //另存文件, 其中Variant(n)参数指定另存为的文件类型,详见代码结束后的文字  
        Dispatch.invoke((Dispatch) doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(17)}, new int[1]); 
        //这句也不懂  
        Variant f = new Variant(false); 
        //关闭并退出  
        Dispatch.call((Dispatch) doc, "Close", f); 
        app.invoke("Quit", new Variant[] {}); 
        System.out.println("转换完毕。"); 
    } 
 


        *其中第44行中的 invoke()函数中的Variant(n)参数指定另存为的文件类型(n的取值范围是0-25),他们分别是:
        *Variant(0):doc
        *Variant(1):dot
        *Variant(2-5),Variant(7):txt
        *Variant(6):rft
        *Variant(8),Variant(10):htm
        *Variant(9):mht
        *Variant(11),Variant(19-22):xml
        *Variant(12):docx
        *Variant(13):docm
        *Variant(14):dotx
        *Variant(15):dotm
        *Variant(16)、Variant(24):docx
        *Variant(17):pdf
        *Variant(18):xps
        *Variant(23):odt
        *Variant(25):与Office2003与2007的转换程序相关,执行本程序后弹出一个警告框说是需要更高版本的 Microsoft Works Converter


因为我是想把word转为图片,但是这里面没有找到可以转图片的方式,所以我选择了先把word转为pdf,再将pdf转jpg,附上代码



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.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 
import javax.swing.SwingUtilities; 
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 PdfChange { 
    public static void setup(String pdfPath,String imgPath) throws IOException { 
 
        File file = new File( 
                pdfPath); 
        RandomAccessFile raf = new RandomAccessFile(file, "r"); 
        FileChannel channel = raf.getChannel(); 
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel 
                .size()); 
        PDFFile pdffile = new PDFFile(buf); 
 
        System.out.println("页数: " + pdffile.getNumPages()); 
 
        for (int i = 1; i <= pdffile.getNumPages(); i++) { 
            // draw the first page to an image 
            PDFPage page = pdffile.getPage(i); 
 
            // get the width and height for the doc at the default zoom 
            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, 0, 0, rect.width, rect.height, 
                    null); 
            FileOutputStream out = new FileOutputStream( 
                    imgPath 
                            + i + ".jpg"); // 输出到文件流 
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
            encoder.encode(tag); // JPEG编码 
 
            out.close(); 
        } 
 
      
    } 
 
    public static void main(final String[] args) { 
        SwingUtilities.invokeLater(new Runnable() { 
            public void run() { 
                try { 
                PdfChange.setup("E:\\aa.pdf","E:\\"); 
                } catch (IOException ex) { 
                    ex.printStackTrace(); 
                } 
            } 
        }); 
    } 
 
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics