java操作word,pdf的四种武器

很多人问到如何抽取word,excel,pdf阿。这里我总结一下抽取word,pdf的
几种方法。
    1。用jacob.
    其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。
   jacob下载:
http://www.matrix.org.cn/down_view.asp?id=13
    下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;

public class FileExtracter{

 public static void main(String[] args) {

  ActiveXComponent app = new ActiveXComponent("Word.Application");
  String inFile = "c://test.doc";
 String tpFile = "c://temp.htm";
  String otFile = "c://temp.xml";
  boolean flag = false;
  try {
   app.setProperty("Visible", new Variant(false));
   Object docs = app.getProperty("document.").toDispatch();
   Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
   Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
   flag = true;
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   app.invoke("Quit", new Variant[] {});
  }

 }
}
    2。用apache的poi来抽取word,excel。
    poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:
    下载经过封装后的poi包:
http://www.matrix.org.cn/down_view.asp?id=14
    下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
   import java.io.*;
import  org.textmining.text.extraction.WordExtractor;
/**
 * <p>Title: pdf extraction</p>
 * <p>Description: email:chris@matrix.org.cn</p>
 * <p>Copyright: Matrix Copyright (c) 2003</p>
 * <p>Company: Matrix.org.cn</p>
 * @author chris
 * @version 1.0,who use this example pls remain the declare
 */

public class PdfExtractor {
  public PdfExtractor() {
  }
  public static void main(String args[]) throws Exception
  {
  FileInputStream in = new FileInputStream ("c://a.doc");
  WordExtractor extractor = new WordExtractor();
  String str = extractor.extractText(in);
  System.out.println("the result length is"+str.length());
   System.out.println("the result is"+str);
}
}
    
    3。pdfbox-用来抽取pdf文件
   但是pdfbox对中文支持还不好,先下载pdfbox:
http://www.matrix.org.cn/down_view.asp?id=12
下面是一个如何使用pdfbox抽取pdf文件的例子:
import org.pdfbox.pdmodel.PDdocument.
import org.pdfbox.pdfparser.PDFParser;
import java.io.*;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
 * <p>Title: pdf extraction</p>
 * <p>Description: email:chris@matrix.org.cn</p>
 * <p>Copyright: Matrix Copyright (c) 2003</p>
 * <p>Company: Matrix.org.cn</p>
 * @author chris
 * @version 1.0,who use this example pls remain the declare
 */

public class PdfExtracter{

public PdfExtracter(){
  }
public String GetTextFromPdf(String filename) throws Exception
  {
  String temp=null;
  PDdocument.nbsppdfdocument.null;
  FileInputStream is=new FileInputStream(filename);
  PDFParser parser = new PDFParser( is );
  parser.parse();
  pdfdocument.nbsp= parser.getPDdocument.);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  OutputStreamWriter writer = new OutputStreamWriter( out );
  PDFTextStripper stripper = new PDFTextStripper();
  stripper.writeText(pdfdocument.getdocument.), writer );
  writer.close();
  byte[] contents = out.toByteArray();

  String ts=new String(contents);
  System.out.println("the string length is"+contents.length+"/n");
  return ts;
}
public static void main(String args[])
{
PdfExtracter pf=new PdfExtracter();
PDdocument.nbsppdfdocument.nbsp= null;

try{
String ts=pf.GetTextFromPdf("c://a.pdf");
System.out.println(ts);
}
catch(Exception e)
  {
  e.printStackTrace();
  }
}

}
 
     4.抽取支持中文的pdf文件-xpdf
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。
下载xpdf函数包:
http://www.matrix.org.cn/down_view.asp?id=15
同时需要下载支持中文的补丁包: http://www.matrix.org.cn/down_view.asp?id=16
按照readme放好中文的patch,就可以开始写调用本地方法的java程序了
下面是一个如何调用的例子:
import java.io.*;
/**
 * <p>Title: pdf extraction</p>
 * <p>Description: email:chris@matrix.org.cn</p>
 * <p>Copyright: Matrix Copyright (c) 2003</p>
 * <p>Company: Matrix.org.cn</p>
 * @author chris
 * @version 1.0,who use this example pls remain the declare
 */


public class PdfWin {
  public PdfWin() {
  }
  public static void main(String args[]) throws Exception
  {
    String PATH_TO_XPDF="C://Program Files//xpdf//pdftotext.exe";
    String filename="c://a.pdf";
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
    Process p = Runtime.getRuntime().exec(cmd);
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
    StringWriter out = new StringWriter();
    char [] buf = new char[10000];
    int len;
    while((len = reader.read(buf))>= 0) {
    //out.write(buf, 0, len);
    System.out.println("the length is"+len);
    }
    reader.close();
    String ts=new String(buf);
    System.out.println("the str is"+ts);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用Apache POI和iText库来实现WordPDF的功能。 1. Apache POI是一个用于读写Microsoft Office格式文件的Java库。它提供了一组API,可以操作Word文档。要将Word文档转换为PDF,可以使用Apache POI读取Word文档的内容,并将其写入PDF文件中。 2. iText是一个用于创建和操作PDF文件的Java库。它提供了一组API,可以创建、修改和转换PDF文件。要将Word文档转换为PDF,可以使用iText库创建一个空白的PDF文件,然后使用Apache POI读取Word文档的内容,并将其写入PDF文件中。 下面是一个使用Apache POI和iText库将Word文档转换为PDF的示例代码: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileInputStream; import java.io.FileOutputStream; public class WordToPdfConverter { public static void main(String[] args) { String wordFilePath = "path/to/word/document.docx"; String pdfFilePath = "path/to/pdf/document.pdf"; try { // 读取Word文档 XWPFDocument document = new XWPFDocument(new FileInputStream(wordFilePath)); // 创建PDF文档 Document pdfDocument = new Document(); PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdfFilePath)); pdfDocument.open(); // 逐段读取Word文档内容,并写入PDF文档 for (XWPFParagraph paragraph : document.getParagraphs()) { String text = paragraph.getText(); pdfDocument.add(new Paragraph(text)); } // 关闭PDF文档 pdfDocument.close(); System.out.println("WordPDF成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,上述示例代码中的`wordFilePath`和`pdfFilePath`需要替换为实际的Word文件路径和要保存的PDF文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值