如何在iText中从pdf的第一页创建图像

时间:2013-02-12 11:05:25

标签: java android pdf itext

我想从PDF的第一页创建图像。我在java中使用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中?

2 个答案:

答案 0 :(得分:5)

iText并不适用于此目的。

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

jar文件在zip中。

下载该库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库,我真的不确定是否可能。对不起存储是硬编码的,但这是我当时最不关心的问题。

答案 1 :(得分:4)

我最终找到了如何做最初问的问题!!!

您需要iTextG库(itextg-5.5.3.jar),scpkix-jdk15on.1.47.0.1.jar&amp; 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
  }
  }

享受家伙