feiyu02
2022-07-28 237d7c42498806a3ca205f63d151671a45304854
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
package cn.flightfeather.supervision.infrastructure.utils
 
import org.apache.commons.lang3.StringUtils
import org.docx4j.TextUtils
import org.docx4j.XmlUtils
import org.docx4j.model.properties.table.tr.TrHeight
import org.docx4j.openpackaging.packages.WordprocessingMLPackage
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart
import org.docx4j.openpackaging.parts.relationships.Namespaces
import org.docx4j.org.apache.poi.util.IOUtils
import org.docx4j.wml.*
import java.io.File
import java.io.FileInputStream
import java.io.StringWriter
import java.math.BigInteger
import java.util.ArrayList
import javax.xml.bind.JAXBElement
 
public class Docx4jUtil {
    private var wordMLPackage: WordprocessingMLPackage? = null
    private var mainPart: MainDocumentPart? = null
    private var factory: ObjectFactory? = null
    private var paragraph: P? = null
    private var url: String? = null
    private var value: String? = null
    private var cnFontName: String? = null
    private var enFontName: String? = null
    private var fontSize: String? = null
    /*------------------------------------other---------------------------------------------------  */
    /**
     * @Description:新增超链接
     */
    @Throws(Exception::class)
    fun createHyperlink(wordMLPackage: WordprocessingMLPackage,
                        mainPart: MainDocumentPart, factory: ObjectFactory, paragraph: P,
                        url: String, value: String, cnFontName: String, enFontName: String,
                        fontSize: String) {
        var cnFontName = cnFontName
        var enFontName = enFontName
        var fontSize = fontSize
        this.wordMLPackage = wordMLPackage
        this.mainPart = mainPart
        this.factory = factory
        this.paragraph = paragraph
        this.url = url
        this.value = value
        this.cnFontName = cnFontName
        this.enFontName = enFontName
        this.fontSize = fontSize
        if (StringUtils.isBlank(enFontName)) {
            enFontName = "Times New Roman"
        }
        if (StringUtils.isBlank(cnFontName)) {
            cnFontName = "微软雅黑"
        }
        if (StringUtils.isBlank(fontSize)) {
            fontSize = "22"
        }
        val reFactory = org.docx4j.relationships.ObjectFactory()
        val rel = reFactory
                .createRelationship()
        rel.type = Namespaces.HYPERLINK
        rel.target = url
        rel.targetMode = "External"
        mainPart.relationshipsPart.addRelationship(rel)
        val sb = StringBuffer()
        // addRelationship sets the rel's @Id
        sb.append("<w:hyperlink r:id=\"")
        sb.append(rel.id)
        sb.append("\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ")
        sb.append("xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >")
        sb.append("<w:r><w:rPr><w:rStyle w:val=\"Hyperlink\" />")
        sb.append("<w:rFonts  w:ascii=\"")
        sb.append(enFontName)
        sb.append("\"  w:hAnsi=\"")
        sb.append(enFontName)
        sb.append("\"  w:eastAsia=\"")
        sb.append(cnFontName)
        sb.append("\" w:hint=\"eastAsia\"/>")
        sb.append("<w:sz w:val=\"")
        sb.append(fontSize)
        sb.append("\"/><w:szCs w:val=\"")
        sb.append(fontSize)
        sb.append("\"/></w:rPr><w:t>")
        sb.append(value)
        sb.append("</w:t></w:r></w:hyperlink>")
 
        val link = XmlUtils.unmarshalString(sb.toString()) as P.Hyperlink
        paragraph.content.add(link)
    }
 
    @Throws(Exception::class)
    fun getElementContent(obj: Any): String {
        val stringWriter = StringWriter()
        TextUtils.extractText(obj, stringWriter)
        return stringWriter.toString()
    }
 
    /**
     * @Description:得到指定类型的元素
     */
    fun getAllElementFromObject(obj: Any,
                                toSearch: Class<*>): List<Any> {
        var obj = obj
        val result = ArrayList<Any>()
        if (obj is JAXBElement<*>)
            obj = obj.value
        if (obj.javaClass == toSearch)
            result.add(obj)
        else if (obj is ContentAccessor) {
            val children = obj.content
            for (child in children) {
                result.addAll(getAllElementFromObject(child, toSearch))
            }
        }
        return result
    }
 
    /**
     * @Description:保存WordprocessingMLPackage
     */
    @Throws(Exception::class)
    fun saveWordPackage(wordPackage: WordprocessingMLPackage, file: File) {
        wordPackage.save(file)
    }
 
    /**
     * @Description:新建WordprocessingMLPackage
     */
    @Throws(Exception::class)
    fun createWordprocessingMLPackage(): WordprocessingMLPackage {
        return WordprocessingMLPackage.createPackage()
    }
 
    /**
     * @Description:加载带密码WordprocessingMLPackage
     */
    @Throws(Exception::class)
    fun loadWordprocessingMLPackageWithPwd(
            filePath: String, password: String): WordprocessingMLPackage {
        val opcPackage = WordprocessingMLPackage.load(File(
                filePath), password)
        return opcPackage as WordprocessingMLPackage
    }
 
    /**
     * @Description:加载WordprocessingMLPackage
     */
    @Throws(Exception::class)
    fun loadWordprocessingMLPackage(filePath: String): WordprocessingMLPackage {
        return WordprocessingMLPackage
                .load(File(filePath))
    }
 
    /*------------------------------------Word 表格相关---------------------------------------------------  */
    /**
     * @Description: 跨列合并
     */
    fun mergeCellsHorizontalByGridSpan(tbl: Tbl, row: Int, fromCell: Int,
                                       toCell: Int) {
        if (row < 0 || fromCell < 0 || toCell < 0) {
            return
        }
        val trList = getTblAllTr(tbl)
        if (row > trList.size) {
            return
        }
        val tr = trList[row]
        val tcList = getTrAllCell(tr)
        for (cellIndex in Math.min(tcList.size - 1, toCell) downTo fromCell) {
            val tc = tcList[cellIndex]
            val tcPr = getTcPr(tc)
            if (cellIndex == fromCell) {
                var gridSpan: TcPrInner.GridSpan? = tcPr.gridSpan
                if (gridSpan == null) {
                    gridSpan = TcPrInner.GridSpan()
                    tcPr.gridSpan = gridSpan
                }
                gridSpan.`val` = BigInteger.valueOf((Math.min(tcList.size - 1,
                        toCell) - fromCell + 1).toLong())
            } else {
                tr.content.removeAt(cellIndex)
            }
        }
    }
 
