解决使用spire操作文件出现红字:Evaluation Warning: The document was created with Spire.Doc fo

2,895 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 9 天,点击查看活动详情

问题描述

今天使用spire依赖进行word文档操作的时候,生成的文件里面头部这样一行红字:

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

image.png

在网上找了很多解决方案,下面分享我的解决方案:

解决问题

修改依赖

这个问题好像是用错了spire依赖:

这是我使用前的:

```sql
		<dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc</artifactId>
            <version>3.7.2</version>
        </dependency>
        
		 <repositories>
		        <repository>
		            <id>com.e-iceblue</id>
		            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
		        </repository>
		    </repositories>

然后修改下依赖,不用这个了,改为:

	<dependency>
         <groupId>e-iceblue</groupId>
         <artifactId>spire.doc.free</artifactId>
         <version>3.9.0</version>
	 </dependency>
	<repositories>
		<repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
	</repositories>

image.png

总结就是把spire.doc换成spire.doc.free,我的理解是换成免费版的

重新运行代码

代码都不用,再次运行,下面是一些引用:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

这次查看效果,红字消失了,问题解决

还有一种方式

使用aspose的License去验证

需要引用aspose包,引入操作我写了一个博客,地址如下

本地创建一个License.xml

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

image.png

然后编写方法

  public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = ConvertPDFUtils.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

在转换的方法里面加入这个方法,值得注意点是aopse里面有针对不同文件有不同的license,别引入错了

这是word的license
import com.aspose.words.License;
这是excel的license
import com.aspose.cells.License;
这是pdf的license
import com.aspose.pdf.License

比如下面这个word转pdf的例子

    public boolean wordConvertPdf(MultipartFile file, String outPath) throws Exception {
        // 验证License
        if (!getLicense()) {
            return false;
        }
        try {
            // 原始word路径
            Document doc = new Document(file.getInputStream());
            // 输出路径
            File pdfFile = new File(outPath);
            // 文件输出流
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            doc.save(fileOS, SaveFormat.PDF);
            fileOS.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



也是一样可以解决的