1

私は Android アプリケーションを持っていて、それに PDF を表示したいと考えています (外部アプリケーションなし)。私が考えた唯一の解決策は、PDFのページを画像に変換することです。誰かがその問題の経験がありますか? どのライブラリをお勧めしますか?

次のライブラリをテストしましたが、問題がありました。

1) PdfRenderer ライブラリは最新の PDF では機能しません
2) jPDFImages は Android では機能しません (Java デスクトップ アプリケーションで機能します)

私の英語でごめんなさい

4

3 に答える 3

2

受け入れられた回答を拡張し、完全なソリューションを提供しています。

このライブラリ: 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 時間を要するため、または類似のメソッドを使用して実行する必要があります (コメントでマークしました)。

于 2016-01-14T08:37:42.960 に答える
0

MuPDFを使用できます。Android 用の MuPDF をビルドする方法を説明するリンクは次のとおりです: http://mupdf.com/docs/how-to-build-mupdf-for-android

于 2013-10-16T18:38:23.917 に答える
0

このライブラリ、 android-pdfviewを使用することをお勧めします

于 2013-10-18T18:51:38.170 に答える