using System; using System.Collections.Generic; using System.Text; using Muhimbi.DocumentConverter.WebService.Data; using Muhimbi.Diagnostics; using Aspose.Words; using System.IO; using Aspose.Words.Saving; namespace Muhimbi.DocumentConverter.WebService.CustomConverters { public class WordConverter : Muhimbi.DocumentConverter.WebService.AbstractDocumentConverter { public WordConverter() : base() { } public WordConverter(Stream sourceFile, OpenOptions openOptions, ConversionSettings conversionSettings) : base(sourceFile, openOptions, conversionSettings) { } public override Stream Convert() { try { // ** Validate as certain options are not supported by this converter if (_openOptions.AllowMacros != MacroSecurityOption.None) Logger.Warn("Macros are not supported by this converter."); // ** Set the licences for Aspose.Words. Aspose.Words.License wordLicence = new Aspose.Words.License(); //wordLicence.SetLicense("Enter your license in here."); Document asposeDocument = new Document(_sourceFile, null, LoadFormat.Auto, _openOptions.Password); // ** Do we need to refresh the fields etc? if (_openOptions.RefreshContent == true) asposeDocument.Range.UpdateFields(); // ** Convert the Document to PDF and save it as a memory stream. if (_conversionSettings.Format == OutputFormat.PDF) { MemoryStream convertedStream = new MemoryStream(); PdfOptions options = new PdfOptions(); // ** Specify the PDF Profile if (_conversionSettings.PDFProfile == PDFProfile.PDF_1_5) options.Compliance = PdfCompliance.Pdf15; else options.Compliance = PdfCompliance.PdfA1b; // ** How to deal with bookmarks if (_conversionSettings.GenerateBookmarks == BookmarkGenerationOption.Automatic) options.HeadingsOutlineLevels = 9; else if (_conversionSettings.GenerateBookmarks == BookmarkGenerationOption.Custom) options.BookmarksOutlineLevel = 9; // ** Correct the start and end pages if needed int startPage = _conversionSettings.StartPage != 0 ? _conversionSettings.StartPage - 1 : 0; int pageCount = asposeDocument.PageCount - startPage; if (_conversionSettings.EndPage != 0) pageCount = Math.Min(_conversionSettings.EndPage - startPage, pageCount); // ** Carry out the actual conversion asposeDocument.SaveToPdf(startPage, pageCount, convertedStream, options); return convertedStream; } else { throw new NotSupportedException("Outputformat '" + _conversionSettings.Format + "' not supported by this Converter."); } } catch (UnsupportedFileFormatException ex) { throw new WebServiceInternalException( WebServiceExceptionType.FileFormatNotSupported, ex.Message); } catch (Exception ex) { string message = "An error occurred while converting a file"; if (_openOptions != null && _openOptions.OriginalFileName != null) message += " - " + _openOptions.OriginalFileName; Logger.Error(message, ex); throw new WebServiceInternalException( WebServiceExceptionType.InternalError, message); } } public override DiagnosticResultItem RunDiagnostics() { DiagnosticResultItem dri = new DiagnosticResultItem(); dri.Valid = true; return dri; } } }