    /**
     * @Description: 跨列合并
     */
    fun mergeCellsHorizontal(tbl: Tbl, row: Int, fromCell: Int, toCell: Int) {
        if (row < 0 || fromCell < 0 || toCell < 0) {
            return
        }
        val trList = getTblAllTr(tbl)
        if (row > trList.size) {
            return
        }
        val tr = trList[row]
        val tcList = getTrAllCell(tr)
        var cellIndex = fromCell
        val len = Math
                .min(tcList.size - 1, toCell)
        while (cellIndex <= len) {
            val tc = tcList[cellIndex]
            val tcPr = getTcPr(tc)
            var hMerge: TcPrInner.HMerge? = tcPr.hMerge
            if (hMerge == null) {
                hMerge = TcPrInner.HMerge()
                tcPr.hMerge = hMerge
            }
            if (cellIndex == fromCell) {
                hMerge.`val` = "restart"
            } else {
                hMerge.`val` = "continue"
            }
            cellIndex++
        }
    }
 
    /**
     * @Description: 跨行合并
     */
    fun mergeCellsVertically(tbl: Tbl, col: Int, fromRow: Int, toRow: Int) {
        if (col < 0 || fromRow < 0 || toRow < 0) {
            return
        }
        for (rowIndex in fromRow..toRow) {
            val tc = getTc(tbl, rowIndex, col) ?: break
            val tcPr = getTcPr(tc)
            var vMerge: TcPrInner.VMerge? = tcPr.vMerge
            if (vMerge == null) {
                vMerge = TcPrInner.VMerge()
                tcPr.vMerge = vMerge
            }
            if (rowIndex == fromRow) {
                vMerge.`val` = "restart"
            } else {
                vMerge.`val` = "continue"
            }
        }
    }
 
    /**
     * @Description:得到指定位置的单元格
     */
    fun getTc(tbl: Tbl, row: Int, cell: Int): Tc? {
        if (row < 0 || cell < 0) {
            return null
        }
        val trList = getTblAllTr(tbl)
        if (row >= trList.size) {
            return null
        }
        val tcList = getTrAllCell(trList[row])
        return if (cell >= tcList.size) {
            null
        } else tcList[cell]
    }
 
    /**
     * @Description:得到所有表格
     */
    fun getAllTbl(wordMLPackage: WordprocessingMLPackage): List<Tbl>? {
        val mainDocPart = wordMLPackage.mainDocumentPart
        val objList = getAllElementFromObject(mainDocPart, Tbl::class.java) ?: return null
        val tblList = ArrayList<Tbl>()
        for (obj in objList) {
            if (obj is Tbl) {
                tblList.add(obj)
            }
        }
        return tblList
    }
 
    /**
     * @Description:删除指定位置的表格,删除后表格数量减一
     */
    @Throws(Exception::class)
    fun removeTableByIndex(wordMLPackage: WordprocessingMLPackage,
                           index: Int): Boolean {
        var flag = false
        if (index < 0) {
            return flag
        }
        val objList = wordMLPackage.mainDocumentPart.content ?: return flag
        var k = -1
        var i = 0
        val len = objList.size
        while (i < len) {
            val obj = XmlUtils.unwrap(objList[i])
            if (obj is Tbl) {
                k++
                if (k == index) {
                    wordMLPackage.mainDocumentPart.content.removeAt(i)
                    flag = true
                    break
                }
            }
            i++
        }
        return flag
    }
 
    /**
     * @Description: 获取单元格内容,无分割符
     */
    @Throws(Exception::class)
    fun getTblContentStr(tbl: Tbl): String {
        return getElementContent(tbl)
    }
 
 
    /**
     * @Description: 获取表格内容
     */
    @Throws(Exception::class)
    fun getTblContentList(tbl: Tbl): List<String> {
        val resultList = ArrayList<String>()
        val trList = getTblAllTr(tbl)
        for (tr in trList) {
            val sb = StringBuffer()
            val tcList = getTrAllCell(tr)
            for (tc in tcList) {
                sb.append(getElementContent(tc) + ",")
            }
            resultList.add(sb.toString())
        }
        return resultList
    }
 
    fun getTblPr(tbl: Tbl): TblPr {
        var tblPr: TblPr? = tbl.tblPr
        if (tblPr == null) {
            tblPr = TblPr()
            tbl.tblPr = tblPr
        }
        return tblPr
    }
 
    /**
     * @Description: 设置表格总宽度
     */
    fun setTableWidth(tbl: Tbl, width: String) {
        if (StringUtils.isNotBlank(width)) {
            val tblPr = getTblPr(tbl)
            var tblW: TblWidth? = tblPr.tblW
            if (tblW == null) {
                tblW = TblWidth()
                tblPr.tblW = tblW
            }
            tblW.w = BigInteger(width)
            tblW.type = "dxa"
        }
    }
 
    /**
     * @Description:创建表格(默认水平居中,垂直居中)
     */
    @Throws(Exception::class)
    fun createTable(wordPackage: WordprocessingMLPackage, rowNum: Int,
                    colsNum: Int): Tbl {
        var rowNum = rowNum
        var colsNum = colsNum
        colsNum = Math.max(1, colsNum)
        rowNum = Math.max(1, rowNum)
        val widthTwips = getWritableWidth(wordPackage)
        val colWidth = widthTwips / colsNum
        val widthArr = IntArray(colsNum)
        for (i in 0 until colsNum) {
            widthArr[i] = colWidth
        }
        return createTable(rowNum, colsNum, widthArr)
    }
 
    /**
     * @Description:创建表格(默认水平居中,垂直居中)
     */
    @Throws(Exception::class)
    fun createTable(rowNum: Int, colsNum: Int, widthArr: IntArray): Tbl {
        var rowNum = rowNum
        var colsNum = colsNum
        colsNum = Math.max(1, Math.min(colsNum, widthArr.size))
        rowNum = Math.max(1, rowNum)
        val tbl = Tbl()
        val tblSb = StringBuffer()
        tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION)
                .append(">")
        tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>")
        tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>")
        // 上边框
        tblSb.append("<w:tblBorders>")
        tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>")
        // 左边框
        tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>")
        // 下边框
        tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>")
        // 右边框
        tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>")
        tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>")
        tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>")
        tblSb.append("</w:tblBorders>")
        tblSb.append("</w:tblPr>")
        var tblPr: TblPr? = null
        tblPr = XmlUtils.unmarshalString(tblSb.toString()) as TblPr
        val jc = Jc()
        // 单元格居中对齐
        jc.`val` = JcEnumeration.CENTER
        tblPr.jc = jc
 
