Convert Document to Other Formats
Microsoft Word - Conversion to Other Formats
Documents can be converted to other formats using Save As option in the File menu.
Aspose.Words - Conversion to Other Formats
To convert a document to other formats using Aspose.Words, simply invoke the save() method of Document class and specify a file name with the desired and supported extension.
Aspose.Words allows exporting or converting word documents to PDF, XPS, XamlFixed, XamlFlow, XamlFlowPack, EPUB, HTML, HtmlFixed, OpenXPS, PostScript, TXT and popular image/multimedia file formats including TIFF, JPG, PNG, BMP, SVG, SVG and SWF.
The following code example demonstrates how to convert a whole document from DOC to other formats using default options. Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AsposeConvertToFormats.class);
// Load the document from disk.
Document doc = new Document(dataDir + "document.doc");
doc.save(dataDir + "Aspose_DocToHTML.html",SaveFormat.HTML); //Save the document in HTML format.
doc.save(dataDir + "Aspose_DocToPDF.pdf",SaveFormat.PDF); //Save the document in PDF format.
doc.save(dataDir + "Aspose_DocToTxt.txt",SaveFormat.TEXT); //Save the document in TXT format.
doc.save(dataDir + "Aspose_DocToJPG.jpg",SaveFormat.JPEG); //Save the document in JPEG format.
Apache POI WP - Conversion to Other Formats
The WordToHtmlConverter is used to convert a DOC to HTML format using Apache POI. Apache POI also allows to covert in FO and text format.
Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ApacheConvertToFormats.class);
HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream(dataDir + "document.doc"));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
FileOutputStream outputStream = new FileOutputStream(dataDir + "Apache_DocToHTML.html");
outputStream.write(out.toByteArray());
outputStream.close();