-5

how to convert ms-document to PDF, is there any example pls share with me.. thanks.

Karthick
  • 29
  • 1
  • 1
  • 2
  • http://stackoverflow.com/questions/26925856/convert-txt-file-to-pdf-using-itext-keep-formatting check this also http://karanbalkar.com/2014/01/convert-text-file-to-pdf-document-in-java/ – Kumar Saurabh Sep 02 '15 at 07:59
  • thanks for your reply.. i will look at that... – Karthick Sep 02 '15 at 08:08

1 Answers1

0

If you are requiered to use POI i guess you should take a look at org.apache.poi.hwpf.converter
I never tried this, but i guess it´s worth a try atleast. It seems like you can use WordToFoConverterto convert your XWPFDocument to a FO-file (example here).
From there you can use apaches FOP to transform the FO-file to a PDF like this:

// Step 1: Construct a FopFactory
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance();

// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:/Temp/myfile.pdf")));

try {
  // Step 3: Construct fop with desired output format
  Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

  // Step 4: Setup JAXP using identity transformer
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = factory.newTransformer(); // identity transformer

  // Step 5: Setup input and output for XSLT transformation
  // Setup input stream
  Source src = new StreamSource(new File("C:/Temp/myfile.fo"));

  // Resulting SAX events (the generated FO) must be piped through to FOP
  Result res = new SAXResult(fop.getDefaultHandler());

  // Step 6: Start XSLT transformation and FOP processing
  transformer.transform(src, res);

} finally {
  //Clean-up
  out.close();
}

This Code was taken from https://xmlgraphics.apache.org/fop/0.95/embedding.html incase you want to read more on this topic.

moli
  • 24
  • 4
  • 2
    No @moli there is no way you can convert your XWPFDocument to a FO file, because the WordToFoConverter is limited to the (old) HWPFDocument... (indeed the link you give IS the example for HWPFDocument only!!!) – maxxyme Nov 22 '16 at 17:40
  • You say "if you are required to use POI" what do you suggest instead ... – James Robinson Nov 20 '17 at 17:52