Where is the end of the humain?

java中word文档相关处理

wordprocessingpackage 处理页脚居中页码,处理 template,处理 word 导出。

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
public byte[] toByte(WordprocessingMLPackage word){
try(ByteArrayOutputStream output = new ByteArrayOutputSream()){
word.save(output);
return output.toByteArray();
} catch(Exception e){
throw new CustomException();
}
}

public byte[] toPdfByte(WordprocessingMLPackage word){
try(ByteArrayOutputStream output = new ByteArrayOutputSream()){
EndNotesPart end = word.getMainDocumentPart().getEndNotesPart();
if(end != null){
endNotePart.remove();
}
Docx4J.toPDF(word,output);
return output.toByteArray();
} catch(Exception e){
throw new CustomException();
}
}

public WordprocessingMLPackage load(String template){
try(InputStream templateInput = this.getClass().getClassLoader().getRessourceAsStream(template)){
WordprocessingMLPackage word = WordprocessingMLPackage.load(templateInput);
VariablePrepare.prepare(word);
return word;
} catch ....
}

public String mapVar(String val) {return Optional.ofNullable(addLineBreak(val).orElse("-"));}

public String addLineBreak(String html){
if(html == null)
return null;

StringTokenizer st = new StringTokenizer(html, '\n\r\f');
StringBuilder sb = new StringBuilder();

boolean first = true;
while(st.hasMoreToken()){
String line = st.nextToken();
if(first){
first = false;
} else {
sb.append("</w:t><w:br/><w:t>");
}
sb.append(line);
}
return sb.toString();
}

public void addPageNumberToFooter(WordprocessingMLPackage word) throws InvalidFormatException {
List<SectionWrapper> sections = word.getDocumentPart().getSection();
if(section.isEmpty()) return;

SectionWrapper section = section.getFirst();
FooterPart foot = section.getHeaderFooterPolicy().getDefaultFooter();

if(foot ==null) {
foot = new FooterPart();
Ftr ftr = objectFactory.create(Ftr);
foot.setJaxbElement(ftr);

Relationship rel = wordprocessingMLPackage.getMaintDocumentPart().addTargetPart(foot);

SectPr sectPr = section.getSectPr();
if(sectPr == null ){
sectPr = objectFactory.createSectPr();
word.getMainDocumentpart().addObject(sectPr);
section.setSectPr(sectPr);
}

FooterReference footerReference = objectFactory.createFooterReference();
footerReference.setType(HdrFtrRef.DEFAULT);
footerReference.setId(rel.getId());

setPr
.getEGHdrFtrReferences()
.removeIf(ref -> ref instanceof FooterReference reference &amp;&amp; reference.getType().equals(HdrFtrRef.DEFAULT));
setPr().getEGHdrFtrReferences().add(footerReference);

Ftr footer = footerPart.getJaxbElement();
P pageNumberParameter = createCenterPageNumberParagraph();
footer.getContent().add(pageNumberParameter);
}
}

P createCenterPageNumberParagraph() {
P p = objectFactory.createP();

PPr ppr = objectFactory.createPPr();
Jc jc = objectFactory.createJc();
jc.setVal(JcEnumeration.CENTER);
ppr.setJc(jc);
p.setPPr(ppr);

R rBegin = objectFactory.createR();
rBegin.getContent().add(objectFactory.createRFldChar(createFldChar(STFldcharType.BEGIN)));

R rInstr = objectFactory.createR();
Text instr = objectFactory.createText();
instr.setValue(" PAGE ");
rInstr.getContent().add(objectFactory.createRInstrText(instr));

R rSeparate = objectFactory.createR();
rSeparate.getContent().add(objectFactory.createRFldChar(STFldCharType.SEPARATE));

R rText = objectFactory.creatR();
rText.getContant()add(createText("1"));

R rEnd = objectFactory.createR();
rEnd.getContent().add(objectFactory.createRFldChar(createFldChar(STFldCharType.END)));

p.getContent().add(rBegin);
p.getContent().add(rInstr);
p.getContent().add(rSeparate);
p.getContent().add(rText);
p.getContent().add(rEnd);

return p;
}

public Text createText(String value){
Text t = objectFactory.createText();
t.setValue(value);
return t;
}

public FldChar createFldChar(STFldCharType type){
FldChar fldChar = objectFactory.createFldChar();
fldChar.setFldCharType(type);
return fldChar;
}

void addPageBreak(MainDocumentPart documentPart){
P paragraph = objectFactory.createP();
R run = objectFactory.createR();
paragraph.getContent().add(run);
Br br = objectFactory.createBr();
run.getContent().add(br);
br.setType(org.docx4j.wml.STBrType.PAGE);
documentPart.addObject(paragraph);
}

public Map<String, String> sanitizeVariables(Map<String, String> variables, List<String> nonSanitizedVars){
variables.forEach((key,v)->{
if(!nonSanitizedVars.contains(key))
variables.put(key,
ofNullable(variables.get(key)).map(...).orElse(null))
});
}

public void replace(Map<String, String> variables, List<String> nonSanitizedVars, WordProcessingMLPackage wpmlp){
try{wpmlp.getMainDocumentPart()
.variableReplace(this.sani...Var..(variables,nonSanitizedVars));
} catch( ...){
...
}
}

© 2026 iattachの小站

Elegant theme by Shiro · Made by Acris with ❤️