【问题标题】:Android compatible library for creating image from pdf file?用于从 pdf 文件创建图像的 Android 兼容库?
【发布时间】:2011-07-31 00:45:57
【问题描述】:

是否有任何兼容的 android 库可以将 pdf 页面转换为图像?我找到了几个适合我需要的 java 库(pdfbox、jpod、jpedal、icepdf),但它们给了我编译错误(基于 awT 或 swt)。我目前正在android上制作pdf查看器,如果我不必从头开始编写pdf解码,我会很棒。

【问题讨论】:

标签: android image pdf


【解决方案1】:

你最好的选择是这个库:http://code.google.com/p/apv/

这是一个 C 项目,因此您需要使用 JNI 调用。

【讨论】:

  • 这是基于 MuPDF 的,它是 AGPL 许可证,与商业软件不完全兼容。仅供参考...
【解决方案2】:

感谢您的帮助。我检查了项目并使用了MuPDF 库(APV 项目所基于的库)。它工作正常!非常感谢,上帝保佑。

【讨论】:

    【解决方案3】:

    使用itextpdf5.3.1.jar

    这是从pdf中提取图像的代码:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);`enter code here`
        PdfReader reader;
    
        File file = new File("/sdcard/vineeth/anni.prc");
        try{
            reader = new PdfReader(file.getAbsolutePath());
    
            for (int i = 0; i < reader.getXrefSize(); i++) {
                PdfObject pdfobj= reader.getPdfObject(i);
                if (pdfobj == null || !pdfobj.isStream()) {
                    continue;
                }
    
                PdfStream stream = (PdfStream) pdfobj;
                PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
    
                if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
                    byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream);
                    FileOutputStream out = new FileOutputStream(new 
                    File(file.getParentFile(),String.format("%1$05d", i) + ".jpg"));
                    out.write(img); out.flush(); out.close();
                }
            }
        } catch (Exception e) { }
    }
    

    【讨论】:

    • 以上代码仅提取 pdf中包含的图片,不会生成pdf文件的图片。
    【解决方案4】:

    以上答案都没有帮助我,所以我正在编写我的解决方案。

    使用这个库: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 中标记它们)。

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      相关资源
      最近更新 更多