【问题标题】:Library to convert pdf to image将pdf转换为图像的库
【发布时间】:2013-10-25 00:02:10
【问题描述】:

我有一个 Android 应用程序,我想在其中显示一个 PDF(没有外部应用程序)。我认为唯一的解决方案是将 PDF 的页面转换为图像。有人有这个问题的经验吗?你推荐我哪个图书馆?

我测试了下一个库,但遇到了麻烦:

1) PdfRenderer library 不适用于现代 PDF
2) jPDFImages 在 Android 中不工作(在 java 桌面应用程序中工作)

对不起我的英语

【问题讨论】:

  • 你可以看一下iText pdf,只要你的产品/服务不收费,它是免费的
  • 也检查这个库:code.google.com/p/apv
  • @ns47731 免费版 iText 在 AGPL 下获得许可,允许收费,但它也需要与它一起分发的代码以 GPL 或 AGPL 的形式获得许可。

标签: java android image pdf


【解决方案1】:

我正在扩展公认的答案并提供完整的解决方案。

使用此库:android-pdfview 和以下代码,您可以可靠地将 PDF 页面转换为图像(JPG、PNG):

DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(mContext.getContentResolver());

// a bit long running
decodeService.open(Uri.fromFile(pdf));

int pageCount = decodeService.getPageCount();
for (int i = 0; i < pageCount; i++) {
    PdfPage page = decodeService.getPage(i);
    RectF rectF = new RectF(0, 0, 1, 1);

    // do a fit center to 1920x1080
    double scaleBy = Math.min(AndroidUtils.PHOTO_WIDTH_PIXELS / (double) page.getWidth(), //
            AndroidUtils.PHOTO_HEIGHT_PIXELS / (double) page.getHeight());
    int with = (int) (page.getWidth() * scaleBy);
    int height = (int) (page.getHeight() * scaleBy);

    // you can change these values as you to zoom in/out
    // and even distort (scale without maintaining the aspect ratio)
    // the resulting images

    // Long running
    Bitmap bitmap = page.renderBitmap(with, height, rectF);

    try {
        File outputFile = new File(mOutputDir, System.currentTimeMillis() + FileUtils.DOT_JPEG);
        FileOutputStream outputStream = new FileOutputStream(outputFile);

        // a bit long running
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

        outputStream.close();
    } catch (IOException e) {
        LogWrapper.fatalError(e);
    }
}

您应该在后台执行此工作,即使用AsyncTask 或类似的东西,因为很多方法需要计算或 IO 时间(我已在 cmets 中标记它们)。

【讨论】:

    【解决方案2】:

    我推荐使用这个库,android-pdfview

    【讨论】:

      【解决方案3】:

      您可以使用MuPDF。这是一个描述如何为 android 构建 MuPDF 的链接:http://mupdf.com/docs/how-to-build-mupdf-for-android

      【讨论】:

        猜你喜欢
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 2016-09-14
        • 2012-03-31
        • 1970-01-01
        相关资源
        最近更新 更多