        tbl.tblPr = tblPr
 
        // 设定各单元格宽度
        val tblGrid = TblGrid()
        tbl.tblGrid = tblGrid
        for (i in 0 until colsNum) {
            val gridCol = TblGridCol()
            gridCol.w = BigInteger.valueOf(widthArr[i].toLong())
            tblGrid.gridCol.add(gridCol)
        }
        // 新增行
        for (j in 0 until rowNum) {
            val tr = Tr()
            tbl.content.add(tr)
            // 列
            for (i in 0 until colsNum) {
                val tc = Tc()
                tr.content.add(tc)
 
                val tcPr = TcPr()
                val cellWidth = TblWidth()
                cellWidth.type = "dxa"
                cellWidth.w = BigInteger.valueOf(widthArr[i].toLong())
                tcPr.tcW = cellWidth
                tc.tcPr = tcPr
 
                // 垂直居中
                setTcVAlign(tc, STVerticalJc.CENTER)
                val p = P()
                val pPr = PPr()
                pPr.jc = jc
                p.pPr = pPr
                val run = R()
                p.content.add(run)
                tc.content.add(p)
            }
        }
        return tbl
    }
 
    /**
     * @Description:表格增加边框 可以设置上下左右四个边框样式以及横竖水平线样式
     */
    fun setTblBorders(tblPr: TblPr, topBorder: CTBorder?,
                      rightBorder: CTBorder?, bottomBorder: CTBorder?, leftBorder: CTBorder?,
                      hBorder: CTBorder?, vBorder: CTBorder?) {
        var borders: TblBorders? = tblPr.tblBorders
        if (borders == null) {
            borders = TblBorders()
            tblPr.tblBorders = borders
        }
        if (topBorder != null) {
            borders.top = topBorder
        }
        if (rightBorder != null) {
            borders.right = rightBorder
        }
        if (bottomBorder != null) {
            borders.bottom = bottomBorder
        }
        if (leftBorder != null) {
            borders.left = leftBorder
        }
        if (hBorder != null) {
            borders.insideH = hBorder
        }
        if (vBorder != null) {
            borders.insideV = vBorder
        }
    }
 
    /**
     * @Description: 设置表格水平对齐方式(仅对表格起作用,单元格不一定水平对齐)
     */
    fun setTblJcAlign(tbl: Tbl, jcType: JcEnumeration?) {
        if (jcType != null) {
            val tblPr = getTblPr(tbl)
            var jc: Jc? = tblPr.jc
            if (jc == null) {
                jc = Jc()
                tblPr.jc = jc
            }
            jc.`val` = jcType
        }
    }
 
    /**
     * @Description: 设置表格水平对齐方式(包括单元格),只对该方法前面产生的单元格起作用
     */
    fun setTblAllJcAlign(tbl: Tbl, jcType: JcEnumeration?) {
        if (jcType != null) {
            setTblJcAlign(tbl, jcType)
            val trList = getTblAllTr(tbl)
            for (tr in trList) {
                val tcList = getTrAllCell(tr)
                for (tc in tcList) {
                    setTcJcAlign(tc, jcType)
                }
            }
        }
    }
 
    /**
     * @Description: 设置表格垂直对齐方式(包括单元格),只对该方法前面产生的单元格起作用
     */
    fun setTblAllVAlign(tbl: Tbl, vAlignType: STVerticalJc?) {
        if (vAlignType != null) {
            val trList = getTblAllTr(tbl)
            for (tr in trList) {
                val tcList = getTrAllCell(tr)
                for (tc in tcList) {
                    setTcVAlign(tc, vAlignType)
                }
            }
        }
    }
 
    /**
     * @Description: 设置单元格Margin
     */
    fun setTableCellMargin(tbl: Tbl, top: String, right: String,
                           bottom: String, left: String) {
        val tblPr = getTblPr(tbl)
        var cellMar: CTTblCellMar? = tblPr.tblCellMar
        if (cellMar == null) {
            cellMar = CTTblCellMar()
            tblPr.tblCellMar = cellMar
        }
        if (StringUtils.isNotBlank(top)) {
            val topW = TblWidth()
            topW.w = BigInteger(top)
            topW.type = "dxa"
            cellMar.top = topW
        }
        if (StringUtils.isNotBlank(right)) {
            val rightW = TblWidth()
            rightW.w = BigInteger(right)
            rightW.type = "dxa"
            cellMar.right = rightW
        }
        if (StringUtils.isNotBlank(bottom)) {
            val btW = TblWidth()
            btW.w = BigInteger(bottom)
            btW.type = "dxa"
            cellMar.bottom = btW
        }
        if (StringUtils.isNotBlank(left)) {
            val leftW = TblWidth()
            leftW.w = BigInteger(left)
            leftW.type = "dxa"
            cellMar.left = leftW
        }
    }
 
    /**
     * @Description: 得到表格所有的行
     */
    fun getTblAllTr(tbl: Tbl): List<Tr> {
        val objList = getAllElementFromObject(tbl, Tr::class.java)
        val trList = ArrayList<Tr>()
        if (objList == null) {
            return trList
        }
        for (obj in objList) {
            if (obj is Tr) {
                trList.add(obj)
            }
        }
        return trList
 
    }
 
    /**
     * @Description:设置tr高度
     */
    fun setTrHeight(tr: Tr, heigth: String) {
        val trPr = getTrPr(tr)
        val ctHeight = CTHeight()
        ctHeight.`val` = BigInteger(heigth)
        val trHeight = TrHeight(ctHeight)
        trHeight.set(trPr)
    }
 
    /**
     * @Description: 在表格指定位置新增一行,默认居中
     */
    fun addTrByIndex(tbl: Tbl, index: Int) {
        addTrByIndex(tbl, index, STVerticalJc.CENTER, JcEnumeration.CENTER)
    }
 
    /**
     * @Description: 在表格指定位置新增一行(默认按表格定义的列数添加)
     */
    fun addTrByIndex(tbl: Tbl, index: Int, vAlign: STVerticalJc?,
                     hAlign: JcEnumeration?) {
        val tblGrid = tbl.tblGrid
        val tr = Tr()
        if (tblGrid != null) {
            val gridList = tblGrid.gridCol
            for (tblGridCol in gridList) {
                val tc = Tc()
                setTcWidth(tc, tblGridCol.w.toString())
                if (vAlign != null) {
                    // 垂直居中
                    setTcVAlign(tc, vAlign)
                }
                val p = P()
                if (hAlign != null) {
                    val pPr = PPr()
                    val jc = Jc()
                    // 单元格居中对齐
                    jc.`val` = hAlign
                    pPr.jc = jc
                    p.pPr = pPr
                }
                val run = R()
                p.content.add(run)
                tc.content.add(p)
                tr.content.add(tc)
            }
        } else {
            // 大部分情况都不会走到这一步
            val firstTr = getTblAllTr(tbl)[0]
            val cellSize = getTcCellSizeWithMergeNum(firstTr)
            for (i in 0 until cellSize) {
                val tc = Tc()
                if (vAlign != null) {
                    // 垂直居中
                    setTcVAlign(tc, vAlign)
                }
                val p = P()
                if (hAlign != null) {
                    val pPr = PPr()
                    val jc = Jc()
                    // 单元格居中对齐
                    jc.`val` = hAlign
                    pPr.jc = jc
                    p.pPr = pPr
                }
                val run = R()
                p.content.add(run)
                tc.content.add(p)
                tr.content.add(tc)
            }
        }
        if (index >= 0 && index < tbl.content.size) {
            tbl.content.add(index, tr)
        } else {
            tbl.content.add(tr)
        }
    }
 
 
    /**
     * @Description: 得到行的列数
     */
    fun getTcCellSizeWithMergeNum(tr: Tr): Int {
        var cellSize = 1
        val tcList = getTrAllCell(tr)
        if (tcList == null || tcList.size == 0) {
            return cellSize
        }
        cellSize = tcList.size
        for (tc in tcList) {
            val tcPr = getTcPr(tc)
            val gridSpan = tcPr.gridSpan
            if (gridSpan != null) {
                cellSize += gridSpan.`val`.toInt() - 1
            }
        }
        return cellSize
    }
 
    /**
     * @Description: 删除指定行 删除后行数减一
     */
    fun removeTrByIndex(tbl: Tbl, index: Int): Boolean {
        var flag = false
        if (index < 0) {
            return flag
        }
        val objList = tbl.content ?: return flag
        var k = -1
        var i = 0
        val len = objList.size
        while (i < len) {
            val obj = XmlUtils.unwrap(objList[i])
            if (obj is Tr) {
                k++
                if (k == index) {
                    tbl.content.removeAt(i)
                    flag = true
                    break
                }
            }
            i++
        }
        return flag
    }
 
    fun getTrPr(tr: Tr): TrPr {
        var trPr: TrPr? = tr.trPr
        if (trPr == null) {
            trPr = TrPr()
            tr.trPr = trPr
        }
        return trPr
    }
 
    /**
     * @Description:隐藏行(只对表格中间的部分起作用,不包括首尾行)
     */
    fun setTrHidden(tr: Tr, hidden: Boolean) {
        val tcList = getTrAllCell(tr)
        for (tc in tcList) {
            setTcHidden(tc, hidden)
        }
    }
 
    /**
     * @Description: 设置单元格宽度
     */
    fun setTcWidth(tc: Tc, width: String) {
        if (StringUtils.isNotBlank(width)) {
            val tcPr = getTcPr(tc)
            var tcW: TblWidth? = tcPr.tcW
            if (tcW == null) {
                tcW = TblWidth()
                tcPr.tcW = tcW
            }
            tcW.w = BigInteger(width)
            tcW.type = "dxa"
        }
    }
 
    /**
     * @Description: 隐藏单元格内容
     */
    fun setTcHidden(tc: Tc, hidden: Boolean) {
        val pList = getTcAllP(tc)
        for (p in pList) {
            val ppr = getPPr(p)
            val objRList = getAllElementFromObject(p, R::class.java) ?: continue
            for (objR in objRList) {
                if (objR is R) {
                    val rpr = getRPr(objR)
                    setRPrVanishStyle(rpr, hidden)
                }
            }
            setParaVanish(ppr, hidden)
        }
    }
 
    fun getTcAllP(tc: Tc): List<P> {
        val objList = getAllElementFromObject(tc, P::class.java)
        val pList = ArrayList<P>()
        if (objList == null) {
            return pList
        }
        for (obj in objList) {
            if (obj is P) {
                pList.add(obj)
            }
        }
        return pList
    }
 
    fun getTcPr(tc: Tc): TcPr {
        var tcPr: TcPr? = tc.tcPr
        if (tcPr == null) {
            tcPr = TcPr()
            tc.tcPr = tcPr
        }
        return tcPr
    }
 
    /**
     * @Description: 设置单元格垂直对齐方式
     */
    fun setTcVAlign(tc: Tc, vAlignType: STVerticalJc?) {
        if (vAlignType != null) {
            val tcPr = getTcPr(tc)
            val vAlign = CTVerticalJc()
            vAlign.`val` = vAlignType
            tcPr.vAlign = vAlign
        }
    }
 
    /**
     * @Description: 设置单元格水平对齐方式
     */
    fun setTcJcAlign(tc: Tc, jcType: JcEnumeration?) {
        if (jcType != null) {
            val pList = getTcAllP(tc)
            for (p in pList) {
                setParaJcAlign(p, jcType)
            }
        }
    }
 
    fun getRPr(r: R): RPr {
        var rpr: RPr? = r.rPr
        if (rpr == null) {
            rpr = RPr()
            r.rPr = rpr
        }
        return rpr
    }
 
    /**
     * @Description: 获取所有的单元格
     */
    fun getTrAllCell(tr: Tr): List<Tc> {
        val objList = getAllElementFromObject(tr, Tc::class.java)
        val tcList = ArrayList<Tc>()
        if (objList == null) {
            return tcList
        }
        for (tcObj in objList) {
            if (tcObj is Tc) {
                tcList.add(tcObj)
            }
        }
        return tcList
    }
 
    /**
     * @Description: 获取单元格内容
     */
    @Throws(Exception::class)
    fun getTcContent(tc: Tc): String {
        return getElementContent(tc)
    }
 
    /**
     * @Description:设置单元格内容,content为null则清除单元格内容
     */
    fun setTcContent(tc: Tc, rpr: RPr, content: String?) {
        val pList = tc.content
        var p: P? = null
        if (pList != null && pList.size > 0) {
            if (pList[0] is P) {
                p = pList[0] as P
            }
        } else {
            p = P()
            tc.content.add(p)
        }
        var run: R? = null
        val rList = p!!.content
        if (rList != null && rList.size > 0) {
            var i = 0
            val len = rList.size
            while (i < len) {
                // 清除内容(所有的r
                p.content.removeAt(0)
                i++
            }
        }
        run = R()
        p.content.add(run)
        if (content != null) {
            val contentArr = content.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            var text = Text()
            text.space = "preserve"
            text.value = contentArr[0]
            run.rPr = rpr
            run.content.add(text)
 
            var i = 1
            val len = contentArr.size
            while (i < len) {
                val br = Br()
                run.content.add(br)// 换行
                text = Text()
                text.space = "preserve"
                text.value = contentArr[i]
                run.rPr = rpr
                run.content.add(text)
                i++
            }
        }
    }
 
    /**
     * @Description:设置单元格内容,content为null则清除单元格内容
     */
    fun removeTcContent(tc: Tc) {
        val pList = tc.content
        var p: P? = null
        if (pList != null && pList.size > 0) {
            if (pList[0] is P) {
                p = pList[0] as P
            }
        } else {
            return
        }
        val rList = p!!.content
        if (rList != null && rList.size > 0) {
            var i = 0
            val len = rList.size
            while (i < len) {
                // 清除内容(所有的r
                p.content.removeAt(0)
                i++
            }
        }
    }
 
    /**
     * @Description:删除指定位置的表格
     */
    @Deprecated("")
    @Throws(Exception::class)
    fun deleteTableByIndex2(wordMLPackage: WordprocessingMLPackage,
                            index: Int) {
        if (index < 0) {
            return
        }
        val xpath = "(//w:tbl)[$index]"
        val jaxbNodes = wordMLPackage.mainDocumentPart
                .getJAXBNodesViaXPath(xpath, true)
        if (jaxbNodes != null && jaxbNodes.size > 0) {
            wordMLPackage.mainDocumentPart.content
                    .remove(jaxbNodes[0])
        }
    }
 
    /**
     * @Description:获取NodeList
     */
    @Deprecated("")
    @Throws(Exception::class)
    fun getObjectByXpath(wordMLPackage: WordprocessingMLPackage,
                         xpath: String): List<Any> {
        return wordMLPackage.mainDocumentPart
                .getJAXBNodesViaXPath(xpath, true)
    }
 
    /*------------------------------------Word 段落相关---------------------------------------------------  */
    /**
     * @Description: 只删除单独的段落,不包括表格内或其他内的段落
     */
    fun removeParaByIndex(wordMLPackage: WordprocessingMLPackage,
                          index: Int): Boolean {
        var flag = false
        if (index < 0) {
            return flag
        }
        val objList = wordMLPackage.mainDocumentPart.content ?: return flag
        var k = -1
        var i = 0
        val len = objList.size
        while (i < len) {
            if (objList[i] is P) {
                k++
                if (k == index) {
                    wordMLPackage.mainDocumentPart.content.removeAt(i)
                    flag = true
                    break
                }
            }
            i++
        }
        return flag
    }
 
    /**
     * @Description: 设置段落水平对齐方式
     */
    fun setParaJcAlign(paragraph: P, hAlign: JcEnumeration?) {
        if (hAlign != null) {
            var pprop: PPr? = paragraph.pPr
            if (pprop == null) {
                pprop = PPr()
                paragraph.pPr = pprop
            }
            val align = Jc()
            align.`val` = hAlign
            pprop.jc = align
        }
    }
 
    /**
     * @Description: 设置段落内容
     */
    fun setParaRContent(p: P, runProperties: RPr, content: String?) {
        var run: R? = null
        val rList = p.content
        if (rList != null && rList.size > 0) {
            var i = 0
            val len = rList.size
            while (i < len) {
                // 清除内容(所有的r
                p.content.removeAt(0)
                i++
            }
        }
        run = R()
        p.content.add(run)
        if (content != null) {
            val contentArr = content.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            var text = Text()
            text.space = "preserve"
            text.value = contentArr[0]
            run.rPr = runProperties
            run.content.add(text)
 
            var i = 1
            val len = contentArr.size
            while (i < len) {
                val br = Br()
                run.content.add(br)// 换行
                text = Text()
                text.space = "preserve"
                text.value = contentArr[i]
                run.rPr = runProperties
                run.content.add(text)
                i++
            }
        }
    }
 
    /**
     * @Description: 添加段落内容
     */
    fun appendParaRContent(p: P, runProperties: RPr, content: String?) {
        if (content != null) {
            val run = R()
            p.content.add(run)
            val contentArr = content.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            var text = Text()
            text.space = "preserve"
            text.value = contentArr[0]
            run.rPr = runProperties
            run.content.add(text)
 
            var i = 1
            val len = contentArr.size
            while (i < len) {
                val br = Br()
                run.content.add(br)// 换行
                text = Text()
                text.space = "preserve"
                text.value = contentArr[i]
                run.rPr = runProperties
                run.content.add(text)
                i++
            }
        }
    }
 
    /**
     * @Description: 添加图片到段落
     */
    @Throws(Exception::class)
    fun addImageToPara(wordMLPackage: WordprocessingMLPackage,
                       factory: ObjectFactory, paragraph: P, filePath: String,
                       content: String?, rpr: RPr, altText: String, id1: Int, id2: Int) {
        val run = factory.createR()
        if (content != null) {
            val text = factory.createText()
            text.value = content
            text.space = "preserve"
            run.rPr = rpr
            run.content.add(text)
        }
 
        val `is` = FileInputStream(filePath)
        val bytes = IOUtils.toByteArray(`is`)
        val imagePart = BinaryPartAbstractImage
                .createImagePart(wordMLPackage, bytes)
        val inline = imagePart.createImageInline(filePath, altText, id1,
                id2, false)
        val drawing = factory.createDrawing()
        drawing.anchorOrInline.add(inline)
        run.content.add(drawing)
        paragraph.content.add(run)
    }
 
    /**
     * @Description: 段落添加Br 页面Break(分页符)
     */
    fun addPageBreak(para: P, sTBrType: STBrType) {
        val breakObj = Br()
        breakObj.type = sTBrType
        para.content.add(breakObj)
    }
 
    /**
     * @Description: 设置段落是否禁止行号(禁止用于当前行号)
     */
    fun setParagraphSuppressLineNum(p: P) {
        val ppr = getPPr(p)
        var line: BooleanDefaultTrue? = ppr.suppressLineNumbers
        if (line == null) {
            line = BooleanDefaultTrue()
        }
        line.isVal = true
        ppr.suppressLineNumbers = line
    }
 
    /**
     * @Description: 设置段落底纹(对整段文字起作用)
     */
    fun setParagraphShdStyle(p: P, shdType: STShd?, shdColor: String) {
        val ppr = getPPr(p)
        var ctShd: CTShd? = ppr.shd
        if (ctShd == null) {
            ctShd = CTShd()
        }
        if (StringUtils.isNotBlank(shdColor)) {
            ctShd.color = shdColor
        }
        if (shdType != null) {
            ctShd.`val` = shdType
        }
        ppr.shd = ctShd
    }
 
    /**
     * @param isSpace
     * 是否设置段前段后值
     * @param before
     * 段前磅数
     * @param after
     * 段后磅数
     * @param beforeLines
     * 段前行数
     * @param afterLines
     * 段后行数
     * @param isLine
     * 是否设置行距
     * @param lineValue
     * 行距值
     * @param sTLineSpacingRule
     * 自动auto 固定exact 最小 atLeast 1磅=20 1行=100 单倍行距=240
     */
    fun setParagraphSpacing(p: P, isSpace: Boolean, before: String,
                            after: String, beforeLines: String, afterLines: String,
                            isLine: Boolean, lineValue: String,
                            sTLineSpacingRule: STLineSpacingRule?) {
        val pPr = getPPr(p)
        var spacing: PPrBase.Spacing? = pPr.spacing
        if (spacing == null) {
            spacing = PPrBase.Spacing()
            pPr.spacing = spacing
        }
        if (isSpace) {
            if (StringUtils.isNotBlank(before)) {
                // 段前磅数
                spacing.before = BigInteger(before)
            }
            if (StringUtils.isNotBlank(after)) {
                // 段后磅数
                spacing.after = BigInteger(after)
            }
            if (StringUtils.isNotBlank(beforeLines)) {
                // 段前行数
                spacing.beforeLines = BigInteger(beforeLines)
            }
            if (StringUtils.isNotBlank(afterLines)) {
                // 段后行数
                spacing.afterLines = BigInteger(afterLines)
            }
        }
        if (isLine) {
            if (StringUtils.isNotBlank(lineValue)) {
                spacing.line = BigInteger(lineValue)
            }
            if (sTLineSpacingRule != null) {
                spacing.lineRule = sTLineSpacingRule
            }
        }
    }
 
    /**
     * @Description: 设置段落缩进信息 1厘米≈567
     */
    fun setParagraphIndInfo(p: P, firstLine: String,
                            firstLineChar: String, hanging: String, hangingChar: String,
                            right: String, rigthChar: String, left: String, leftChar: String) {
        val ppr = getPPr(p)
        var ind: PPrBase.Ind? = ppr.ind
        if (ind == null) {
            ind = PPrBase.Ind()
            ppr.ind = ind
        }
        if (StringUtils.isNotBlank(firstLine)) {
            ind.firstLine = BigInteger(firstLine)
        }
        if (StringUtils.isNotBlank(firstLineChar)) {
            ind.firstLineChars = BigInteger(firstLineChar)
        }
        if (StringUtils.isNotBlank(hanging)) {
            ind.hanging = BigInteger(hanging)
        }
        if (StringUtils.isNotBlank(hangingChar)) {
            ind.hangingChars = BigInteger(hangingChar)
        }
        if (StringUtils.isNotBlank(left)) {
            ind.left = BigInteger(left)
        }
        if (StringUtils.isNotBlank(leftChar)) {
            ind.leftChars = BigInteger(leftChar)
        }
        if (StringUtils.isNotBlank(right)) {
            ind.right = BigInteger(right)
        }
        if (StringUtils.isNotBlank(rigthChar)) {
            ind.rightChars = BigInteger(rigthChar)
        }
    }
 
    fun getPPr(p: P): PPr {
        var ppr: PPr? = p.pPr
        if (ppr == null) {
            ppr = PPr()
            p.pPr = ppr
        }
        return ppr
    }
 
    fun getParaRPr(ppr: PPr): ParaRPr {
        var parRpr: ParaRPr? = ppr.rPr
        if (parRpr == null) {
            parRpr = ParaRPr()
            ppr.rPr = parRpr
        }
        return parRpr
 
    }
 
    fun setParaVanish(ppr: PPr, isVanish: Boolean) {
        val parRpr = getParaRPr(ppr)
        var vanish: BooleanDefaultTrue? = parRpr.vanish
        if (vanish != null) {
            vanish.isVal = isVanish
        } else {
            vanish = BooleanDefaultTrue()
            parRpr.vanish = vanish
            vanish.isVal = isVanish
        }
    }
 
    /**
     * @Description: 设置段落边框样式
     */
    fun setParagraghBorders(p: P, topBorder: CTBorder?,
                            bottomBorder: CTBorder?, leftBorder: CTBorder?, rightBorder: CTBorder?) {
        val ppr = getPPr(p)
        val pBdr = PPrBase.PBdr()
        if (topBorder != null) {
            pBdr.top = topBorder
        }
        if (bottomBorder != null) {
            pBdr.bottom = bottomBorder
        }
        if (leftBorder != null) {
            pBdr.left = leftBorder
        }
        if (rightBorder != null) {
            pBdr.right = rightBorder
        }
        ppr.pBdr = pBdr
    }
 
    /**
     * @Description: 设置字体信息
     */
    fun setFontStyle(runProperties: RPr, cnFontFamily: String,
                     enFontFamily: String, fontSize: String, color: String) {
        setFontFamily(runProperties, cnFontFamily, enFontFamily)
        setFontSize(runProperties, fontSize)
        setFontColor(runProperties, color)
    }
 
    /**
     * @Description: 设置字体大小
     */
    fun setFontSize(runProperties: RPr, fontSize: String) {
        if (StringUtils.isNotBlank(fontSize)) {
            val size = HpsMeasure()
            size.`val` = BigInteger(fontSize)
            runProperties.sz = size
            runProperties.szCs = size
        }
    }
 
    /**
     * @Description: 设置字体
     */
    fun setFontFamily(runProperties: RPr, cnFontFamily: String?,
                      enFontFamily: String?) {
        if (StringUtils.isNotBlank(cnFontFamily) || StringUtils.isNotBlank(enFontFamily)) {
            var rf: RFonts? = runProperties.rFonts
            if (rf == null) {
                rf = RFonts()
                runProperties.rFonts = rf
            }
            if (cnFontFamily != null) {
                rf.eastAsia = cnFontFamily
            }
            if (enFontFamily != null) {
                rf.ascii = enFontFamily
            }
        }
    }
 
    /**
     * @Description: 设置字体颜色
     */
    fun setFontColor(runProperties: RPr, color: String?) {
        if (color != null) {
            val c = Color()
            c.`val` = color
            runProperties.color = c
        }
    }
 
    /**
     * @Description: 设置字符边框
     */
    fun addRPrBorderStyle(runProperties: RPr, size: String,
                          bordType: STBorder?, space: String, color: String) {
        val value = CTBorder()
        if (StringUtils.isNotBlank(color)) {
            value.color = color
        }
        if (StringUtils.isNotBlank(size)) {
            value.sz = BigInteger(size)
        }
        if (StringUtils.isNotBlank(space)) {
            value.space = BigInteger(space)
        }
        if (bordType != null) {
            value.`val` = bordType
        }
        runProperties.bdr = value
    }
 
    /**
     * @Description:着重号
     */
    fun addRPrEmStyle(runProperties: RPr, emType: STEm?) {
        if (emType != null) {
            val em = CTEm()
            em.`val` = emType
            runProperties.em = em
        }
    }
 
    /**
     * @Description: 空心
     */
    fun addRPrOutlineStyle(runProperties: RPr) {
        val outline = BooleanDefaultTrue()
        outline.isVal = true
        runProperties.outline = outline
    }
 
    /**
     * @Description: 设置上标下标
     */
    fun addRPrcaleStyle(runProperties: RPr, vAlign: STVerticalAlignRun?) {
        if (vAlign != null) {
            val value = CTVerticalAlignRun()
            value.`val` = vAlign
            runProperties.vertAlign = value
        }
    }
 
    /**
     * @Description: 设置字符间距缩进
     */
    fun addRPrScaleStyle(runProperties: RPr, indent: Int) {
        val value = CTTextScale()
        value.`val` = indent
        runProperties.w = value
    }
 
    /**
     * @Description: 设置字符间距信息
     */
    fun addRPrtSpacingStyle(runProperties: RPr, spacing: Int) {
        val value = CTSignedTwipsMeasure()
        value.`val` = BigInteger.valueOf(spacing.toLong())
        runProperties.spacing = value
    }
 
    /**
     * @Description: 设置文本位置
     */
    fun addRPrtPositionStyle(runProperties: RPr, position: Int) {
        val ctPosition = CTSignedHpsMeasure()
        ctPosition.`val` = BigInteger.valueOf(position.toLong())
        runProperties.position = ctPosition
    }
 
    /**
     * @Description: 阴文
     */
    fun addRPrImprintStyle(runProperties: RPr) {
        val imprint = BooleanDefaultTrue()
        imprint.isVal = true
        runProperties.imprint = imprint
    }
 
    /**
     * @Description: 阳文
     */
    fun addRPrEmbossStyle(runProperties: RPr) {
        val emboss = BooleanDefaultTrue()
        emboss.isVal = true
        runProperties.emboss = emboss
    }
 
    /**
     * @Description: 设置隐藏
     */
    fun setRPrVanishStyle(runProperties: RPr, isVanish: Boolean) {
        var vanish: BooleanDefaultTrue? = runProperties.vanish
        if (vanish != null) {
            vanish.isVal = isVanish
        } else {
            vanish = BooleanDefaultTrue()
            vanish.isVal = isVanish
            runProperties.vanish = vanish
        }
    }
 
    /**
     * @Description: 设置阴影
     */
    fun addRPrShadowStyle(runProperties: RPr) {
        val shadow = BooleanDefaultTrue()
        shadow.isVal = true
        runProperties.shadow = shadow
    }
 
    /**
     * @Description: 设置底纹
     */
    fun addRPrShdStyle(runProperties: RPr, shdtype: STShd?) {
        if (shdtype != null) {
            val shd = CTShd()
            shd.`val` = shdtype
            runProperties.shd = shd
        }
    }
 
    /**
     * @Description: 设置突出显示文本
     */
    fun addRPrHightLightStyle(runProperties: RPr, hightlight: String) {
        if (StringUtils.isNotBlank(hightlight)) {
            val highlight = Highlight()
            highlight.`val` = hightlight
            runProperties.highlight = highlight
        }
    }
 
    /**
     * @Description: 设置删除线样式
     */
    fun addRPrStrikeStyle(runProperties: RPr, isStrike: Boolean,
                          isDStrike: Boolean) {
        // 删除线
        if (isStrike) {
            val strike = BooleanDefaultTrue()
            strike.isVal = true
            runProperties.strike = strike
        }
        // 双删除线
        if (isDStrike) {
            val dStrike = BooleanDefaultTrue()
            dStrike.isVal = true
            runProperties.dstrike = dStrike
        }
    }
 
    /**
     * @Description: 加粗
     */
    fun addRPrBoldStyle(runProperties: RPr) {
        val b = BooleanDefaultTrue()
        b.isVal = true
        runProperties.b = b
    }
 
    /**
     * @Description: 倾斜
     */
    fun addRPrItalicStyle(runProperties: RPr) {
        val b = BooleanDefaultTrue()
        b.isVal = true
        runProperties.i = b
    }
 
    /**
     * @Description: 添加下划线
     */
    fun addRPrUnderlineStyle(runProperties: RPr,
                             enumType: UnderlineEnumeration) {
        val `val` = U()
        `val`.`val` = enumType
        runProperties.u = `val`
    }
 
    /*------------------------------------Word 相关---------------------------------------------------  */
    /**
     * @Description: 设置分节符 nextPage:下一页 continuous:连续 evenPage:偶数页 oddPage:奇数页
     */
    fun setDocSectionBreak(wordPackage: WordprocessingMLPackage,
                           sectValType: String) {
        if (StringUtils.isNotBlank(sectValType)) {
            val sectPr = getDocSectPr(wordPackage)
            var sectType: SectPr.Type? = sectPr.type
            if (sectType == null) {
                sectType = SectPr.Type()
                sectPr.type = sectType
            }
            sectType.`val` = sectValType
        }
    }
 
    /**
     * @Description: 设置页面背景色
     */
    @Throws(Exception::class)
    fun setDocumentBackGround(wordPackage: WordprocessingMLPackage,
                              factory: ObjectFactory, color: String) {
        val mdp = wordPackage.mainDocumentPart
        var bkground: CTBackground? = mdp.contents.background
        if (StringUtils.isNotBlank(color)) {
            if (bkground == null) {
                bkground = factory.createCTBackground()
                bkground!!.color = color
            }
            mdp.contents.background = bkground
        }
    }
 
    /**
     * @Description: 设置页面边框
     */
    fun setDocumentBorders(wordPackage: WordprocessingMLPackage,
                           factory: ObjectFactory, top: CTBorder?, right: CTBorder?,
                           bottom: CTBorder?, left: CTBorder?) {
        val sectPr = getDocSectPr(wordPackage)
        var pgBorders: SectPr.PgBorders? = sectPr.pgBorders
        if (pgBorders == null) {
            pgBorders = factory.createSectPrPgBorders()
            sectPr.pgBorders = pgBorders
        }
        if (top != null) {
            pgBorders!!.top = top
        }
        if (right != null) {
            pgBorders!!.right = right
        }
        if (bottom != null) {
            pgBorders!!.bottom = bottom
        }
        if (left != null) {
            pgBorders!!.left = left
        }
    }
 
    /**
     * @Description: 设置页面大小及纸张方向 landscape横向
     */
    fun setDocumentSize(wordPackage: WordprocessingMLPackage,
                        factory: ObjectFactory, width: String, height: String,
                        stValue: STPageOrientation?) {
        val sectPr = getDocSectPr(wordPackage)
        var pgSz: SectPr.PgSz? = sectPr.pgSz
        if (pgSz == null) {
            pgSz = factory.createSectPrPgSz()
            sectPr.pgSz = pgSz
        }
        if (StringUtils.isNotBlank(width)) {
            pgSz!!.w = BigInteger(width)
        }
        if (StringUtils.isNotBlank(height)) {
            pgSz!!.h = BigInteger(height)
        }
        if (stValue != null) {
            pgSz!!.orient = stValue
        }
    }
 
    fun getDocSectPr(wordPackage: WordprocessingMLPackage): SectPr {
        return wordPackage.documentModel.sections[0]
                .sectPr
    }
 
    /**
     * @Description:设置页边距
     */
    fun setDocMarginSpace(wordPackage: WordprocessingMLPackage,
                          factory: ObjectFactory, top: String, left: String, bottom: String,
                          right: String) {
        val sectPr = getDocSectPr(wordPackage)
        var pg: SectPr.PgMar? = sectPr.pgMar
        if (pg == null) {
            pg = factory.createSectPrPgMar()
            sectPr.pgMar = pg
        }
        if (StringUtils.isNotBlank(top)) {
            pg!!.top = BigInteger(top)
        }
        if (StringUtils.isNotBlank(bottom)) {
            pg!!.bottom = BigInteger(bottom)
        }
        if (StringUtils.isNotBlank(left)) {
            pg!!.left = BigInteger(left)
        }
        if (StringUtils.isNotBlank(right)) {
            pg!!.right = BigInteger(right)
        }
    }
 
    /**
     * @Description: 设置行号
     * @param distance
     * :距正文距离 1厘米=567
     * @param start
     * :起始编号(0开始)
     * @param countBy
     * :行号间隔
     * @param restartType
     * :STLineNumberRestart.CONTINUOUS(continuous连续编号)<br></br>
     * STLineNumberRestart.NEW_PAGE(每页重新编号)<br></br>
     * STLineNumberRestart.NEW_SECTION(每节重新编号)
     */
    fun setDocInNumType(wordPackage: WordprocessingMLPackage,
                        countBy: String, distance: String, start: String,
                        restartType: STLineNumberRestart?) {
        val sectPr = getDocSectPr(wordPackage)
        var lnNumType: CTLineNumber? = sectPr.lnNumType
        if (lnNumType == null) {
            lnNumType = CTLineNumber()
            sectPr.lnNumType = lnNumType
        }
        if (StringUtils.isNotBlank(countBy)) {
            lnNumType.countBy = BigInteger(countBy)
        }
        if (StringUtils.isNotBlank(distance)) {
            lnNumType.distance = BigInteger(distance)
        }
        if (StringUtils.isNotBlank(start)) {
            lnNumType.start = BigInteger(start)
        }
        if (restartType != null) {
            lnNumType.restart = restartType
        }
    }
 
    /**
     * @Description:设置文字方向 tbRl 垂直
     */
    fun setDocTextDirection(wordPackage: WordprocessingMLPackage,
                            textDirection: String) {
        if (StringUtils.isNotBlank(textDirection)) {
            val sectPr = getDocSectPr(wordPackage)
            var textDir: TextDirection? = sectPr.textDirection
            if (textDir == null) {
                textDir = TextDirection()
                sectPr.textDirection = textDir
            }
            textDir.`val` = textDirection
        }
    }
 
    /**
     * @Description:设置word 垂直对齐方式(Word默认方式都是"顶端对齐")
     */
    fun setDocVAlign(wordPackage: WordprocessingMLPackage,
                     valignType: STVerticalJc?) {
        if (valignType != null) {
            val sectPr = getDocSectPr(wordPackage)
            var valign: CTVerticalJc? = sectPr.vAlign
            if (valign == null) {
                valign = CTVerticalJc()
                sectPr.vAlign = valign
            }
            valign.`val` = valignType
        }
    }
 
    /**
     * @Description:获取文档的可用宽度
     */
    @Throws(Exception::class)
    fun getWritableWidth(wordPackage: WordprocessingMLPackage): Int {
        return wordPackage.documentModel.sections[0]
                .pageDimensions.writableWidthTwips
    }
 
}