import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
/**
* @author 作者 Jeffy
* @email XXXXXX
* @version
*
*/
public class DocToPdf {
private static Logger logger = LogManager.getLogger(DocToPdf.class);
/**
* 获取license
*
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = DocToPdf.class.getClassLoader().getResourceAsStream("aspose/license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 判断是否为空
* @param obj
* 字符串对象
* @return
*/
protected static boolean notNull(String obj) {
if (obj != null && !obj.equals("") && !obj.equals("undefined")
&& !obj.trim().equals("") && obj.trim().length() > 0) {
return true;
}
return false;
}
/**
* doc to pdf
*
* @param docPath
* doc源文件
* @param pdfPath
* pdf目标文件
*/
public static void doc2PDF(String docPath, String pdfPath) {
try {
// 验证License
if (!getLicense()) {
return;
}
if (notNull(docPath) && notNull(pdfPath)) {
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(docPath);
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
logger.info(pdfPath + "存储成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// DocToPdf word = new DocToPdf();
// word.toWord("c:\test1.docx","c:\signDoc.docx",map);
// word.doc2PDF("D:\report\test\1.doc", "D:\report\test\DoctoPdf.pdf");
}
}
<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>2sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>