The previous method of converting PDF is too limited, only docX format can be converted and the content in the table is easy to lose data. As a result, I spent time to optimize it later. The advantage of this method is that both doc and DOCX files can be converted, but the disadvantage is that intermediate temporary files need to be generated, which consumes additional IO resources. Because my business needs to upload PDFS to a file server, not locally. If be, save the word to this locality to have no matter.
The cracking package of Aspose is used. The license varies with the version of this package.
Rely on
<! -- https://mvnrepository.com/artifact/com.aspose.words/aspose-words-jdk16 --> <dependency> < the groupId > com. Aspose. Words < / groupId > < artifactId > aspose - words - jdk16 < / artifactId > < version > 15.8.0 < / version > < / dependency >Copy the code
The document constructor for aspose has a variety of methods. The demo uses a path parameter, and the project uses a stream parameter that reads a file on the file server over the network and turns it into an input stream.
- Document Document = new Document()
- The argument is path: Document Document = new Document(String fileName)
- The argument is Stream: Document Document = new Document(InputStream Stream)
Code implementation
package com.topdf.demo.demo.util; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; Public class Word2PdfUtil {/** * word to PDF * @param docPath source file * @param savePath saved PDF file */ public static void Word2pdf (String docPath,String savePath){//aspose Cracking JAR packages Different JAR packages correspond to different licenses String licenseStr = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseE xpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGM UdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7 aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>"; ByteArrayInputStream is = null; FileOutputStream fos = null; try { is = new ByteArrayInputStream(licenseStr.getBytes()); fos = new FileOutputStream(new File(savePath)); License license = new License(); license.setLicense(is); Document document = new Document(docPath); // Generate a PDF file document.save(fos, saveformat.pdf); } catch (Exception e) { e.printStackTrace(); }finally { try { if(is ! = null){ is.close(); } if(fos ! = null){ fos.close(); } }catch (Exception e){ e.printStackTrace(); @param args */ public static void main(String[] args){// srcPath = "C: \ \ Users \ \ hy \ \ Desktop \ \ test. Doc"; String desPath = "D:\ test.pdf "; word2pdf(srcPath,desPath); System.out.println(thread.currentThread ()+" execute successfully "); File File = new File(desPath); if(file.exists()){ boolean delete = file.delete(); System.out.println(thread.currentThread ()+" delete return "+delete); }}}Copy the code