Android中的PDFBox或其他方式从设备上的PDF中提取文本?



我的应用程序需要处理来自由文本组成的PDF文件的输入(大部分(。我可以在我的服务器上进行解析,但我不想这样做。无论如何,在探索了我的文本提取选项之后,我找到了PDFBox库及其与Android一起使用的端口(https://github.com/TomRoush/PdfBox-Android(

在应用程序中,我向用户展示了一个标准 UI,用于通过ACTION_OPEN_DOCUMENT选择源文档。然后覆盖onActivityResult以获取Uri - 你知道,通常的东西。

问题是我不知道如何将其提供给PDFBox。因为我们谈论的不是"文件",而是"文档",并且 lib 想要一个真实的文件路径。如果我为某个文件提供它,文本解析就可以了,但这肯定不是最佳实践,也不能对所有文档(云存储等(都这样做,所以我这样做:

InputStream inputStream = getContentResolver().openInputStream(uri);

然后逐行阅读,以便最终我可以将其全部放在一个字符串中。显然,它工作正常。

但是如何将这些数据实际输入到PDFBox中以执行其文本提取魔术呢?当我没有"真实文件路径"时,我找不到任何关于如何在场景中执行此操作的文档。

也许现在有更好的方法?这个图书馆很旧。基本上,我需要从PDF中提取文本并在Android设备上进行操作,而不是通过API调用。真的被困在这里了。

我的应用程序需要类似的功能,所以我尝试了 Mike M. 在您的问题下的评论中建议的解决方案,它对我来说效果很好(所以这真的是他的答案——我只是确认它有效并提供代码(。 希望对您有所帮助。

"魔力"其实就在这两行:

InputStream inputStream = this.getContentResolver().openInputStream(fileUri);
document = PDDocument.load(inputStream);

但是对于某些上下文(以及那些将在另一个场合搜索此问题答案的人(,以下是整个示例代码:

public class MainActivity extends AppCompatActivity {
private static final int OPEN_FILE_REQUEST_CODE = 1;
Intent intentOpenfile;
Uri fileUri;
TextView tvTextDisplay;
Button bOpenFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTextDisplay = findViewById(R.id.tv_text_display);
PDFBoxResourceLoader.init(getApplicationContext());
bOpenFile = findViewById(R.id.b_open_file);
bOpenFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intentOpenfile = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intentOpenfile.setType("application/pdf");
startActivityForResult(intentOpenfile, OPEN_FILE_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OPEN_FILE_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
fileUri = data.getData();
PDDocument document = null;
String parsedText = null;
try {
InputStream inputStream = this.getContentResolver().openInputStream(fileUri);
document = PDDocument.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(1);
parsedText = "Parsed text: " + pdfStripper.getText(document);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (document != null) document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
tvTextDisplay.setText(parsedText);
}
}
}
}

最新更新