2017-08-01 38 views
0

當我嘗試從pdf和doc等驅動器文件中讀取文本時,文本格式如下所示。從谷歌驅動器pdf和doc文件中讀取文本android

%PDF-1.3 
%âãÏÓ 
7 0 obj 
<</Linearized 1/L 7945/O 9/E 3524/N 1/T 7656/H [ 451 137]>> 
endobj 
13 0 obj 
<</DecodeParms<</Columns 4/Predictor 12>>/Filter/FlateDecode/ID[<4DC91A1875A6D707AEC203BB021C93A0><F6C92B368A8A13408457A1D395A37EB9>]/Index[7 21]/Info 6 0 R/Length 52/Prev 7657/Root 8 0 R/Size 28/Type/XRef/W[1 2 1]>>stream 
hÞbbd``b`² ±H0§ 6G ñå#Ã4,#¹ÄÆ_L¤ð 
endstream 
endobj 
startxref 
0 
%%EOF 
27 0 obj 
<</Filter/FlateDecode/I 69/Length 58/S 38>>stream 
hÞb```a``üÏÜ6¨¬$ËѬ,ÆÅ 
ÜÜÓt&¬8ÌZ-R,¿â 
endstream 
endobj 
8 0 obj 
<</Metadata 1 0 R/Pages 5 0 R/Type/Catalog>> 
endobj 
9 0 obj 
<</Contents 11 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 5 0 R/Resources 14 0 R/Rotate 0/Type/Page>> 
endobj 

請找到下面的代碼:

DriveContents contents = result.getDriveContents(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));  
StringBuilder builder = new StringBuilder(); 
String line; 
while ((line = reader.readLine()) != null) { 
    builder.append(line); 
} 
String contentsAsString = builder.toString(); 

但實際上,我需要在原始格式文本here

請幫我在這。

+2

PDF文件不是純文本文件,並且您的代碼僅適用於純文本文件。查找理解PDF文件格式的PDF庫(例如iText)。 – CommonsWare

回答

0

除了Commonsware的說法,如果您只是想查看pdf文件,您可以通過Files.get使用該文件的webViewLink。但是如果你真的想轉換PDF文件並「提取」文本,那麼你需要一些類似於SO post中提到的類型的Android OCR轉換庫,例如Tesseract

相關問題