1. 下载jar包或者引入pom.xml
下载:https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html
引入pom.xml:https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
1 <repositories> 2 <repository> 3 <id>com.e-iceblue</id> 4 <name>e-iceblue</name> 5 <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> 6 </repository> 7 </repositories> 8 9 <dependencies> 10 <dependency> 11 <groupId>e-iceblue</groupId> 12 <artifactId>spire.pdf.free</artifactId> 13 <version>2.2.2</version> 14 </dependency> 15 </dependencies>
2. Word 转 PDF
1 public void testWord2Pdf() { 2 Document document = new Document(); 3 document.loadFromFile("C:\Users\Administrator\Desktop\source.docx"); 4 document.saveToFile("C:\Users\Administrator\Desktop\target.pdf", FileFormat.PDF); 5 document.close(); 6 }
3. Word 转 PDF(加密)
1 public void testWord2PdfPwd() { 2 Document document = new Document(); 3 document.loadFromFile("C:\Users\Administrator\Desktop\source.docx"); 4 5 ToPdfParameterList toPdf = new ToPdfParameterList(); 6 String password = "password"; 7 toPdf.getPdfSecurity().encrypt(password, password, PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_128_Bit); 8 9 document.saveToFile("C:\Users\Administrator\Desktop\target.pdf", toPdf); 10 document.close(); 11 }