I need to convert PDFfile(PDF page) into a Bitmap(or Image file) in Android.
1.Used Pdfbox jar from Apache. But it uses some java classes that is not supported in android. 2. Tried Itext jar which converts image to pdf(I need its reverse operation) Like that I have tried many jars. But no positive result.
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);
I am getting the Image File,
Instead of,
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();
}
}
}
Else, any other way to display pdf file in android using function inbuilt within application?