POI复制图像不会调整大小

时间:2018-07-19 15:11:19

标签: java excel apache-poi

我正在使用POI API将许多Excel工作簿复制到1个大型Excel工作簿中。对于我的要求之一,有一些图像在文档中专门放置和调整大小。我正在使用以下代码的组合来传输所有必要的图像。它们只会是图片,并且找到所有图片。

import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; // only important class you need to know about.

private static void transferShape (XSSFSheet sheet, XSSFSheet newSheet) {
    XSSFDrawing drawing = sheet.createDrawingPatriarch();

    for(XSSFShape shape : drawing.getShapes()) {
        if(shape instanceof XSSFPicture) {
            XSSFPicture picture = (XSSFPicture) shape;
            transferPicture(picture, newSheet);
        }
    }
}

private static void transferPicture(XSSFPicture picture, XSSFSheet newSheet) {
    XSSFPictureData xssfPictureData = picture.getPictureData();
    XSSFClientAnchor anchor = picture.getPreferredSize();

    int col1 = anchor.getCol1();
    int col2 = anchor.getCol2();
    int row1 = anchor.getRow1();
    int row2 = anchor.getRow2();

    int x1 = anchor.getDx1();
    int x2 = anchor.getDx2();
    int y1 = anchor.getDy1();
    int y2 = anchor.getDy2();

    XSSFWorkbook newWb = newSheet.getWorkbook();
    XSSFCreationHelper newHelper = newWb.getCreationHelper();
    XSSFDrawing newDrawing = newSheet.createDrawingPatriarch();
    XSSFClientAnchor newAnchor = newHelper.createClientAnchor();

    newAnchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);

    // Row / Column placement.
    newAnchor.setCol1(col1);
    newAnchor.setCol2(col2);
    newAnchor.setRow1(row1);
    newAnchor.setRow2(row2);

    // Fine touch adjustment along the XY coordinate.
    newAnchor.setDx1(x1);
    newAnchor.setDx2(x2);
    newAnchor.setDy1(y1);
    newAnchor.setDy2(y2);

    double imageX = 0;
    double imageY = 0;

    if(anchor.getSize() != null) {
        newAnchor.setSize(anchor.getSize());
    } else {
        int width = 0;
        int height = 0;

        for(int col = col1; col <= col2; col++) {
            width += Math.round(Units.columnWidthToEMU(newSheet.getColumnWidth(col)));
        }

        width -= x1 - x2;

        for(int row = row1; row <= row2; row++) {
            height += Math.round(Units.TwipsToEMU(newSheet.getRow(row).getHeight()));
        }

        height -= y1 - y2;


        CTPositiveSize2D ps2D = CTPositiveSize2D.Factory.newInstance();


        ps2D.setCx((long) 0);
        ps2D.setCy((long) 0);

        newAnchor.setSize(ps2D);
    }

    int newPictureIndex = newWb.addPicture(xssfPictureData.getData(), xssfPictureData.getPictureType());

    XSSFPicture newPicture = newDrawing.createPicture(newAnchor, newPictureIndex);
    newPicture.resize();
}

但是,主要问题是图像不再正确调整尺寸。发生了一些事情,破坏了该功能。我必须添加以下其他内容以开发新的解决方案。

else {
    int width = 0;
    int height = 0;

    for(int col = col1; col <= col2; col++) {
        width += Math.round(Units.columnWidthToEMU(newSheet.getColumnWidth(col)));
    }

    width -= x1 - x2;

    for(int row = row1; row <= row2; row++) {
        height += Math.round(Units.TwipsToEMU(newSheet.getRow(row).getHeight()));
    }

    height -= y1 - y2;


    CTPositiveSize2D ps2D = CTPositiveSize2D.Factory.newInstance();


    ps2D.setCx((long) 0);
    ps2D.setCy((long) 0);

    newAnchor.setSize(ps2D);
}

但是...即使将Cx和Cy设置为0,也不会调整大小。我不确定发生了什么。

newAnchor.setSize(anchor.getSize())过去只能自己工作。不知道是什么原因使其返回null。

1 个答案:

答案 0 :(得分:0)

这是来自API的太多代码的情况!基本上,图像的大小由dxdy变量推动的位置和距离来定义。

以下功能是最终产品,效果很好。

private static void transferShape (XSSFSheet sheet, XSSFSheet newSheet) {
    XSSFDrawing drawing = sheet.createDrawingPatriarch();

    for(XSSFShape shape : drawing.getShapes()) {
        if(shape instanceof XSSFPicture) {
            transferPicture(shape, newSheet);
        }
    }
}

private static void transferPicture(XSSFShape shape, XSSFSheet newSheet) {
    XSSFPicture picture = (XSSFPicture) shape;

    XSSFPictureData xssfPictureData = picture.getPictureData();
    XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();

    int col1 = anchor.getCol1();
    int col2 = anchor.getCol2();
    int row1 = anchor.getRow1();
    int row2 = anchor.getRow2();

    int x1 = anchor.getDx1();
    int x2 = anchor.getDx2();
    int y1 = anchor.getDy1();
    int y2 = anchor.getDy2();

    XSSFWorkbook newWb = newSheet.getWorkbook();
    XSSFCreationHelper newHelper = newWb.getCreationHelper();
    XSSFDrawing newDrawing = newSheet.createDrawingPatriarch();
    XSSFClientAnchor newAnchor = newHelper.createClientAnchor();

    // Row / Column placement.
    newAnchor.setCol1(col1);
    newAnchor.setCol2(col2);
    newAnchor.setRow1(row1);
    newAnchor.setRow2(row2);

    // Fine touch adjustment along the XY coordinate.
    newAnchor.setDx1(x1);
    newAnchor.setDx2(x2);
    newAnchor.setDy1(y1);
    newAnchor.setDy2(y2);

    int newPictureIndex = newWb.addPicture(xssfPictureData.getData(), xssfPictureData.getPictureType());

    XSSFPicture newPicture = newDrawing.createPicture(newAnchor, newPictureIndex);
}

希望这对某人有帮助。这是一个我还没有发现的非常奇怪的问题。