/// <summary> /// Gets the RTF HTML node list. /// </summary> /// <param name="htmlNode">The document node.</param> /// <returns></returns> private static List <RtfHtmlNode> GetRtfHtmlNodes(HtmlNode htmlNode) { List <RtfHtmlNode> rtfHtmlNodes = new List <RtfHtmlNode>(); foreach (var node in htmlNode.ChildNodes) { IEnumerable <HtmlNode> descendantNodes = node.Descendants(); RtfHtmlNode rtfHtmlNode = new RtfHtmlNode(); if (node.Attributes.Count > 0) { rtfHtmlNode.RtfParagraphFormat = node.Attributes[0].Value; } RtfHtmlItem rtfHtmlItem = new RtfHtmlItem(); foreach (var currentNode in descendantNodes) { if (currentNode.Name.Equals(RtfImageNodeName) && currentNode.Attributes.Count > 0) //Image { rtfHtmlItem = GetImage(currentNode); rtfHtmlNode.RtfHtmlItem.Add(rtfHtmlItem); rtfHtmlItem = new RtfHtmlItem(); } else if (currentNode.Name.Equals(RtfTextNodeName)) //Text { rtfHtmlItem.HtmlText = currentNode.InnerText; rtfHtmlNode.RtfHtmlItem.Add(rtfHtmlItem); rtfHtmlItem = new RtfHtmlItem(); } else //Format text { rtfHtmlItem.RtfFormattedText.Add(currentNode.Name); } } rtfHtmlNodes.Add(rtfHtmlNode); } return(rtfHtmlNodes); }
/// <summary> /// Gets the image. /// </summary> /// <param name="htmlNode">The HTML node.</param> /// <returns></returns> private static RtfHtmlItem GetImage(HtmlNode htmlNode) { RtfHtmlItem rtfHtmlItem = new RtfHtmlItem(); if (htmlNode.Attributes.Count > 0) { string imageUrl = HttpUtility.UrlDecode(htmlNode.Attributes.Where(y => y.Name.Equals(RtfImageSrcName)).Select(x => x.Value).First()); if (!string.IsNullOrWhiteSpace(imageUrl)) { //to get the image height from the specified path int imageHeight = Image.FromFile(imageUrl).Height; //to get the image width from the specified path int imageWidth = Image.FromFile(imageUrl).Width; if (!string.IsNullOrEmpty(imageUrl)) { //declare default height and width as 0 (As per Rtf dll if its 0 than it will take original height/width else specified height/width) //Get height/width if html node (Image) contains those attribute var heightAttribute = htmlNode.Attributes.FirstOrDefault(y => y.Name.Equals(RtfHeightAttribute)); var widthAttribute = htmlNode.Attributes.FirstOrDefault(y => y.Name.Equals(RtfWidthAttribute)); int height; int width; //checking heightAttribute value if (heightAttribute != null) { //setting maximum height for the image height = Convert.ToInt32(heightAttribute.Value) > Constants.ImageHeight ? Constants.ImageHeight : Convert.ToInt32(heightAttribute.Value); } else { //setting maximum height for the image height = imageHeight > Constants.ImageHeight ? Constants.ImageHeight : imageHeight; } if (widthAttribute != null) { //setting maximum width for the image width = Convert.ToInt32(widthAttribute.Value) > Constants.ImageWidth ? Constants.ImageWidth : Convert.ToInt32(widthAttribute.Value); } else { //setting maximum width for the image width = imageWidth > Constants.ImageWidth ? Constants.ImageWidth : imageWidth; } //Decode image url imageUrl = Uri.UnescapeDataString(imageUrl); Regex regEx = new Regex(Constants.UrlSourceRegex, RegexOptions.Compiled); //Check added image is from URL or uploaded folder if (regEx.Match(imageUrl).Success) { //Getting image from url and convert it into bitmap WebRequest request = WebRequest.Create(imageUrl); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); if (stream != null) { Bitmap bitmap = new Bitmap(stream); rtfHtmlItem.HtmlImage = bitmap; } } else { if (File.Exists(imageUrl)) { //getting image from uploaded folder using (FileStream fileStream = File.Open(imageUrl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { Bitmap bitmap = new Bitmap(fileStream); rtfHtmlItem.HtmlImage = bitmap; } } } //Assign Image width/height rtfHtmlItem.ImageHeight = height; rtfHtmlItem.ImageWidth = width; } } } return(rtfHtmlItem); }