JAVA讀取PDF內容
package com.pdfcom;
import java.io.IOException;
import java.net.URL;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
/**
*
* @author hp
*讀取pdf指定內容
*/
public class TestAll {
public static void main(String[] args) throws IOException {
URL url=new URL("file:/C:\\Users\\hp\\Desktop\\新建資料夾 (2)\\國網京峽ECI光傳輸系統500kV保北站XDM1000裝置停運三措一案.pdf");
readPdf(url);//直接讀全PDF面
}
public static void readPdf(URL url){
String pageContent = "";
try {
PdfReader reader = new PdfReader(url);
int pageNum = reader.getNumberOfPages();
for(int i=1;i<=pageNum;i++){
pageContent += PdfTextExtractor.getTextFromPage(reader, i);//讀取第i頁的文件內容
}
System.out.println(pageContent);
} catch (Exception e) {
e.printStackTrace();
}finally{
}
}
}