/// <summary> /// parse a word document and build a kinesis document model /// </summary> /// <param name="path">full path of the word document</param> /// <returns>equivalent kinesis document model</returns> KineSis.ContentManagement.Model.Document DocumentHelper.ParseNewDocument(String path, ProcessingProgress pp) { //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.OverallOperationName = "All Document Pages"; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// OpenOfficeApplication(); //directory where all the data will be saved String folderName = DocumentUtil.GenerateDirectoryName(); String documentPath = System.IO.Path.Combine(DocumentService.TEMP_DIRECTORY, folderName); System.IO.Directory.CreateDirectory(documentPath); // Make this instance of word invisible (Can still see it in the taskmgr). wordApplication.Visible = false; // Interop requires objects. object oMissing = System.Reflection.Missing.Value; object isVisible = false; object readOnly = false; object oInput = path; object oOutput = documentPath + DD + DOC_FILES + DD + "document.xps"; object oFormat = WdSaveFormat.wdFormatXPS; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationName = "Opening MS Office"; pp.CurrentOperationTotalElements = 1; pp.CurrentOperationElement = 0; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// // Load a document into our instance of word.exe Microsoft.Office.Interop.Word._Document wdoc = wordApplication.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // Make this document the active document. wdoc.Activate(); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 1; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// KineSis.ContentManagement.Model.Document document = new KineSis.ContentManagement.Model.Document(); document.Name = wdoc.Name; document.Location = folderName; //write directory for pages String pagesPath = documentPath + DD + DOC_FILES; System.IO.Directory.CreateDirectory(pagesPath); //create a new page KineSis.ContentManagement.Model.Page page = new KineSis.ContentManagement.Model.Page(); page.Name = wdoc.Name; page.Location = documentPath + DD + DOC_FILES + DD + "document.html"; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationName = "Saving document"; pp.CurrentOperationTotalElements = 2; pp.CurrentOperationElement = 0; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// // Save this document in XPS format. wdoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 1; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// wdoc.Close(SaveChanges: false); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 2; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// try { wdoc.Close(SaveChanges: false); } catch (Exception) { } //CloseOfficeApplication(); //wordApplication.Quit(ref oMissing, ref oMissing, ref oMissing); BuildDocumentHTMLArgs args = new BuildDocumentHTMLArgs(documentPath + DD + DOC_FILES, pp); Thread thread = new Thread(new ParameterizedThreadStart(BuildDocumentHTML)); thread.SetApartmentState(ApartmentState.STA); thread.Start(args); thread.Join(); //build a html page from saved xps //BuildDocumentHTML(documentPath + DD + DOC_FILES, pp); //add page to document model document.Pages.Add(page); //delete the generated xps file FileInfo fi = new FileInfo(documentPath + DD + DOC_FILES + DD + "document.xps"); fi.Delete(); //return built document return(document); }
/// <summary> /// parse a word document and build a kinesis document model /// </summary> /// <param name="path">full path of the word document</param> /// <returns>equivalent kinesis document model</returns> KineSis.ContentManagement.Model.Document DocumentHelper.ParseNewDocument(String path, ProcessingProgress pp) { //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.OverallOperationName = "All Document Pages"; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// OpenOfficeApplication(); //directory where all the data will be saved String folderName = DocumentUtil.GenerateDirectoryName(); String documentPath = System.IO.Path.Combine(DocumentService.TEMP_DIRECTORY, folderName); System.IO.Directory.CreateDirectory(documentPath); // Make this instance of word invisible (Can still see it in the taskmgr). wordApplication.Visible = false; // Interop requires objects. object oMissing = System.Reflection.Missing.Value; object isVisible = false; object readOnly = false; object oInput = path; object oOutput = documentPath + DD + DOC_FILES + DD + "document.xps"; object oFormat = WdSaveFormat.wdFormatXPS; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationName = "Opening MS Office"; pp.CurrentOperationTotalElements = 1; pp.CurrentOperationElement = 0; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// // Load a document into our instance of word.exe Microsoft.Office.Interop.Word._Document wdoc = wordApplication.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // Make this document the active document. wdoc.Activate(); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 1; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// KineSis.ContentManagement.Model.Document document = new KineSis.ContentManagement.Model.Document(); document.Name = wdoc.Name; document.Location = folderName; //write directory for pages String pagesPath = documentPath + DD + DOC_FILES; System.IO.Directory.CreateDirectory(pagesPath); //create a new page KineSis.ContentManagement.Model.Page page = new KineSis.ContentManagement.Model.Page(); page.Name = wdoc.Name; page.Location = documentPath + DD + DOC_FILES + DD + "document.html"; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationName = "Saving document"; pp.CurrentOperationTotalElements = 2; pp.CurrentOperationElement = 0; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// // Save this document in XPS format. wdoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 1; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// wdoc.Close(SaveChanges: false); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 2; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// try { wdoc.Close(SaveChanges: false); } catch (Exception) { } //CloseOfficeApplication(); //wordApplication.Quit(ref oMissing, ref oMissing, ref oMissing); BuildDocumentHTMLArgs args = new BuildDocumentHTMLArgs(documentPath + DD + DOC_FILES, pp); Thread thread = new Thread(new ParameterizedThreadStart(BuildDocumentHTML)); thread.SetApartmentState(ApartmentState.STA); thread.Start(args); thread.Join(); //build a html page from saved xps //BuildDocumentHTML(documentPath + DD + DOC_FILES, pp); //add page to document model document.Pages.Add(page); //delete the generated xps file FileInfo fi = new FileInfo(documentPath + DD + DOC_FILES + DD + "document.xps"); fi.Delete(); //return built document return document; }
/// <summary> /// build a html document based on an xps file /// </summary> /// <param name="path">path where the xps will be found and the html will be generated</param> private void BuildDocumentHTML(object args) { BuildDocumentHTMLArgs bdha = args as BuildDocumentHTMLArgs; String path = bdha.path; ProcessingProgress pp = bdha.pp; XpsDocument xpsDoc = new XpsDocument(path + DD + "document.xps", System.IO.FileAccess.Read, CompressionOption.Normal); FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence(); System.IO.StreamWriter fileO = new System.IO.StreamWriter(path + DD + "document.html", false); fileO.WriteLine("<html><body>"); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.OverallOperationTotalElements = docSeq.DocumentPaginator.PageCount; pp.OverallOperationElement = 0; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// // You can get the total page count from docSeq.PageCount for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; ++pageNum) { //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationName = "Page " + (pageNum + 1); pp.CurrentOperationTotalElements = 3; pp.CurrentOperationElement = 0; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum); BitmapImage bitmap = new BitmapImage(); RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)docPage.Size.Width, (int)docPage.Size.Height, 96, // WPF (Avalon) units are 96dpi based 96, System.Windows.Media.PixelFormats.Default); renderTarget.Render(docPage.Visual); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderTarget)); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 1; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// FileStream pageOutStream = new FileStream(path + DD + "page" + (pageNum + 1) + ".png", FileMode.Create, FileAccess.Write); encoder.Save(pageOutStream); pageOutStream.Close(); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 2; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// fileO.WriteLine("<div align=\"center\"><img src=\"" + path + DD + "page" + (pageNum + 1) + ".png\"/></div>"); //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// pp.CurrentOperationElement = 3; pp.OverallOperationElement = pageNum + 1; //~~~~~~~~~~~~~progress~~~~~~~~~~~~~// } fileO.WriteLine("</body></html>"); fileO.Flush(); fileO.Close(); docSeq = null; xpsDoc.Close(); xpsDoc = null; }