Android将PDF页面转换为图像错误:Java.io.ioexception:无法创建文档:不用PDF格式或损坏的文件

发布于 2025-01-30 16:24:00 字数 1834 浏览 1 评论 0原文

我正在开发一个使用此库从服务器接收到的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文