poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ajay <ajay_deshpan...@persistent.co.in>
Subject Correct the formatting while converting a doc file to pdf.
Date Thu, 31 Jul 2014 10:30:46 GMT

I had a requirement where I had to convert a doc file to a pdf file, and for
that I was exploring the POI and FOP projects. I have tried the conversion
using the code snippet below.

	    String templateFile = "/path/to/file.doc";
	    String reportFile = "/path/to/file.pdf";
	    HWPFDocumentCore wordDocument = WordToFoUtils.loadDoc(new
	    WordToFoConverter wordToFoConverter = new
	    wordToFoConverter.processDocument( wordDocument );

            StringWriter stringWriter = new StringWriter();
	    Transformer transformer =
	    transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
	    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
	    transformer.transform(new DOMSource( wordToFoConverter.getDocument() ),
new StreamResult( stringWriter ) );
	    FopFactory fopFactory = FopFactory.newInstance();
	    OutputStream out = new BufferedOutputStream(new FileOutputStream(new
	    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

            transformer = TransformerFactory.newInstance().newTransformer();

	    Source src = new StreamSource(new

	    Result res = new SAXResult(fop.getDefaultHandler());

	    transformer.transform(src, res);

But the formatting of the pdf file is all messed up. So any pointers in
correcting the formatting of the pdf file would help greatly. 
Also I wanted to know if we can use an xsl file while converting the doc
file to an FO file, or if we can in any way convert the doc file to a docx

Thanks in advance.

PS: The word doc I have tried and its generated pdf:

View this message in context: http://apache-poi.1045710.n5.nabble.com/Correct-the-formatting-while-converting-a-doc-file-to-pdf-tp5716155.html
Sent from the POI - Dev mailing list archive at Nabble.com.

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org

View raw message