首先會需要兩個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轉爲文本文檔的過程。其他類型的轉化和細節使用,
待以後繼續測試使用後再來分享經驗