1

我正在尝试将 pdf 页面转换为图像。我正在使用 PdfViewer.jar 文件。

听到是我的代码,我想将转换后的图像显示为 imageview

package com.info.org;

import java.io.File;
import java.io.FileInputStream;

import net.sf.andpdf.nio.ByteBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.widget.ImageView;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class PDFImageReadActivity extends Activity {
    /** Called when the activity is first created. */

    ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            byte[] bytes;
            setContentView(R.layout.main);

            imageView=(ImageView) findViewById(R.id.img);

            File file = new File(Environment.getExternalStorageDirectory()+"/infobooks/Book5.pdf");
            if (file.exists()) {

                FileInputStream is = new FileInputStream(file);
                long length = file.length();
                bytes = new byte[(int) length];
                int offset = 0;
                int numRead = 0;
                while (offset < bytes.length
                        && (numRead = is.read(bytes, offset, bytes.length
                                - offset)) >= 0) {
                    offset += numRead;
                }

                ByteBuffer buffer = ByteBuffer.NEW(bytes);
                String data = Base64.encodeToString(bytes, Base64.DEFAULT);
                PDFFile pdf_file = new PDFFile(buffer);
                PDFPage page = pdf_file.getPage(1);

                RectF rect = new RectF(0, 0, (int) page.getBBox().width(),(int) page.getBBox().height());

                Bitmap image = page.getImage((int) rect.width(),(int) rect.height(), rect);
                imageView.setImageBitmap(image);

                //FileOutputStream os = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/pdfImage.jpg");
                //image.compress(Bitmap.CompressFormat.JPEG, 80, os);

            } else {
                System.err.println("File is not found" + file.getAbsolutePath());
            }

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("Error in main -->>" + e.toString());
        }

    }
}

当我运行它时在 logcat 中显示错误

06-28 18:12:40.353: E/APV.PDFRenderer(852): do not show images

06-28 18:12:40.353: E/APV.PDFRenderer(852): java.lang.UnsupportedOperationException: do not show images

06-28 18:12:40.353: E/APV.PDFRenderer(852):     at com.sun.pdfview.PDFImage.getImage(PDFImage.java:223)

06-28 18:12:40.353: E/APV.PDFRenderer(852):     at com.sun.pdfview.PDFRenderer.drawImage(PDFRenderer.java:374)

06-28 18:12:40.353: E/APV.PDFRenderer(852):     at com.sun.pdfview.PDFImageCmd.execute(PDFPage.java:654)

06-28 18:12:40.353: E/APV.PDFRenderer(852):     at com.sun.pdfview.PDFRenderer.iterate(PDFRenderer.java:674)

06-28 18:12:40.353: E/APV.PDFRenderer(852):     at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)

06-28 18:12:40.353: E/APV.PDFRenderer(852):     at java.lang.Thread.run(Thread.java:1096)

谁能看到问题?

这段代码来自这篇文章。 需要帮助将 Pdf 页面转换为 Android Java 中的位图

4

1 回答 1

0

您可以使用 GIMP。它是一个非常好的界面。

将 pdf 文件导入 GIMP 将文件另存为 .xcf 对图像进行操作 - 如果需要,进行编辑 以您想要的任何格式导出:.bmp、.jpeg、.png 等

在这篇文章中回答:PDF 到图像转换器

于 2013-10-24T11:20:15.343 回答