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
Hello, 

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
FileInputStream(templateFile));
	    WordToFoConverter wordToFoConverter = new
WordToFoConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
	    wordToFoConverter.processDocument( wordDocument );

            StringWriter stringWriter = new StringWriter();
	    Transformer transformer =
TransformerFactory.newInstance().newTransformer();
	    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
File(reportFile)));
	
	    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

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

	    Source src = new StreamSource(new
ByteArrayInputStream(stringWriter.getBuffer().toString().getBytes()));

	    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
file. 


Thanks in advance.

PS: The word doc I have tried and its generated pdf:
2014_04_msw_usltr_format.doc
<http://apache-poi.1045710.n5.nabble.com/file/n5716155/2014_04_msw_usltr_format.doc>
 
2014_04_msw_usltr_format.pdf
<http://apache-poi.1045710.n5.nabble.com/file/n5716155/2014_04_msw_usltr_format.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


Mime
View raw message