How can I convert a Word document to PDF?

前端 未结 11 693
深忆病人
深忆病人 2020-11-28 20:39

How can I convert a Word document to PDF where the document contains various things, such as tables. When trying to use iText, the original document looks different to the c

相关标签:
11条回答
  • 2020-11-28 21:05

    I agree with posters listing OpenOffice as a high-fidelity import/export facility of word / pdf docs with a Java API and it also works across platforms. OpenOffice import/export filters are pretty powerful and preserve most formatting during conversion to various formats including PDF. Docmosis and JODReports value-add to make life easier than learning the OpenOffice API directly which can be challenging because of the style of the UNO api and the crash-related bugs.

    0 讨论(0)
  • 2020-11-28 21:06

    I think JOD Converter is easiest way to implement, Please refer below link for more information.

    http://mytechbites.blogspot.in/2014/10/convert-documents-to-pdf-in-java.html

    0 讨论(0)
  • 2020-11-28 21:10

    unoconv, it's a python tool worked in UNIX. While I use Java to invoke the shell in UNIX, it works perfect for me. My source code : UnoconvTool.java. Both JODConverter and unoconv are said to use open office/libre office.

    docx4j/docxreport, POI, PDFBox are good but they are missing some formats in conversion.

    0 讨论(0)
  • 2020-11-28 21:11

    Using JACOB call Office Word is a 100% perfect solution. But it only supports on Windows platform because need Office Word installed.

    1. Download JACOB archive (the latest version is 1.19);
    2. Add jacob.jar to your project classpath;
    3. Add jacob-1.19-x32.dll or jacob-1.19-x64.dll (depends on your jdk version) to ...\Java\jdk1.x.x_xxx\jre\bin
    4. Using JACOB API call Office Word to convert doc/docx to pdf.

      public void convertDocx2pdf(String docxFilePath) {
      File docxFile = new File(docxFilePath);
      String pdfFile = docxFilePath.substring(0, docxFilePath.lastIndexOf(".docx")) + ".pdf";
      
      if (docxFile.exists()) {
          if (!docxFile.isDirectory()) { 
              ActiveXComponent app = null;
      
              long start = System.currentTimeMillis();
              try {
                  ComThread.InitMTA(true); 
                  app = new ActiveXComponent("Word.Application");
                  Dispatch documents = app.getProperty("Documents").toDispatch();
                  Dispatch document = Dispatch.call(documents, "Open", docxFilePath, false, true).toDispatch();
                  File target = new File(pdfFile);
                  if (target.exists()) {
                      target.delete();
                  }
                  Dispatch.call(document, "SaveAs", pdfFile, 17);
                  Dispatch.call(document, "Close", false);
                  long end = System.currentTimeMillis();
                  logger.info("============Convert Finished:" + (end - start) + "ms");
              } catch (Exception e) {
                  logger.error(e.getLocalizedMessage(), e);
                  throw new RuntimeException("pdf convert failed.");
              } finally {
                  if (app != null) {
                      app.invoke("Quit", new Variant[] {});
                  }
                  ComThread.Release();
              }
          }
      }
      

      }

    0 讨论(0)
  • 2020-11-28 21:14

    It's already 2019, I can't believe still no easiest and conveniencest way to convert the most popular Micro$oft Word document to Adobe PDF format in Java world.

    I almost tried every method the above answers mentioned, and I found the best and the only way can satisfy my requirement is by using OpenOffice or LibreOffice. Actually I am not exactly know the difference between them, seems both of them provide soffice command line.

    My requirement is:

    1. It must run on Linux, more specifically CentOS, not on Windows, thus we cannot install Microsoft Office on it;
    2. It must support Chinese character, so ISO-8859-1 character encoding is not a choice, it must support Unicode.

    First thing came in mind is doc-to-pdf-converter, but it lacks of maintenance, last update happened 4 years ago, I will not use a nobody-maintain-solution. Xdocreport seems a promising choice, but it can only convert docx, but not doc binary file which is mandatory for me. Using Java to call OpenOffice API seems good, but too complicated for such a simple requirement.

    Finally I found the best solution: use OpenOffice command line to finish the job:

    Runtime.getRuntime().exec("soffice --convert-to pdf -outdir . /path/some.doc");
    

    I always believe the shortest code is the best code (of course it should be understandable), that's it.

    0 讨论(0)
提交回复
热议问题