在word文档拆分时遇到的不兼容问题,.docx文档可以读取,.doc文档读取报错,需要进一步转换,借助aspose-words-15.8.0-jdk16.jar和license.xml实现文档格式转换,具体实现如下:首先引入aspose-words-15.8.0-jdk16.jar
package com.ideal.news;
import java.io.*;
import com.aspose.words.*;
import junit.framework.Test;
/**
* 文档转换格式工具类.
*/
public class WordToPDF {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream(
"license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
// 验证License
if (!getLicense()) {
return;
}
String path = "d:\\Nginx安装.doc";
InputStream is = null;
try {
is = new FileInputStream(path);
Document doc = new Document(is);
doc.save("d:\\Nginx安装.docx");//转pdf、docx都可以
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
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>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
</Data>
<Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>