2013-02-12 99 views
2

我想從PDF的第一頁創建圖像。我在java中使用iText。你能建議我怎麼做才能提取PDF的第一頁作爲圖像?如何在iText中從pdf的第一頁創建圖像

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(); 

我寫了上面的代碼。如何將pdf的第一頁作爲圖像提取並保存在SDCARD中?

+0

iText的不是這是正確的工具,因爲它不是PDF渲染器。 – 2013-02-12 12:15:20

+0

你能告訴我應該遵循什麼程序來完成我的目標? – 2013-02-12 13:21:41

+0

尋找在android上工作的pdf渲染器,並允許您導出縮略圖。 – 2013-02-12 14:21:33

回答

5

iText不適用於此目的。

http://www.java2s.com/Open-Source/Android_Free_Code/Pdf/Download_Free_code_Android_Pdf_Viewer_Library.htm

的jar文件在壓縮。

下載該庫PdfViewer.jar和試試這個代碼:

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(); 
     } 

你可以改變周圍的矩形,使其提取你想等也相當不錯的PDF格式的任何部分。在找到解決方案之前,花了大約16個小時將我的頭靠在牆上。不確定是否有可能沒有swing awt庫。對不起,存儲是硬編碼的,但這是我當時最擔心的問題。

4

我最終發現如何去做最初提出的問題!

你需要iTextG庫(itextg-5.5.3.jar),scpkix-jdk15on.1.47.0.1.jar & scprov-jdk15on-1.47.0.2.jar

內,其中希望從調用它:

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); 
    } 
} 

內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 
    } 
    } 

享受傢伙

相關問題