java uses spire Remove the watermark from the word file generated by Doc (warning information in the header)

1. Demand

Dynamically generate word documents through word templates and data in database tables.

The word operation tool itself carries word to xml (save as xml directly).

However, the requirements are clear. Only word templates can be used, and users are not allowed to manually transfer from word to xml and then upload.

Freemark is used for dynamic data generation. Freemark requires an xml or flt file. Therefore, in combination with the above requirement that xml cannot be transmitted, I use spire. Com as the word template Doc converts the word template to xml, and completes the task of dynamic parsing of xml using freemark

2. Problems

When using spire The word document of DOC generates a warning watermark

(Evaluation Warning: The document was created with Spire.Doc for JAVA.)

3. Problem solving

        3.1. I first looked for a solution from the Internet, but I used the first method to directly report the error, but I didn't find the reason for the error, so I gave up directly. The second method was to give up directly because I had to change the package and have a length limit

Method 1:

/ / reread the generated document
  InputStream is = new FileInputStream("E:\\demo.docx");
        XWPFDocument document = new XWPFDocument(is);
/ / above spire The file generated by Doc will have its own warning message. Here to delete spire Doc warning
        document.removeBodyElement(0);
/ / output word content file stream, new output path location
        OutputStream os=new FileOutputStream("E:\\demo1.docx");
        try {
            document.write(os);
            System.out.println("docx document generated successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
Method 2:

Use free spire Doc for Java is a free version with certain restrictions. The number of paragraphs that can be generated during export cannot exceed a fixed number, but the exported Word has no warning information. It is the spire of the commercial version Only doc for Java will have warning messages
--------
Copyright notice: This is the original article of CSDN blogger "a blank paper", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Original link: https://blog.csdn.net/qq_33745005/article/details/108140983

        3.2. Here is my solution

My first thought is to parse the word in each line into an array, then delete the elements in the first line and output the data to a new file (I'm thinking about how to solve the format problem of word document, but I decided to try it first). After I parse it, I found that the output of word document is a line of xml. So I came up with a new solution: replace the warning in xml directly with an empty string in the output and paste the code

In the following method, I directly operate the generated word or xml when word is converted to xml. The principle and method are exactly the same

//Here is the style of the warning watermark extracted from the parsed xml and its xml tag, which is convenient for replacement below
//If you only replace the text here, there will be blank lines, so replace the whole label directly
private final String WARN = "<w:p><w:pPr /><w:r><w:rPr><w:color w:val=\"FF0000\" /><w:sz w:val=\"24\" /></w:rPr><w:t xml:space=\"preserve\">Evaluation Warning: The document was created with Spire.Doc for JAVA.</w:t></w:r></w:p>";


//Original document
String docName = fileName + uuid + ".doc";
File file = new File(docPath);
/**
 * Clear warning start
 * Create a parsing object. Note that the package here is CN hutool. core. io. file. FileReader
 * This is because the Java. Net that comes with jdk is used here io. The xml parsed by FileReader is incomplete
 */
FileReader fileReader = new FileReader(file);
String str = fileReader.readString();
//replace
str = str.replaceAll(WARN,"");
//Output, I will directly use Java for the package here IO, it's OK to use hutool
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(str);//Eliminate warning end

The following is word to xml

//The bag here is com spire. doc. Document
Document doc = new Document();
doc.loadFromStream(inputStream, FileFormat.Doc);
doc.saveToFile(xmlPath, FileFormat.Word_Xml);
doc.dispose();

jar package pom used

    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc</artifactId>
            <version>3.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.19</version>
        </dependency>
    </dependencies>

4. Summary

The essence of word is actually xml, which should also be the essence of word storage style.

Like to remember the attention points~

Please indicate the source for reprint~

Keywords: Java Back-end

Added by ryan-uk on Sat, 15 Jan 2022 15:22:31 +0200