package cn.flightfeather.supervision.lightshare.web
|
|
import cn.flightfeather.supervision.common.utils.DateUtil
|
import org.docx4j.dml.wordprocessingDrawing.Inline
|
import org.docx4j.jaxb.Context
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage
|
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType
|
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage
|
import org.docx4j.wml.*
|
import org.springframework.stereotype.Controller
|
import org.springframework.web.bind.annotation.RequestMapping
|
import org.springframework.web.bind.annotation.ResponseBody
|
import org.springframework.web.reactive.function.client.WebClient
|
import springfox.documentation.annotations.ApiIgnore
|
import java.io.File
|
import java.io.FileInputStream
|
import java.math.BigInteger
|
import java.util.*
|
|
@ApiIgnore
|
@Controller
|
@RequestMapping("table")
|
class TableController {
|
|
val client = WebClient.create("http://localhost:8080/table")
|
val t = client.get().retrieve().bodyToMono(String::class.java)
|
val mlPackage = WordprocessingMLPackage.createPackage()
|
val factory = Context.getWmlObjectFactory()
|
|
// var m = StringHttpMessageConverter(Charset.forName("UTF-8"))
|
// var restTemplate = RestTemplateBuilder().additionalMessageConverters(m).build()
|
|
@RequestMapping
|
fun test(): String {
|
println(File("/Users/liwei/docx/htmltable.docx").canonicalPath)
|
return "table"
|
}
|
|
// wc.get().uri("")
|
// .retrieve()
|
// .bodyToFlux(Movie::class.java)
|
// .filter{ it.title.equals("雷神3") }
|
// .flatMap{wc.get().uri("/{id}/events", it.id).retrieve().bodyToFlux(MovieEvent::class.java) }
|
// .subscribe { println(it) }
|
|
@RequestMapping("/test")
|
@ResponseBody
|
fun testdate() {
|
val wordMLPackage = WordprocessingMLPackage.createPackage()
|
wordMLPackage.mainDocumentPart.addAltChunk(AltChunkType.Html, t.block()!!.toString().toByteArray())
|
val time = DateUtil().getTime(Date()).toString()
|
val outputfilepath = File("/Users/liwei/resource/supervision/src/main/resources/public/htmltable.docx")
|
wordMLPackage.save(outputfilepath)
|
//Thread.sleep(1000)
|
//return RedirectView("/htmltable01.docx")
|
}
|
|
@RequestMapping("/test1")
|
@ResponseBody
|
fun testdate1() {
|
|
val wordMLPackage = mlPackage
|
|
val table = factory.createTbl()
|
addBorders(table)
|
|
val tr = factory.createTr()
|
|
val paragraphOfText = wordMLPackage.mainDocumentPart.createParagraphOfText("Field 1")
|
addTableCell(tr, paragraphOfText)
|
val time = DateUtil().getTime(Date())
|
val file = File("/Users/liwei/resource/supervision/src/main/resources/static/images/tutu.png")
|
val paragraphWithImage = addInlineImageToParagraph(createInlineImage(file))
|
addTableCell(tr, paragraphWithImage)
|
|
table.content.add(tr)
|
val outputfilepath = File("/Users/liwei/resource/supervision/src/main/resources/public/htmltable$time.docx")
|
wordMLPackage.mainDocumentPart.addObject(table)
|
wordMLPackage.save(outputfilepath)
|
//Thread.sleep(1000)
|
//return RedirectView("/htmltable$time.docx")
|
|
}
|
|
@Throws(Exception::class)
|
private fun createInlineImage(file: File): Inline {
|
val bytes = convertImageToByteArray(file)
|
|
val imagePart = BinaryPartAbstractImage.createImagePart(mlPackage, bytes)
|
|
val docPrId = 1
|
val cNvPrId = 2
|
|
return imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false)
|
}
|
|
|
private fun addInlineImageToParagraph(inline: Inline): P {
|
// 添加内联对象到一个段落中
|
val factory = ObjectFactory()
|
val paragraph = factory.createP()
|
val run = factory.createR()
|
paragraph.getContent().add(run)
|
val drawing = factory.createDrawing()
|
run.content.add(drawing)
|
drawing.anchorOrInline.add(inline)
|
return paragraph
|
}
|
|
private fun addTableCell(tr: Tr, paragraph: P) {
|
val tc1 = factory.createTc()
|
tc1.content.add(paragraph)
|
tr.content.add(tc1)
|
}
|
|
private fun convertImageToByteArray(file: File): ByteArray {
|
val input = FileInputStream(file)
|
val length = file.length()
|
// 不能使用long类型创建数组, 需要用int类型.
|
if (length > Integer.MAX_VALUE) {
|
System.out.println("File too large!!");
|
}
|
val bytes = ByteArray(length.toInt())
|
var offset = 0
|
var numRead = 0
|
// numRead = input.read(bytes, offset, bytes.size - offset)
|
while (offset < bytes.size && numRead >= 0) {
|
numRead = input.read(bytes, offset, bytes.size - offset)
|
offset += numRead
|
}
|
// 确认所有的字节都没读取
|
if (offset < bytes.size) {
|
System.out.println("Could not completely read file " + file.name)
|
}
|
input.close()
|
return bytes
|
}
|
|
private fun addBorders(table: Tbl) {
|
table.tblPr = TblPr()
|
|
val border = CTBorder()
|
border.color = "auto"
|
border.sz = BigInteger("4")
|
border.space = BigInteger("0")
|
border.`val` = STBorder.SINGLE
|
|
val borders = TblBorders()
|
borders.bottom = border
|
borders.left = border
|
borders.top = border
|
borders.right = border
|
borders.insideH = border
|
borders.insideV = border
|
|
table.tblPr.tblBorders = borders
|
}
|
|
|
}
|