DocumentConverter class allows you to convert between wide range of document file formats.
You can use static Convert method to easily convert an input document to another format:
DocumentConverter.Convert("MyDocument.docx", DocumentFormat.Pdf);
Convert method also returns an instance of DocumentConverterResult class which has some useful properties:
var result = DocumentConverter.Convert(inputDocument, DocumentFormat.Docx); result.ElapsedTime /* Total elapse time for the document conversion */ result.OutputFiles /* A string array for the paths of the converted (output) document files. When there is only one output file, the value will be a single item array. If the output is a directory (e.g. for DocumentFormat.Web format), the string will have a trailing backslash (\).*/
You can use static CanConvert method to check beforehand if an input document can be directly converted another format.
if (DocumentConverter.CanConvert("MyDocument.docx", DocumentFormat.Pdf)) { // ... }
You can also create an instance of DocumentConverter class with an input document and call instance methods ConvertTo and CanConvertTo. This is useful especially when you want to convert the same input document to several output formats.
var documentConverter = new DocumentConverter("MyDocument.xlsx"); documentConverter.ConvertTo(DocumentFormat.Pdf); documentConverter.ConvertTo("MyDocument.xps"); var result = documentConverter.ConvertTo(DocumentFormat.Jpg); if (documentConverter.CanConvertTo(DocumentFormat.Html)) { // ... }
You can enumerate possible output document formats for a given input document file with EnumeratePossibleOutputFormats method and vice versa with EnumeratePossibleInputFormats method:
foreach (var outputFormat in DocumentConverter.EnumeratePossibleOutputFormats(inputDocument)) { // ... } foreach (var inputFormat in DocumentConverter.EnumeratePossibleInputFormats(outputDocument)) { // ... }