【发布时间】:2012-02-07 13:01:14
【问题描述】:
我需要在 Android 中将 PDF 文件(PDF 页面)转换为位图(或图像文件)。
1.使用 Apache 的 Pdfbox jar。但它使用了一些 android 不支持的 java 类。 2.尝试了将图像转换为pdf的Itext jar(我需要它的反向操作) 像这样,我尝试了很多罐子。但没有阳性结果。
byte[] bytes;
try {
File file = new File(this.getFilesDir().getAbsolutePath()+"/2010Q2_SDK_Overview.pdf");
FileInputStream is = new FileInputStream(file);
// Get the size of the 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(2);
RectF rect = new RectF(0, 0, (int) page.getBBox().width(),
(int) page.getBBox().height());
// Bitmap bufferedImage = Bitmap.createBitmap((int)rect.width(), (int)rect.height(),
// Bitmap.Config.ARGB_8888);
Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect);
FileOutputStream os = new FileOutputStream(this.getFilesDir().getAbsolutePath()+"/pdf.jpg");
image.compress(Bitmap.CompressFormat.JPEG, 80, os);
// ((ImageView) findViewById(R.id.testView)).setImageBitmap(image);
我正在获取图像文件,
而不是,
package com.test123;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import net.sf.andpdf.nio.ByteBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Base64;
public class Test123Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
byte[] bytes;
try {
File file = new File(this.getFilesDir().getAbsolutePath()+"/2010Q2_SDK_Overview.pdf");
FileInputStream is = new FileInputStream(file);
// Get the size of the 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(2);
RectF rect = new RectF(0, 0, (int) page.getBBox().width(),
(int) page.getBBox().height());
// Bitmap bufferedImage = Bitmap.createBitmap((int)rect.width(), (int)rect.height(),
// Bitmap.Config.ARGB_8888);
Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect);
FileOutputStream os = new FileOutputStream(this.getFilesDir().getAbsolutePath()+"/pdf.jpg");
image.compress(Bitmap.CompressFormat.JPEG, 80, os);
//((ImageView) findViewById(R.id.testView)).setImageBitmap(image);
} catch (Exception e) {
e.printStackTrace();
}
}
}
另外,还有其他方法可以使用应用程序内置的功能在 android 中显示 pdf 文件吗?
【问题讨论】:
-
我们可以使用java中的awt工具将pdf转换为图像。但是android不支持awt。我也在使用itext..如果你知道python你可以使用ghostscript将pdf转换为位图。
-
我不知道 python.. 有没有办法在 java 中做到这一点?
-
请看上面的代码。我已经使用了链接中的 jar,github.com/jblough/Android-Pdf-Viewer-Library/blob/master/… 我可以将 PDFpage 转换为 jpg 文件。但是转换后的图像是部分转换的。我不知道我错在哪里?
-
你给出的代码是什么?这行得通吗?
-
它在某种意义上是有效的,我得到的是第一张图片而不是第二张。