feiyu02
2022-11-15 23bd719cebe5feeff4e48fde925b0b39755eea93
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
/*
 *  Copyright 2007-2010, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.
 
    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
 
    You may obtain a copy of the License at 
 
        http://www.apache.org/licenses/LICENSE-2.0 
 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.
 
 */
 
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.samples.AbstractSample;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.PPr;
import org.docx4j.wml.SectPr;
 
import java.io.File;
import java.util.List;
 
/**
 * Create a WordML Pkg and add a header to it.
 * Output is Flat OPC XML.
 * Notice:
 * 1. the Header part
 * 2. the contents of the sectPr element
 * 
 * @author jharrop
 *
 */
public class HeaderFooterNoneOnTitlePage  extends AbstractSample {
 
    private static ObjectFactory objectFactory = new ObjectFactory();
 
    public static void main(String[] args) throws Exception {
        
        try {
            getInputFilePath(args);
        } catch (IllegalArgumentException e) {
            inputfilepath = System.getProperty("user.dir")
                    + "/simpleH.docx";
        }
 
        WordprocessingMLPackage output = WordprocessingMLPackage
                .load(new File(inputfilepath));
 
        List mainContent = output.getMainDocumentPart().getContent(); 
        
        // Add a sectPr        
        org.docx4j.wml.P  par = objectFactory.createP(); 
        mainContent.add(0,par); 
        SectPr sectPr = objectFactory.createSectPr();
        PPr ppr = objectFactory.createPPr();
        ppr.setSectPr(sectPr);
        par.setPPr(ppr);
        
        
        // From the spec, if this element is set to true and the
        // first page header type is omitted, then a blank header
        // shall be created as needed
        sectPr.setTitlePg(new BooleanDefaultTrue());
        
        // Set nextPage on the *next* (!) section
//        output.getDocumentModel().refresh();
//        List<SectionWrapper> sections = output.getDocumentModel().getSections();
//        SectPr nextSectPr = sections.get(1).getSectPr();
//        Type sectionType = objectFactory.createSectPrType();
//        sectionType.setVal("nextPage");
//        nextSectPr.setType(sectionType);
        
 
        // Now add before this sectPr, whatever content you
        // want at the beginning
        org.docx4j.wml.P  yourContent = objectFactory.createP(); 
        mainContent.add(0,yourContent);         
    
        output.save(new File(System.getProperty("user.dir")
                    + "/simpleH_OUT.docx"));
    }
    
 
    
}