import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Slf4j
@Component
public class Word2PdfAsposeUtil {
public void docToPdf(String docPath, HttpServletResponse response) throws FileNotFoundException {
FileInputStream inputStream = new FileInputStream(docPath);
docToPdf(inputStream, response);
}
public void docToPdf(MultipartFile file, HttpServletResponse response) throws IOException {
InputStream inputStream = file.getInputStream();
this.docToPdf(inputStream, response);
}
public void docToPdf(InputStream inputStream, HttpServletResponse response) throws FileNotFoundException {
String path = this.getClass().getClassLoader().getResource("").getPath();
File fileLicense = new File(path + "license.xml");
InputStream is = new FileInputStream(fileLicense);
OutputStream outputStream = null;
try {
try {
// 去除水印
License license = new License();
license.setLicense(is);
} catch (Exception e) {
log.error("docToPdf去除水印失败");
}
//到处
Document document = new Document(inputStream);
response.reset();
outputStream = response.getOutputStream();
document.save(outputStream, SaveFormat.PDF);
} catch (Exception e) {
log.error("docToPdf转化失败:" + e.getMessage());
} finally {
try {
outputStream.flush();
outputStream.close();
// is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// public File docToPdf(InputStream docPathInputStream, String pdfPath) {
// File pdfFile = new File(pdfPath);
// ByteArrayInputStream is = new ByteArrayInputStream(licenseStr.getBytes());
// try {
// License license = new License();
// license.setLicense(is);
// Document document = new Document(docPathInputStream);
// FileOutputStream outputStream = new FileOutputStream(pdfFile);
// document.save(outputStream, SaveFormat.PDF);
// outputStream.close();
// is.close();
// } catch (Exception e) {
// System.out.println("转化失败");
// e.printStackTrace();
// }
// return pdfFile;
// }
}
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>
依赖jar包
<dependency>
<groupId>com.aspose.cells</groupId>
<artifactId>aspose-cells</artifactId>
<version>20.4-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/../../start/src/main/resources/libs/aspose-cells-20.4 - c.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>aspose-words</artifactId>
<version>19.5-RELEASE</version>
<scope>system</scope>
<systemPath>${project.basedir}/../../start/src/main/resources/libs/aspose-words-18.8.jar</systemPath>
</dependency>