spire 操作word文档(java)在页脚插入图片并且右对齐

发布时间 2023-07-18 10:45:46作者: 我是深水的猫

继续上一个需求,在页脚右下角插入图片,支持国产化的文档

#main方法测试

public static void main(String[] args) {
        String path = "D:\\charu2.docx";
        String imageFileName = "D:\\4fa94058-de6d-47c8-a8a0-77b07c739e98.png";
        try {
            Document document = new Document();
            // 载入Word文档
            document.loadFromFile(path);
            // 获取文档第一节
            Section section = document.getSections().get(0);
            // 删除页脚
            section.getHeadersFooters().getFooter().getChildObjects().clear();
            // 获取页脚
            HeaderFooter footer = section.getHeadersFooters().getFooter();
            Paragraph paragraph = footer.addParagraph();
            // 插入图片
            paragraph.appendPicture(imageFileName);
            // 设置页脚图片右对齐
            paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
            //保存文档
            document.saveToFile("D:\\charu2.docx", FileFormat.WPS);

        } catch (Exception e) {
            System.out.println("保存失败");
        }
    }