doc转pdf之aspose
添加依赖
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>aspose-words</artifactId>
<version>20.10</version>
</dependency>
<dependency>
<groupId>com.aspose.cells</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
代码实现
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* Aspose工具类
*
* @author stsinghua
*/
public enum AsposeUtil {
INSTANCE;
private static final String DEF_CELLS = "cells";
private static final String DEF_WORDS = "words";
private static final Logger logger = LoggerFactory.getLogger(AsposeUtil.class);
/**
* 获取license
*/
public boolean getLicense(String type) {
boolean result = false;
try {
// 凭证
StringBuilder licenseStr = new StringBuilder();
licenseStr.append("<License>\n");
licenseStr.append("<Data>\n");
licenseStr.append("<Products>\n");
licenseStr.append("<Product>Aspose.Total for Java</Product>\n");
licenseStr.append("<Product>Aspose.Words for Java</Product>\n");
licenseStr.append("</Products>\n");
licenseStr.append("<EditionType>Enterprise</EditionType>\n");
licenseStr.append("<SubscriptionExpiry>20991231</SubscriptionExpiry>\n");
licenseStr.append("<LicenseExpiry>20991231</LicenseExpiry>\n");
licenseStr.append("<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n");
licenseStr.append("</Data>\n");
licenseStr.append("<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n");
licenseStr.append("</License>");
InputStream inputStream = new ByteArrayInputStream(licenseStr.toString().getBytes("UTF-8"));
if (DEF_WORDS.equalsIgnoreCase(type)) {
com.aspose.words.License asposeLic = new com.aspose.words.License();
asposeLic.setLicense(inputStream);
} else {
com.aspose.cells.License asposeLic = new com.aspose.cells.License();
asposeLic.setLicense(inputStream);
}
result = true;
} catch (Exception e) {
logger.error("set aspose license error,{}", e);
}
return result;
}
/**
* word 转 pdf.
*/
public void wordToPdf(String docFilePath, String pdfFilePath) {
wordToOther(docFilePath, pdfFilePath, com.aspose.words.SaveFormat.PDF);
}
/**
* xml 转 word.
*/
public void xmlToWord(String xmlFilePath, String docFilePath) {
wordToOther(xmlFilePath, docFilePath, com.aspose.words.SaveFormat.DOC);
}
/**
* docx 转 word.
*/
public void docxToWord(String docxFilePath, String docFilePath) {
wordToOther(docxFilePath, docFilePath, com.aspose.words.SaveFormat.DOC);
}
/**
* word 转 other.
*/
public void wordToOther(String filePath, String toFilePath, int type) {
if (!getLicense(DEF_WORDS)) {
return;
}
try {
Document document = new Document(filePath);
wordToOther(document, toFilePath, type);
} catch (Exception e) {
logger.error("wordToOther error,{}", e);
}
}
/**
* word 转 other.
*/
public void wordToOther(Document document, String docFilePath, int type) {
FileOutputStream fos = null;
if (!getLicense(DEF_WORDS)) {
return;
}
try {
document.acceptAllRevisions();
fos = new FileOutputStream(new File(docFilePath));
document.save(fos, type);
} catch (Exception e) {
logger.error("wordToWord error,{}", e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
logger.error("fos error,{}", e);
}
}
}
/**
* excel 转 pdf.
*/
public void excelToPdf(String excelFilePath, String pdfFilePath) {
FileOutputStream fos = null;
if (!getLicense(DEF_CELLS)) {
return;
}
try {
// pdf输出路径
File pdfFile = new File(pdfFilePath);
// excel路径
Workbook wb = new Workbook(excelFilePath);
wb.acceptAllRevisions();
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
fos = new FileOutputStream(pdfFile);
wb.save(fos, com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
logger.error("excelToPdf error,{}", e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
logger.error("fos error,{}", e);
}
}
}
/**
* 判断书签是否存在
*/
public boolean isExistBookmark(Document dc, String bookmarkStr) {
Bookmark bookmark = dc.getRange().getBookmarks().get(bookmarkStr);
if (null != bookmark && bookmarkStr.equalsIgnoreCase(bookmark.getName())) {
return true;
}
return false;
}
/**
* 在书签处插入文本
*/
public void setTextAtBookMark(Document document, String bookMark, String text) throws Exception {
if (isExistBookmark(document, bookMark)) {
try {
document.getRange().getBookmarks().get(bookMark).setText(text);
} catch (Exception e) {
logger.error("setTextAtBookMark,书签{}赋值异常, error,{}", bookMark, e);
}
}
}
/**
* 在书签处插入图片
*/
public void insertPictureAtBookMark(Document document, String bookMark, String pictureFilePath) throws FileNotFoundException {
if (isExistBookmark(document, bookMark)) {
try {
document.getRange().getBookmarks().get(bookMark).setText("");
DocumentBuilder documentBuilder = new DocumentBuilder(document);
documentBuilder.moveToBookmark(bookMark);
documentBuilder.insertImage(pictureFilePath);
} catch (Exception e) {
logger.error("insertPictureAtBookMark,书签{}赋值异常, error,{}", bookMark, e);
}
}
}
/**
* 移除全部水印
*/
private void removeWatermark(Document doc) throws Exception {
for (Section sect : doc.getSections()) {
removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_PRIMARY);
removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_FIRST);
removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_EVEN);
}
}
/**
* 移除指定Section的水印
*/
private void removeWatermarkFromHeader(Section sect, int headerType) throws Exception {
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header != null) {
header.removeAllChildren();
}
}
}
附录
详情请参考官网:
但管努力,莫问前程,事在人为,功不唐捐。