分享

java实现word转pdf文件(高效不失真)

 三十的狼 2018-08-04
 2017年11月23日 17:31:14

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import org.aspectj.weaver.ast.Test;
  5. import com.aspose.words.Document;
  6. import com.aspose.words.License;
  7. import com.aspose.words.SaveFormat;
  8. /**
  9. * @author Administrator
  10. * @version $Id$
  11. * @since
  12. * @see
  13. */
  14. public class Word2PdfUtil {
  15. public static void main(String[] args) {
  16. //doc2pdf("C:/Users/lss/Desktop/test.doc");
  17. }
  18. public static boolean getLicense() {
  19. boolean result = false;
  20. try {
  21. InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
  22. License aposeLic = new License();
  23. aposeLic.setLicense(is);
  24. result = true;
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. return result;
  29. }
  30. public static void doc2pdf(String inPath, String outPath) {
  31. if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
  32. return;
  33. }
  34. try {
  35. long old = System.currentTimeMillis();
  36. File file = new File(outPath); // 新建一个空白pdf文档
  37. FileOutputStream os = new FileOutputStream(file);
  38. Document doc = new Document(inPath); // Address是将要被转化的word文档
  39. doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
  40. // EPUB, XPS, SWF 相互转换
  41. long now = System.currentTimeMillis();
  42. System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }

license.xml(复制下面一段破解码,保存为license.xml文件,该文件破解Aspose.Words,使得生产的pdf没有自带的水印):
  1. <License>
  2. <Data>
  3. <Products>
  4. <Product>Aspose.Total for Java</Product>
  5. <Product>Aspose.Words for Java</Product>
  6. </Products>
  7. <EditionType>Enterprise</EditionType>
  8. <SubscriptionExpiry>20991231</SubscriptionExpiry>
  9. <LicenseExpiry>20991231</LicenseExpiry>
  10. <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  11. </Data>
  12. <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
  13. </License>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多