提取MS Word表格单元格作为图像? [英] Extracting MS Word Table Cell as image?

查看:82
本文介绍了提取MS Word表格单元格作为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取表格单元格作为图像.单元格可能包含混合的内容(文本+图像"),我需要将其合并为一个图像.我可以获取核心文本,但是我不知道要获取图像+文本. 不确定Apace POI是否会帮助您.

I need to extract table cells as images. The cells may contain mixed content (Text + Image), which I need to merge into a single image. I am able to get the core text but I have no idea to get an image+text. Not sure if Apace POI would help.

有人有做过这样的事情吗?

Has anyone done something like this earlier?

  public static void readTablesDataInDocx(XWPFDocument doc) {
    int tableIdx = 1;
    int rowIdx = 1;
    int colIdx = 1;
    List table = doc.getTables();
    System.out.println("==========No Of Tables in Document=============================================" + table.size());
    for (int k = 0; k < table.size(); k++) {
        XWPFTable xwpfTable = (XWPFTable) table.get(k);
        System.out.println("================table -" + tableIdx + "===Data==");
        rowIdx = 1;
        List row = xwpfTable.getRows();
        for (int j = 0; j < row.size(); j++) {
            XWPFTableRow xwpfTableRow = (XWPFTableRow) row.get(j);
            System.out.println("Row -" + rowIdx);
            colIdx = 1;
            List cell = xwpfTableRow.getTableCells();
            for (int i = 0; i < cell.size(); i++) {
                XWPFTableCell xwpfTableCell = (XWPFTableCell) cell.get(i);
                if (xwpfTableCell != null) {
                    System.out.print("\t" + colIdx + "- column value: " + xwpfTableCell.getText());
                }
                colIdx++;
            }
            System.out.println("");
            rowIdx++;
        }
        tableIdx++;
        System.out.println("");
    }
}

现在我可以借助此方法获取文本

Now I am able to get Text with the help of this method

System.out.print("\t" + colIdx + "- column value: " + xwpfTableCell.getText());

如果一个单元格中也包含一个图像,如何获取图像?

How do I get the Image if a cell also contains one?

推荐答案

尝试以下代码,对我有用

Try this code, it's working for me

 XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName));
            List<XWPFTable> table = doc.getTables();
            for (XWPFTable xwpfTable : table) {
                List<XWPFTableRow> row = xwpfTable.getRows();
                for (XWPFTableRow xwpfTableRow : row) {
                    List<XWPFTableCell> cell = xwpfTableRow.getTableCells();
                    for (XWPFTableCell xwpfTableCell : cell) {
                        if (xwpfTableCell != null) {
                            System.out.println(xwpfTableCell.getText());
                            String s = xwpfTableCell.getText();
                            for (XWPFParagraph p : xwpfTableCell.getParagraphs()) {
                                for (XWPFRun run : p.getRuns()) {
                                    for (XWPFPicture pic : run.getEmbeddedPictures()) {
                                        byte[] pictureData = pic.getPictureData().getData();
                                        System.out.println("picture : " + pictureData);
                                    }
                                }
                            }
                        }
                    }
                }
            }

这篇关于提取MS Word表格单元格作为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