OneNoteConversionTool.FormatReaders.EpubReader.GetPagePaths C# (CSharp) Method

GetPagePaths() public method

Get the file paths of all pages
public GetPagePaths ( ) : List
return List
		public List<string> GetPagePaths()
		{
			// Get the paths of each page using _mEpub if it is not null
			if (_mEpub != null)
			{
				return (from string htmlPath in _mEpub.Content.Keys
					select Path.GetFullPath(Path.Combine(_mContentDir, htmlPath))).ToList();
			}

			var pagePaths = new List<string>();

			// Get the opf path and ensure that it exists
			string opfPath = GetOpfFilePath();
			if (opfPath.Equals(String.Empty))
			{
				return pagePaths;
			}

			// Load the opf file
			var xOpfDoc = new XmlDocument();
			xOpfDoc.Load(opfPath);

			// Get the spine data
			XmlNodeList xNodes = xOpfDoc.SelectNodes("//*");
			if (xNodes == null)
			{
				return pagePaths;
			}

			List<XmlNode> xItemRefNodes = (from XmlNode xNode in xNodes 
										   where xNode.Name.Contains("itemref") 
										   select xNode).ToList();
			foreach (XmlNode xItemRefNode in xItemRefNodes)
			{
				if (xItemRefNode.Attributes == null || xItemRefNode.Attributes["idref"] == null)
				{
					pagePaths.Add(String.Empty);
					continue;
				}
				string idref = xItemRefNode.Attributes["idref"].Value;
				string xPath = String.Format("//*[@id=\"{0}\"]", idref);
				XmlNode node = xOpfDoc.SelectSingleNode(xPath);
				
				pagePaths.Add(node == null || node.Attributes == null || node.Attributes["href"] == null
					? String.Empty 
					: Path.GetFullPath(Path.Combine(_mContentDir, node.Attributes["href"].Value)));
			}

			return pagePaths;
		}

Usage Example

		/// <summary>
		/// Converts the ePub document input file into OneNote
		/// </summary>
		/// <param name="inputFile"></param>
		/// <param name="outputDir"></param>
		/// <returns></returns>
		public virtual bool ConvertEpubToOneNote(string inputFile, string outputDir)
		{
			try
			{
				// Initialize epub reader class
				var epub = new EpubReader(inputFile, outputDir);

				// Get the page contents
				List<string> pageTitles = epub.GetPageTitles();
				List<HtmlDocument> pagesHtml = epub.GetPagesAsHtmlDocuments();
				List<string> pagePaths = epub.GetPagePaths();
				Dictionary<string, int> pagesLevel = epub.GetPagesLevel();

				// Create a new OneNote Notebook
				var note = new OneNoteGenerator(outputDir);
				string notebookId = note.CreateNotebook(GetSupportedInputFormat());
				string sectionId = note.CreateSection(epub.GetTitle(), notebookId);

				// Create pages
				var pageIds = pageTitles.Select(pageTitle => note.CreatePage(pageTitle, sectionId)).ToArray();

				// Get links to pages
				var pageLinks = pageIds.Select(pageId => note.GetHyperLinkToObject(pageId)).ToList();

				// Replace links to .html with .one
				ReplaceEpubLinksFromHtmlToOneNote(pagesHtml, pagePaths, pageLinks);

				// Add content to pages
				for (var i = 0; i < pageIds.Length; i++)
				{
					note.AppendPageContentAsHtmlBlock(pageIds[i], pagesHtml[i].DocumentNode.OuterHtml);
					if (pagesLevel.ContainsKey(pagePaths[i]))
					{
						note.SetPageLevel(pageIds[i], pagesLevel[pagePaths[i]]);
					}
				}

				return true;
			}
			catch (Exception e)
			{
				Console.WriteLine(@"Error in ConvertEpubToOneNote for file {0}: {1}", inputFile, e.Message);
				return false;
			}
		}