feiyu02
2024-09-10 6c7f45871b93ef26d353a5a3596701ac2f39ed9c
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
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
    }
 
 
}