package cn.flightfeather.supervision.common.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
|
}
|
|
}
|