java 多张图片转ODF 返回 base64 springboot

发布时间 2023-07-25 17:34:00作者: vx_guanchaoguo0

将图片平铺到真个页面

package com.example;


import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.PageLayout;
import org.ofdrw.layout.VirtualPage;
import org.ofdrw.layout.element.Img;
import org.ofdrw.layout.element.Position;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;


public class GenerateOfdFile {

    public static void main(String[] args) throws IOException {
        Path dst = Paths.get("target/IMAGE-PAGES.ofd");
        Path img1 = Paths.get("src/test/resources/151690255274_.pic.jpg");
        try (OFDDoc ofdDoc = new OFDDoc(dst)) {
            PageLayout pageLayout = ofdDoc.getPageLayout();
            VirtualPage vPage = new VirtualPage(pageLayout);
            BufferedImage image = Img.readImage(img1.toFile());
            float height = image.getHeight();
            float width = image.getWidth();
            Img img;
            if (height / width > pageLayout.getHeight() / pageLayout.getWidth()) {
                img = new Img((pageLayout.getHeight()) * width / height, pageLayout.getHeight(), img1);
            } else {
                img = new Img((pageLayout.getWidth()), (pageLayout.getWidth()) / width * height, img1);
            }
            double x = (pageLayout.getWidth() - img.getWidth()) / 2;
            double y = (pageLayout.getHeight() - img.getHeight()) / 2;
            img.setPosition(Position.Absolute).setX(x).setY(y);
            vPage.add(img);
            ofdDoc.addVPage(vPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}


完整代码 github