/// <summary> /// Converts Html file into OneNote Section of Pages /// </summary> /// <param name="inputFile"></param> /// <param name="originalPicFolder"></param> /// <param name="auxPicFolder"></param> /// <param name="onGenerator"></param> /// <param name="sectionId"></param> /// <returns></returns> protected virtual bool ConvertHtmlToOneNote(string inputFile, string originalPicFolder, string auxPicFolder, OneNoteGenerator onGenerator, string sectionId) { var retVal = false; var htmlContent = string.Empty; try { using (var sr = new StreamReader(inputFile, Encoding.Default)) { htmlContent = sr.ReadToEnd(); } var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlContent); //Preserve the HTML surrounding tags var content = htmlDoc.DocumentNode; var previousPageTitle = string.Empty; //store error info var errorList = new List <Dictionary <InfoType, string> >(); foreach (var page in SeparatePages(content)) { if (page != null) { //Get page title var pageTitle = GetPageTitle(page); //Create the page var pageId = onGenerator.CreatePage(pageTitle, sectionId); //error info var errorInfo = new Dictionary <InfoType, string>(); //Add the content of the page var pageContent = GeneratePageContent(content, page, originalPicFolder, auxPicFolder, errorInfo); onGenerator.AddPageContentAsHtmlBlock(pageId, pageContent); //Attempt to do subpage if (!pageTitle.Equals(DefaultPageTitle, StringComparison.CurrentCultureIgnoreCase) && pageTitle.Equals(previousPageTitle, StringComparison.CurrentCultureIgnoreCase)) { onGenerator.SetSubPage(sectionId, pageId); } previousPageTitle = pageTitle; if (errorInfo.Count > 0) { errorInfo.Add(InfoType.Id, pageId); errorInfo.Add(InfoType.Title, pageTitle); errorList.Add(errorInfo); } } } if (errorList.Count > 0) { CreateErrorPage(onGenerator, sectionId, errorList); } retVal = true; } catch (Exception e) { Console.WriteLine(e.Message); retVal = false; } return(retVal); }
/// <summary> /// Converts Html file into OneNote Section of Pages /// </summary> /// <param name="inputFile"></param> /// <param name="originalPicFolder"></param> /// <param name="auxInputFolder"></param> /// <param name="onGenerator"></param> /// <param name="sectionId"></param> /// <returns></returns> protected override bool ConvertHtmlToOneNote(string inputFile, string originalPicFolder, string auxInputFolder, OneNoteGenerator onGenerator, string sectionId) { var retVal = false; try { string htmlContent; using (var sr = new StreamReader(inputFile, Encoding.Default)) { htmlContent = sr.ReadToEnd(); } var doc = new HtmlDocument(); doc.LoadHtml(htmlContent); var content = doc.DocumentNode; //outer html contains format information var htmlBox = doc.DocumentNode; //list of onenote page Id var pageIdList = new List <string>(); //list of onenote page/subpage name var pageNameList = new List <string>(); //separate the whole doc into pages var pages = SeparatePages(content) as List <List <HtmlNode> >; //get chapter names from table contents var chapterNameList = new List <string>(); //may produce problem by calling first() //maybe empty page with pageNameList[0] = null, fix later var tableContent = pages.FirstOrDefault(); if (tableContent != null) { foreach (var node in tableContent) { chapterNameList.Add(node.InnerText.Replace("\r\n", " ").Trim()); } } //store errors occurred during conversion var errorList = new List <Dictionary <InfoType, string> >(); //print pages to onenote foreach (var page in pages) { var pageTitle = GetPageTitle(page); var pageId = onGenerator.CreatePage(pageTitle, sectionId); var errorInfo = new Dictionary <InfoType, string>(); var pageContent = GeneratePageContent(htmlBox, page, originalPicFolder, auxInputFolder, errorInfo); onGenerator.AddPageContentAsHtmlBlock(pageId, pageContent); pageIdList.Add(pageId); pageNameList.Add(pageTitle); if (errorInfo.Count > 0) { errorInfo.Add(InfoType.Id, pageId); errorInfo.Add(InfoType.Title, pageTitle); errorList.Add(errorInfo); } } if (errorList.Count > 0) { CreateErrorPage(onGenerator, sectionId, errorList); } //set some pages as subpages according to observed rules var lastSeenChapterName = String.Empty; for (int i = 0; i < pageIdList.Count; i++) { var pageId = pageIdList[i]; var name = pageNameList[i]; //check if the page name is a chapter name (a substring of table content items) var isChapterName = chapterNameList.Any(x => x.Contains(name)); bool isSubpage = !isChapterName && name != lastSeenChapterName; //if it is a new chapter name, set it as a page //if it is not a chapter name, but it is the first of consecutive same name pages, set it as a page if (isChapterName == false && i > 0 && name != pageNameList[i - 1] && i < pageNameList.Count && name == pageNameList[i + 1]) { isSubpage = false; } if (isSubpage) { onGenerator.SetSubPage(sectionId, pageId); } if (isChapterName) { lastSeenChapterName = name; } } retVal = true; } catch (Exception e) { Console.WriteLine(e.Message); retVal = false; } return(retVal); }
/// <summary> /// Converts the InDesign document input file into OneNote /// </summary> /// <param name="inputFile"></param> /// <param name="outputDir"></param> /// <returns></returns> public virtual bool ConvertInDesignToOneNote(string inputFile, string outputDir) { #if INDESIGN_INSTALLED // get the file name string inputFileName = Path.GetFileNameWithoutExtension(inputFile); // gets the InDesign App Type inDesignAppType = Type.GetTypeFromProgID(ConfigurationManager.AppSettings.Get("InDesignProgId")); if (inDesignAppType == null) { throw new InvalidOperationException("Failed to find InDesign application. Please ensure that it is installed and is running on your machine."); } InDesignApplication app = (InDesignApplication)Activator.CreateInstance(inDesignAppType, true); // open the indd file try { app.Open(inputFile); } catch (Exception e) { Console.WriteLine(@"Error in ConvertInDesignToOneNote for file {0}: {1}", inputFile, e.Message); return(false); } InDesignDocument doc = app.ActiveDocument; // temp directory for html string htmlDir = Utility.CreateDirectory(Utility.NewFolderPath(outputDir, "html")); // Get the file name string htmlFile = Utility.NewFilePath(htmlDir, inputFileName, HtmlFileExtension); // Save the html file SetInDesignHtmlExportOptions(doc); doc.Export(idExportFormat.idHTML, htmlFile); // Create a new OneNote Notebook var note = new OneNoteGenerator(outputDir); string notebookId = note.CreateNotebook(GetSupportedInputFormat()); string sectionId = note.CreateSection(inputFileName, notebookId); // get the html var htmlDoc = new HtmlDocument(); htmlDoc.Load(htmlFile); // change the links to have the full path ModifyInDesignHtmlLinks(htmlDoc, htmlDir); // get the title string title = GetInDesignTitle(htmlDoc); string pageId = note.CreatePage(title, sectionId); note.AddPageContentAsHtmlBlock(pageId, htmlDoc.DocumentNode.OuterHtml); return(true); #else throw new NotImplementedException(); #endif }