6c
2023-11-16 a0e46f7f776b6c4526e9596fb0ee0ad51e8340cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.product.print.util;
 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
 
/**
 * @Author cheng
 * @Date 2023/8/5 16:40
 * @Desc
 */
public class PrintPoiUtil {
    /**
     * 复制行,从source到target
     *
     * @param target
     * @param source
     */
    public static void copyTableRow(XWPFTableRow target, XWPFTableRow source) {
        // 复制样式
        if (source.getCtRow() != null) {
            target.getCtRow().setTrPr(source.getCtRow().getTrPr());
        }
        // 复制单元格
        for (int i = 0; i < source.getTableCells().size(); i++) {
            XWPFTableCell cell1 = target.getCell(i);
            XWPFTableCell cell2 = source.getCell(i);
            if (cell1 == null) {
                cell1 = target.addNewTableCell();
            }
            copyTableCell(cell1, cell2);
        }
 
    }
 
    /**
     * 复制单元格,从source到target
     *
     * @param target
     * @param source
     */
    public static void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
        // 列属性
        if (source.getCTTc() != null) {
            target.getCTTc().setTcPr(source.getCTTc().getTcPr());
        }
        // 删除段落
        for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
            target.removeParagraph(pos);
        }
        // 添加段落
        for (XWPFParagraph sp : source.getParagraphs()) {
            XWPFParagraph targetP = target.addParagraph();
            copyParagraph(targetP, sp);
        }
    }
 
    /**
     * 复制图片到target
     *
     * @param target
     * @param picture
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static void copyPicture(XWPFRun target, XWPFPicture picture) throws IOException, InvalidFormatException {
 
        String filename = picture.getPictureData().getFileName();
        InputStream pictureData = new ByteArrayInputStream(picture
                .getPictureData().getData());
        int pictureType = picture.getPictureData().getPictureType();
        int width = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
                .getCx();
 
        int height = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
                .getCy();
 
        // target.addBreak();
        target.addPicture(pictureData, pictureType, filename, width, height);
        // target.addBreak(BreakType.PAGE);
    }
 
    /**
     * 复制段落,从source到target
     *
     * @param target
     * @param source
     */
    public static void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
 
        // 设置段落样式
        target.getCTP().setPPr(source.getCTP().getPPr());
 
        // 移除所有的run
        for (int pos = target.getRuns().size() - 1; pos >= 0; pos--) {
            target.removeRun(pos);
        }
 
        // copy 新的run
        for (XWPFRun s : source.getRuns()) {
            XWPFRun targetrun = target.createRun();
            copyRun(targetrun, s);
        }
 
 
    }
 
    /**
     * 复制RUN,从source到target
     *
     * @param target
     * @param source
     */
    public static void copyRun(XWPFRun target, XWPFRun source) {
        // 设置run属性
        target.getCTR().setRPr(source.getCTR().getRPr());
        // 设置文本
        target.setText(source.text());
        // 处理图片
        List<XWPFPicture> pictures = source.getEmbeddedPictures();
 
        for (XWPFPicture picture : pictures) {
            try {
                copyPicture(target, picture);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}