none
docx to xls-fo RRS feed

  • Question

  • Hello there I can from word-ui app create first xls and transform it to create a xls-fo file but how can I in command line create xls-fo file from .docx file ? After that I want use apache FOP to create pdf. (this is to try workaround for this issue/requirement  https://answers.microsoft.com/fr-fr/msoffice/forum/msoffice_word-mso_win10-mso_2016/word-to-pdf-conversion-performance/948d266c-6527-45ed-a198-4405b9e453b8?tm=1543490612211)
    Thursday, November 29, 2018 2:09 PM

All replies

  • Hi istiti,

    Please refer to the following code:

    package org.samples.docxconverters.xdocreport.pdf;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    import org.apache.poi.xwpf.converter.pdf.PdfConverter;
    import org.apache.poi.xwpf.converter.pdf.PdfOptions;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
     
    public class HelloWorldToPDF {
     
        public static void main(String[] args) {
            createPDF();
            createPDF();
        }
     
        private static void createPDF() {
            try {
                long start = System.currentTimeMillis();
     
                // 1) Load DOCX into XWPFDocument
                InputStream is = new FileInputStream(new File(
                        "docx/HelloWorld.docx"));
                XWPFDocument document = new XWPFDocument(is);
     
                // 2) Prepare Pdf options
                PdfOptions options = PdfOptions.create();
     
                // 3) Convert XWPFDocument to Pdf
                OutputStream out = new FileOutputStream(new File(
                        "pdf/HelloWorld.pdf"));
                PdfConverter.getInstance().convert(document, out, options);
                 
                System.err.println("Generate pdf/HelloWorld.pdf with "
                        + (System.currentTimeMillis() - start) + "ms");
                 
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    For more information, please see the following links:

    XDocReport (Apache POI XWPF)

    Convert Word document to XSL-FO

    Converting Word documents to XSL-FO

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, November 30, 2018 2:40 AM
  • after trying this code my pdf is always 0octes/corrupted (even with simple pdf with hello world text)
    Friday, November 30, 2018 6:47 AM
  • Instead of adding yet another layer of complexity, as discussed @ MS Answers you should learn to format your documents properly using Styles.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 30, 2018 7:50 AM