首先会需要两个jar包文件 itext-5.5.5和text-asian.jar,可以在我上传的资源处下载。
将这两个jar包添加到项目中。
然后编写简单的测试Demo
/** * */ package com.skd.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.util.PDFTextStripper; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; /** * @author JING * @date 2015年4月28日 * @time 下午9:11:14 * @fileName Office2PDF.java * @function */ public class Office2PDF { static final String PDF_SUFFIX=".PDF"; static final String TXT_SUFFIX=".txt"; static final String JAVA_SUFFIX=".java"; static final String DOCX_SUFFIX=".docx"; static final String DOC_SUFFIX=".doc"; /** * @param args * @throws Exception * @throws IOException */ public static void main(String[] args) throws IOException, Exception { //这两个路径可以指定为自己的文本文件路径,不带扩展名 String filePath="D:\\test_data\\开发"; String pdfPath=filePath+PDF_SUFFIX; String txtPath=filePath+JAVA_SUFFIX; //文本文件转为pdf文件 //txt2Pdf(txtPath, pdfPath); //获取pdf文件中的内容并保存在同名的文本文件中 //getPdf(pdfPath); System.out.println("结束"); } /** * 获取pdf文件中的内容并保存在同名的文本文件中 * @param pdfPath * @throws IOException */ public static void getPdf(String pdfPath) throws IOException { //是否排序 boolean sort=false; //pdf文件名 String fileName=pdfPath; //读取文件的内容 String pdfContent=null; //编码方式 String encoding="UTF-8"; //开始提取页 int startPage=1; //结束提取页 int endPage=Integer.MAX_VALUE; //文件输入流 Writer writer=null; PDDocument doc=null; doc=PDDocument.load(fileName); if(fileName.length() > 4){ //以原来pdf名称来命名新产生的txt文件 File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt"); fileName = outputFile.getPath(); } //文件输出流,写入到filename中 writer=new OutputStreamWriter(new FileOutputStream(fileName), encoding); PDFTextStripper pdfTextStripper = new PDFTextStripper(); pdfTextStripper.setSortByPosition(sort); pdfTextStripper.setStartPage(startPage); pdfTextStripper.setEndPage(endPage); //调用PDFTextStripper的writeText pdfTextStripper.writeText(doc,writer ); writer.close(); if(writer != null){ } doc.close(); if(doc != null){ } } /** * 文本文件转为pdf文件 * @param txtPath * @param pdfPath * @throws IOException * @throws DocumentException */ public static void txt2Pdf(String txtPath,String pdfPath) throws IOException, DocumentException { Document document = new Document(); //系统字体的路径C:\Windows\Fonts\SIMKAI.TTF 楷体常规 放置在项目src下的res下。不设置字体不显示中文 //BaseFont bfChinese = BaseFont.createFont("res/SIMKAI.TTF",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //"fonts/UniGB-UCS2-H" BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.EMBEDDED); Font FontChinese = new Font(bfChinese, 12,Font.NORMAL); try { BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(txtPath))); OutputStream os=new FileOutputStream(pdfPath); PdfWriter.getInstance(document, os); document.open(); String cache=null; while((cache=reader.readLine())!=null){ document.add(new Paragraph(cache, FontChinese)); } } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ document.close(); } } }
上面代码中的两个方法就是文本文档转为pdf,然后pdf转为文本文档的过程。其他类型的转化和细节使用,
待以后继续测试使用后再来分享经验