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

Time:2022-1-23

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. When exporting, the number of paragraphs that can be generated cannot exceed a fixed number, but the exported word has no warning message. It is the spire of the commercial version Only doc for Java will have warning messages
————————————————
Copyright notice: This is the original article of the blogger “a blank paper”, which follows the CC 4.0 by-sa copyright agreement. For reprint, please attach the source link of the original text and this notice.
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 of each line into an array, then delete the elements of 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~