Advertisement
Guest User

Untitled

a guest
May 24th, 2012
7,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.vinfo.mjpdfdemo;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.RandomAccessFile;
  6. import java.nio.channels.FileChannel;
  7.  
  8. import net.sf.andpdf.nio.ByteBuffer;
  9. import android.graphics.Bitmap;
  10. import android.graphics.RectF;
  11. import android.util.Log;
  12.  
  13. import com.sun.pdfview.PDFFile;
  14. import com.sun.pdfview.PDFPage;
  15. import com.sun.pdfview.decrypt.PDFAuthenticationFailureException;
  16.  
  17. public class PdfTest {
  18.    
  19.     private static Bitmap bArr;
  20.     public PdfTest()
  21.     {
  22.         PdfFind obj = new PdfFind();
  23.         try {
  24.             obj.parsePDF("/sdcard/iloveindia.pdf");
  25.             if(obj.showPage(3, 1)!=null)
  26.                 bArr = obj.showPage(3, 1);         
  27.         } catch (PDFAuthenticationFailureException e) {
  28.             // TODO Auto-generated catch block
  29.             e.printStackTrace();
  30.         } catch (Exception e) {
  31.             // TODO Auto-generated catch block
  32.             e.printStackTrace();
  33.         }
  34.        
  35.     }
  36.    
  37.     public Bitmap getBitmapImages()
  38.     {
  39.         return bArr;
  40.     }
  41.  
  42.     private class PdfFind {
  43.         private PDFFile mPdfFile;
  44.         private PDFPage mPdfPage;
  45.         private void parsePDF(String filename)
  46.         throws PDFAuthenticationFailureException {
  47.    
  48.             try {
  49.                 File f = new File(filename);
  50.                 long len = f.length();
  51.                 if (len == 0) {
  52.                     Log.i("No File","File length is 0");
  53.                 } else {
  54.                     openFile(f);
  55.                     Log.i("ParsePDF","parse pdf called f ="+f.toString());
  56.                 }
  57.             } catch (PDFAuthenticationFailureException e) {
  58.                 throw e;
  59.             } catch (Throwable e) {
  60.                 e.printStackTrace();
  61.                 Log.i("Error","Cant Read File");
  62.             }
  63.         }
  64.        
  65.        
  66.         public void openFile(File file) throws IOException {
  67.             // first open the file for random access
  68.             RandomAccessFile raf = new RandomAccessFile(file, "r");
  69.    
  70.             // extract a file channel
  71.             FileChannel channel = raf.getChannel();
  72.    
  73.             // now memory-map a byte-buffer
  74.             ByteBuffer bb = ByteBuffer.NEW(channel.map(
  75.                     FileChannel.MapMode.READ_ONLY, 0, channel.size()));
  76.             // create a PDFFile from the data          
  77.             mPdfFile = new PDFFile(bb);
  78.             Log.i("File","mPdfFile Obj created pages = "+mPdfFile.getNumPages());
  79.         }
  80.        
  81.         private Bitmap showPage(int page,int zoom) throws Exception {
  82.             try {
  83.                 // free memory from previous page
  84.                
  85.                 System.gc();
  86.                
  87.                 mPdfPage = mPdfFile.getPage(page, true);
  88.                 Log.i("File Page","mPdfPage creates width = "+mPdfPage.getWidth());
  89.                 float wi = mPdfPage.getWidth();
  90.                 float hei = mPdfPage.getHeight();
  91.                
  92.                 RectF clip = null;         
  93.                 Bitmap bi = mPdfPage.getImage((int) (wi * 1.25),
  94.                         (int) (hei * 1.25 ), clip, true, true);            
  95.                 Log.i("PAge Image","Image Created width = "+bi.getWidth());            
  96.                 return bi;
  97.             } catch (Throwable e) {
  98.                 Log.e("Error", e.getMessage(), e);         
  99.             }
  100.             return null;
  101.    
  102.         }
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement