首页 文章

使用Java在excel中读取嵌入式pdf文件

提问于
浏览
0

我是Java编程的新手 . 我当前的项目要求我在excel表中读取嵌入式(ole)文件并在其中获取文本内容 . 读取嵌入式word文件的示例工作正常,但是我无法找到读取嵌入式pdf文件的帮助 . 通过查看类似的例子来尝试一些事情....这些事情没有成功 .

http://poi.apache.org/spreadsheet/quick-guide.html#Embedded

我有下面的代码,可能有帮助,我可以正确的方向 . 我使用Apache POI读取excel和pdfbox中的嵌入文件来解析pdf数据 .

public class ReadExcel1 {

public static void main(String[] args) {

    try {

        FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

        POIFSFileSystem fs = new POIFSFileSystem(file);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);

        for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {

            String oleName = obj.getOLE2ClassName();

           if(oleName.equals("Acrobat Document")){
                System.out.println("Acrobat reader document");

                try{
                    DirectoryNode dn = (DirectoryNode) obj.getDirectory();
                    for (Iterator<Entry> entries = dn.getEntries(); entries.hasNext();) {

                        DocumentEntry nativeEntry = (DocumentEntry) dn.getEntry("CONTENTS");
                        byte[] data = new byte[nativeEntry.getSize()];

                        ByteArrayInputStream bao= new ByteArrayInputStream(data);
                        PDFParser pdfparser = new PDFParser(bao);

                        pdfparser.parse();
                        COSDocument cosDoc = pdfparser.getDocument();
                        PDFTextStripper pdfStripper = new PDFTextStripper();
                        PDDocument pdDoc = new PDDocument(cosDoc);
                        pdfStripper.setStartPage(1);
                        pdfStripper.setEndPage(2);
                        System.out.println("Text from the pdf "+pdfStripper.getText(pdDoc));
                    }
                }catch(Exception e){
                    System.out.println("Error reading "+ e.getMessage());
                }finally{
                    System.out.println("Finally ");
                }
            }else{
                System.out.println("nothing ");
            }
        }

        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

以下是eclipse中的输出

Acrobat reader document

读错误:文件结束,预期行最后没有

1 回答

  • 1

    PDF不是封装的OLE 1.0,但不同的嵌入方式 - 至少提取对我有用 . 这不是一般的解决方案,因为它取决于你如何能嵌入应用程序名称的条目......当然对于PDF文件检查所有 DocumentNode -s为神奇数字"%PDF" - 在OLE 1.0包装元素的情况下,这就需要将做得不同......

    我认为,pdf的真实文件名是隐藏在 \1OleCompObj 条目中的某个地方,但是对于示例而言,显然对于您的用例而言,没有必要确定 .

    import java.io.*;
    import java.net.URL;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.poifs.filesystem.*;
    import org.apache.poi.util.IOUtils;
    
    public class EmbeddedPdfInExcel {
        public static void main(String[] args) throws Exception {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(new URL("http://jamesshaji.com/sample.xls").openStream());
            HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
            for (HSSFObjectData obj : wb.getAllEmbeddedObjects()) {
                String oleName = obj.getOLE2ClassName();
                DirectoryNode dn = (DirectoryNode)obj.getDirectory();
                if(oleName.contains("Acro") && dn.hasEntry("CONTENTS")){
                    InputStream is = dn.createDocumentInputStream("CONTENTS");
                    FileOutputStream fos = new FileOutputStream(obj.getDirectory().getName()+".pdf");
                    IOUtils.copy(is, fos);
                    fos.close();
                    is.close();
                }
            }
            fs.close();
        }
    }
    

相关问题