本文目錄一覽:
- 1、java 如何讀取PDF文件內容
- 2、如何用java讀取pdf文檔的部分內容
- 3、怎麼用java讀取pdf文件內容
- 4、用Java 讀取 PDF 遇到中文標籤該怎麼處理
- 5、怎麼用java讀取pdf中的表格
java 如何讀取PDF文件內容
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 輸入文本文件名稱
String textFile = null;
// 編碼方式
String encoding = “UTF-8”;
// 開始提取頁數
int startPage = 1;
// 結束提取頁數
int endPage = Integer.MAX_VALUE;
// 文件輸入流,生成文本文件
Writer output = null;
// 內存中存儲的PDF Document
PDDocument document = null;
try {
try {
// 首先當作一個URL來裝載文件,如果得到異常再從本地文件系統//去裝載文件
URL url = new URL(pdfFile);
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 獲取PDF的文件名
String fileName = url.getFile();
// 以原來PDF的名稱來命名新產生的txt文件
if (fileName.length() 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() – 4)
+ “.txt”);
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作為URL裝載得到異常則從文件系統裝載
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() 4) {
textFile = pdfFile.substring(0, pdfFile.length() – 4)
+ “.txt”;
}
}
// 文件輸入流,寫入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper來提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 設置是否排序
stripper.setSortByPosition(sort);
// 設置起始頁
stripper.setStartPage(startPage);
// 設置結束頁
stripper.setEndPage(endPage);
// 調用PDFTextStripper的writeText提取並輸出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 關閉輸出流
output.close();
}
if (document != null) {
// 關閉PDF Document
document.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PdfReader pdfReader = new PdfReader();
try {
// 取得E盤下的SpringGuide.pdf的內容
pdfReader.readFdf(“E://SpringGuide.pdf”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如何用java讀取pdf文檔的部分內容
你需要用到PDFbox api
例子如下
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
try {
PDDocument document = null;
document = PDDocument.load(new File(“test.pdf”));
document.getClass();
if (!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper Tstripper = new PDFTextStripper();
String st = Tstripper.getText(document);
System.out.println(“Text:” + st);
}
} catch (Exception e) {
e.printStackTrace();
}
怎麼用java讀取pdf文件內容
你可以把pdf轉成word在進行讀取
推薦使用轉轉大師pdf轉word轉換器,免費的在線工具
百度搜索下,在線免費轉換就行了,不用下載註冊,很方便
用Java 讀取 PDF 遇到中文標籤該怎麼處理
直接使用系統字體讀取或創建帶中文的pdf,需要注意jar的版本。
dependency
groupIdcom.itextpdf/groupId
artifactIditextpdf/artifactId
version5.5.8/version
/dependency
dependency
groupIdcom.itextpdf/groupId
artifactIditext-asian/artifactId
version5.2.0/version
/dependency
dependency
groupIdcom.itextpdf.tool/groupId
artifactIdxmlworker/artifactId
version5.5.6/version
/dependency123456789101112131415
代碼如下,覆寫XMLWorkerFontProvider$getFont即可讀取中文
public void createPdf(String src, String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(src), null, new XMLWorkerFontProvider(){ public Font getFont(final String fontname, final String encoding,
final boolean embedded, final float size, final int style,
final BaseColor color) {
BaseFont bf = null;
try {
bf = BaseFont.createFont(“C:/Windows/Fonts/SIMYOU.TTF”,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
});
document.close();
}1234567891011121314151617181920212223
創建時,使用系統(windows下)的字體即可
BaseFont baseFont = BaseFont.createFont(“C:/Windows/Fonts/SIMYOU.TTF”,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
怎麼用java讀取pdf中的表格
ITEXT插件方法
/**
* @param pdf PDF文件路徑
* @param txt 輸出文本文件路徑
* @throws IOException
*/
public void parsePdf(String pdf, String txt) throws IOException {
PdfReader reader = new PdfReader(pdf);
PrintWriter out = new PrintWriter(new FileOutputStream(txt));
Rectangle rect = new Rectangle(70, 80, 490, 580);
RenderFilter filter = new RegionTextRenderFilter(rect);
TextExtractionStrategy strategy;
for (int i = 1; i = reader.getNumberOfPages(); i++) {
strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
out.println(PdfTextExtractor.getTextFromPage(reader, i, strategy));
}
out.flush();
out.close();
reader.close();
}
PDFBOX插件方法
PDDocument document = PDDocument.load( args[0] );
if( document.isEncrypted() )
{
document.decrypt( “” );
}
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition( true );
Rectangle rect = new Rectangle( 10, 280, 275, 60 );
stripper.addRegion( “class1”, rect );
List allPages = document.getDocumentCatalog().getAllPages();
PDPage firstPage = (PDPage)allPages.get( 0 );
stripper.extractRegions( firstPage );
System.out.println( “Text in the area:” + rect );
System.out.println( stripper.getTextForRegion( “class1” ) );
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/195758.html