我需要在Android中将PDF文件(PDF页面)转换为位图(或图像文件)。
1. 使用Apache的Pdfbox jar,但它使用了一些在Android中不支持的Java类。 2. 尝试过将图像转换为PDF的Itext jar(我需要相反的操作)。 我已经尝试了许多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文件吗?