Reputation: 2897
I want to create image from first page of an PDF . I am using iText in java . Can you suggest me what to do to extract first page of an pdf as an image ?
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(
document, new FileOutputStream(RESULT));
document.open();
File extStore = Environment.getExternalStorageDirectory();
String path=extStore.getPath()+"/FirstPdf.pdf";
PdfReader reader = new PdfReader(path);
int n = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
page = writer.getImportedPage(reader, i);
// Image.getInstance(page) ;
}
document.close();
I have written the above code . What to do to extract first page of a pdf as an image and save it in SDCARD ?
Upvotes: 2
Views: 9776
Reputation: 1061
I ended up finding out how to do what the question initially asked!!!
You need iTextG library (itextg-5.5.3.jar), scpkix-jdk15on.1.47.0.1.jar & scprov-jdk15on-1.47.0.2.jar
inside where want to call it from:
public static final String RESULT = "/storage/sdcard0/dirAtExtStorage/Img%s.%s";
public void extractImages(String filename)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(filename);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener(RESULT);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
}
}
inside MyImageRendererListener.java:
public class MyImageRenderListener implements RenderListener{
private String path;
public MyImageRenderListener(String path) {
this.path = path;
}
@Override
public void beginTextBlock() {
// TODO Auto-generated method stub
}
@Override
public void endTextBlock() {
// TODO Auto-generated method stub
}
public void renderImage(ImageRenderInfo renderInfo) {
try {
System.out.print("renderImage");
String filename;
FileOutputStream os;
PdfImageObject image = renderInfo.getImage();
if (image == null) return;
filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
os = new FileOutputStream(filename);
os.write(image.getImageAsBytes());
os.flush();
os.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
@Override
public void renderText(TextRenderInfo arg0) {
// TODO Auto-generated method stub
}
}
enjoy guys
Upvotes: 4
Reputation: 1061
iText doesn't work for that purpose.
The jar file is in the zip.
Download that library PdfViewer.jar and try this code:
byte[] bytes;
try {
File file = new File("/storage/extSdCard/Test.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, true);
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);
FileOutputStream os = new FileOutputStream("/storage/extSdCard/pdf.jpg");
image.compress(Bitmap.CompressFormat.JPEG, 80, os);
//((ImageView) findViewById(R.id.testView)).setImageBitmap(image);
} catch (Exception e) {
e.printStackTrace();
}
You can change the rect around to make it extract any part of the pdf you want etc too, pretty good. Spent about 16 hours banging my head against a wall before finding that solution. Wasn't really sure if it was possible without the swing awt library. Sorry the storage is hard coded but it was the least of my concerns at the time.
Upvotes: 5