最终我找到了解决方案。我们不能在Android中使用awt,因此最终选择了http://sourceforge.net/projects/andpdf/files/上的pdf查看器库。
找到apk以及将pdf转换为图像的源代码。
这是我的类来将pdf转换为图像:
package com.print;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import net.sf.andpdf.nio.ByteBuffer;
import net.sf.andpdf.refs.HardReference;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFImage;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFPaint;
import com.sun.pdfview.decrypt.PDFAuthenticationFailureException;
import com.sun.pdfview.font.PDFFont;
public class Transformer extends Activity{
private static final int STARTPAGE = 1;
private static final float STARTZOOM = 1.0f;
private static final String TAG = "PDFVIEWER";
Bitmap _bitmap=null;
private String pdffilename;
private PDFFile mPdfFile;
private int mPage;
private float mZoom;
private File mTmpFile;
private PDFPage mPdfPage;
private Thread backgroundThread;
private Handler uiHandler;
ImageView myimg;
TextView command;
Context context;
ProgressDialog _p=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trans);
context=Transformer.this;
uiHandler = new Handler();
initContrils();
}
private void initContrils() {
boolean showImages = getIntent().getBooleanExtra(printDemo.EXTRA_SHOWIMAGES, true);
PDFImage.sShowImages = showImages;
boolean antiAlias = getIntent().getBooleanExtra(printDemo.EXTRA_ANTIALIAS, true);
PDFPaint.s_doAntiAlias = antiAlias;
boolean useFontSubstitution = getIntent().getBooleanExtra(printDemo.EXTRA_USEFONTSUBSTITUTION, false);
PDFFont.sUseFontSubstitution= useFontSubstitution;
boolean keepCaches = getIntent().getBooleanExtra(printDemo.EXTRA_KEEPCACHES, false);
HardReference.sKeepCaches= keepCaches;
if (this.getIntent() != null) {
pdffilename = getIntent().getStringExtra(printDemo.EXTRA_PDFFILENAME);
}
mPage = STARTPAGE;
mZoom = STARTZOOM;
myimg=(ImageView)findViewById(R.id.imageView1);
command=(TextView)findViewById(R.id.textView1);
_p=new ProgressDialog(context);
_p.setMessage("Converting........");
_p.setCancelable(false);
_p.show();
setContent();
}
private void setContent() {
try {
parsePDF(pdffilename);
startRenderThread(mPage, mZoom);
}
catch (PDFAuthenticationFailureException e) {
Log.e("Amit...","Error....");
}
}
private synchronized void startRenderThread(final int page, final float zoom) {
Log.e("Amit","renderrar");
if (backgroundThread != null)
return;
backgroundThread = new Thread(new Runnable() {
@Override
public void run() {
try {
if (mPdfFile != null) {
showPage(page, zoom);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
backgroundThread = null;
}
});
updateImageStatus();
backgroundThread.start();
}
private void updateImageStatus() {
if (backgroundThread == null) {
updateTexts();
return;
}
uiHandler.postDelayed(new Runnable() {
@Override public void run() {
updateImageStatus();
}
}, 1000);
}
protected void updateTexts() {
int maxCmds = PDFPage.getParsedCommands();
int curCmd = PDFPage.getLastRenderedCommand()+1;
if(maxCmds==curCmd){
_p.dismiss();
finish();
Log.e("Amit","check update_finish()");
}else {
setContent();
Log.e("Amit","check update_again");
}
}
private Bitmap showPage(int page, float zoom) throws Exception {
Bitmap b=null;
try {
Log.e("amit","Go to page bitmap");
mPdfPage = mPdfFile.getPage(page, true);
float wi = mPdfPage.getWidth();
float hei = mPdfPage.getHeight();
RectF clip = null;
Bitmap bi = mPdfPage.getImage((int)(wi*zoom), (int)(hei*zoom), clip, true, true);
b=bi;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "Firstpdf.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
Log.e("amit","Go to page bitmap______SAVE");
} catch (Throwable e) {
Log.e(TAG, e.getMessage(), e);
}
return b;
}
private void parsePDF(String filename) throws PDFAuthenticationFailureException {
try {
File f = new File(filename);
long len = f.length();
if (len == 0) {
Log.e("amit","No file found");
}
else {
Log.e("amit","file '" + filename + "' has " + len + " bytes");
openFile(f);
}
}
catch (PDFAuthenticationFailureException e) {
throw e;
} catch (Throwable e) {
e.printStackTrace();
}
}
public void openFile(File file) throws IOException {
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer bb =
ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
mPdfFile = new PDFFile(bb);
Log.e("Amit","Pages-----"+mPdfFile.getNumPages());
}
}