首页 文章

POI复制图像不会调整大小

提问于
浏览
0

我正在使用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

    这是来自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);
    }
    

    希望这有助于某人 . 这是一个非常奇怪的问题,我还没有找到 .

相关问题