Android将PDF页面转换为图像错误:Java.io.ioexception:无法创建文档:不用PDF格式或损坏的文件
我正在开发一个使用此库从服务器接收到的PDF文档的应用:
https://github.com/barteksc/AndroidPdfViewer
一切正常,我可以使用以下输入流渲染PDF:
reportsViewModel.inputStreamResponse.observe(getViewLifecycleOwner(), _inputStream -> {
pdfView.fromStream(_inputStream).load();
progressDialog.dismiss();
PdiToImage.convert(requireContext(), _inputStream);
});
现在我想将每个PDF页面转换为图像,我尝试了类似的事情:
private static Bitmap bitmap = null;
public static Bitmap convert(Context context, InputStream inputStream) {
PdfiumCore pdfiumCore = new PdfiumCore(context);
try {
PdfDocument document = pdfiumCore.newDocument(new byte[inputStream.available()]);
final int pageCount = pdfiumCore.getPageCount(document);
for (int index = 0; index < pageCount; index++) {
pdfiumCore.openPage(document, index);
int width = pdfiumCore.getPageWidthPoint(document, index);
int height = pdfiumCore.getPageHeightPoint(document, index);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
pdfiumCore.renderPageBitmap(document, bitmap, index, 0, 0, width, height);
}
pdfiumCore.closeDocument(document);
} catch (IOException e) {
//TODO: Error: document is not pdf
e.printStackTrace();
}
return bitmap;
}
但是该代码引发了一个例外:
java.io.IOException: cannot create document: File not in PDF format or corrupted
我的文档是PDF,因为我将其上传到服务器中,我尝试使用不同的PDF尝试了很多次。同样,Github也有一个问题:
https://github.com/barteksc/AndroidPdfViewer/issues/175
但是尚未解决,不幸的是已关闭。我的问题是,我应该如何找到解决方案,或者我在代码上做错了什么。 感谢Adavance。
I'm developing an app to display pdf documents received from server using this library:
https://github.com/barteksc/AndroidPdfViewer
Everything works fine, I can render the pdf using input streams like below:
reportsViewModel.inputStreamResponse.observe(getViewLifecycleOwner(), _inputStream -> {
pdfView.fromStream(_inputStream).load();
progressDialog.dismiss();
PdiToImage.convert(requireContext(), _inputStream);
});
Now I want to convert each pdf pages to images, I tried something like this:
private static Bitmap bitmap = null;
public static Bitmap convert(Context context, InputStream inputStream) {
PdfiumCore pdfiumCore = new PdfiumCore(context);
try {
PdfDocument document = pdfiumCore.newDocument(new byte[inputStream.available()]);
final int pageCount = pdfiumCore.getPageCount(document);
for (int index = 0; index < pageCount; index++) {
pdfiumCore.openPage(document, index);
int width = pdfiumCore.getPageWidthPoint(document, index);
int height = pdfiumCore.getPageHeightPoint(document, index);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
pdfiumCore.renderPageBitmap(document, bitmap, index, 0, 0, width, height);
}
pdfiumCore.closeDocument(document);
} catch (IOException e) {
//TODO: Error: document is not pdf
e.printStackTrace();
}
return bitmap;
}
But the code throws an exception:
java.io.IOException: cannot create document: File not in PDF format or corrupted
My document is pdf because I uploaded it in server, I tried many times with different pdfs. Also there is an issue in the github about this problem:
https://github.com/barteksc/AndroidPdfViewer/issues/175
But it is not solved yet, and unfortunately is closed. My question is how should I approach to find a solution, or what I'm doing wrong on my code.
Thanks in adavance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论