private async Task <String> GetUgcPadContent() { String richText; #region Get RichEdit Raw Data this.RichBox.Document.GetText(TextGetOptions.FormatRtf, out richText); #endregion var structureBuilder = new RtfParserListenerStructureBuilder(); var parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(new RtfSource(richText)); var rtfStructure = structureBuilder.StructureRoot; // image converter var imageAdapter = new RtfVisualImageAdapter(ImageFormat.Jpeg); var imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ScaleImage = true; // scale images var imageConverter = new RtfImageConverter(imageConvertSettings); // rtf interpreter var interpreterSettings = new RtfInterpreterSettings(); var rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, imageConverter); var htmlSegments = await RtfToHtmlSegments(rtfDocument.VisualContent); return(htmlSegments); }
} // EnsureDestinationDirectory // ---------------------------------------------------------------------- private IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter) { IRtfDocument rtfDocument; RtfInterpreterListenerFileLogger interpreterLogger = null; try { // logger if (settings.LogInterpreter) { string logFileName = settings.BuildDestinationFileName( settings.LogDirectory, RtfInterpreterListenerFileLogger.DefaultLogFileExtension); interpreterLogger = new RtfInterpreterListenerFileLogger(logFileName); } // image converter RtfImageConverter imageConverter = null; if (settings.SaveImage) { RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ImagesPath = settings.DestinationDirectory; imageConvertSettings.BackgroundColor = settings.ImageBackgroundColor; imageConvertSettings.ScaleImage = !settings.UnscaledImages; if (settings.ExtendedImageScale) { imageConvertSettings.ScaleExtension = 0.5f; } imageConverter = new RtfImageConverter(imageConvertSettings); } // rtf interpreter RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings(); interpreterSettings.IgnoreDuplicatedFonts = settings.IgnoreDuplicatedFonts; interpreterSettings.IgnoreUnknownFonts = settings.IgnoreUnknownFonts; // interpret the rtf structure using the extractors rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, interpreterLogger, imageConverter); } catch (Exception e) { if (interpreterLogger != null) { interpreterLogger.Dispose(); } Console.WriteLine("error while interpreting rtf: " + e.Message); ExitCode = ProgramExitCode.InterpretRtf; return(null); } return(rtfDocument); } // InterpretRtf
private IRtfDocument InterpretRtf(IRtfGroup rtfStructure, RtfVisualImageAdapter imageAdapter) { IRtfDocument rtfDocument = null; RtfImageConverter imageConverter = null; RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ImagesPath = ImagePath; imageConvertSettings.BackgroundColor = null; imageConverter = new RtfImageConverter(imageConvertSettings); rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, null, imageConverter); return(rtfDocument); }
} // EnsureDestinationDirectory // ---------------------------------------------------------------------- private IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter) { IRtfDocument rtfDocument; RtfInterpreterListenerFileLogger interpreterLogger = null; try { // logger if (settings.LogInterpreter) { string logFileName = settings.BuildDestinationFileName( settings.LogDirectory, RtfInterpreterListenerFileLogger.DefaultLogFileExtension); interpreterLogger = new RtfInterpreterListenerFileLogger(logFileName); } // image converter RtfImageConverter imageConverter = null; if (settings.SaveImage) { RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ImagesPath = settings.DestinationDirectory; imageConvertSettings.BackgroundColor = settings.ImageBackgroundColor; if (settings.ExtendedImageScale) { imageConvertSettings.ScaleExtension = 0.5f; } imageConverter = new RtfImageConverter(imageConvertSettings); } // rtf parser // interpret the rtf structure using the extractors rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger, imageConverter); } catch (Exception e) { if (interpreterLogger != null) { interpreterLogger.Dispose(); } MessageBox(IntPtr.Zero, "Error while interpreting RTF:\n" + e.Message, "Itenso.Rtf2Html", 0x010000); ExitCode = ProgramExitCode.InterpretRtf; return(null); } return(rtfDocument); } // InterpretRtf
private static IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter, string imageAdapterLogFile = null, RtfImageConvertSettings imageConvertSettings = null, bool throwOnError = false) { IRtfDocument rtfDocument; RtfInterpreterListenerFileLogger interpreterLogger = null; try { // logger if (imageAdapterLogFile != null) { interpreterLogger = new RtfInterpreterListenerFileLogger(imageAdapterLogFile); } // image converter RtfImageConverter imageConverter = null; if (imageConvertSettings != null) { imageConverter = new RtfImageConverter(imageConvertSettings); } // rtf interpreter RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings(); interpreterSettings.IgnoreDuplicatedFonts = true; interpreterSettings.IgnoreUnknownFonts = true; // interpret the rtf structure using the extractors rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, interpreterLogger, imageConverter); } catch (Exception e) { if (interpreterLogger != null) { interpreterLogger.Dispose(); } if (throwOnError) { ThrowOnUnexpectedExitCode(e); } return(null); } return(rtfDocument); }
} // EnsureDestinationDirectory // ---------------------------------------------------------------------- private IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter) { IRtfDocument rtfDocument; try { // image converter RtfImageConverter imageConverter = null; if (settings.SaveImage) { RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ImagesPath = settings.DestinationDirectory; if (settings.ImageBackgroundColor.HasValue) { imageConvertSettings.BackgroundColor = new Model.RtfColor(settings.ImageBackgroundColor.Value.R , settings.ImageBackgroundColor.Value.G, settings.ImageBackgroundColor.Value.B); } imageConvertSettings.ScaleImage = !settings.UnscaledImages; if (settings.ExtendedImageScale) { imageConvertSettings.ScaleExtension = 0.5f; } imageConverter = new RtfImageConverter(imageConvertSettings); } // rtf interpreter RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings(); interpreterSettings.IgnoreDuplicatedFonts = settings.IgnoreDuplicatedFonts; interpreterSettings.IgnoreUnknownFonts = settings.IgnoreUnknownFonts; // interpret the rtf structure using the extractors rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, imageConverter); } catch (Exception e) { Console.WriteLine("error while interpreting rtf: " + e.Message); ExitCode = ProgramExitCode.InterpretRtf; return(null); } return(rtfDocument); } // InterpretRtf
} // ConvertRtf2Txt // ---------------------------------------------------------------------- public string ConvertRtf2Html(string fileName) { // parser IRtfGroup rtfStructure = ParseRtf(fileName); if (rtfStructure == null) { return(string.Empty); } // interpreter logger RtfInterpreterListenerFileLogger interpreterLogger = null; if (!string.IsNullOrEmpty(InterpreterLogFileName)) { interpreterLogger = new RtfInterpreterListenerFileLogger(InterpreterLogFileName); } // image converter RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter(ImageFormat.Jpeg); RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ScaleImage = true; // scale images RtfImageConverter imageConverter = new RtfImageConverter(imageConvertSettings); // rtf interpreter RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings(); IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, interpreterLogger, imageConverter); // html converter RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings(); htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.Content; RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument); return(htmlConverter.Convert()); } // ConvertRtf2Html
private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { String richText; this.RichBox.Document.GetText(TextGetOptions.FormatRtf, out richText); var structureBuilder = new RtfParserListenerStructureBuilder(); var parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(new RtfSource(richText)); var rtfStructure = structureBuilder.StructureRoot; // image converter var imageAdapter = new RtfVisualImageAdapter(ImageFormat.Jpeg); var imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ScaleImage = true; // scale images var imageConverter = new RtfImageConverter(imageConvertSettings); // rtf interpreter var interpreterSettings = new RtfInterpreterSettings(); var rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, imageConverter); var htmlSegments = await RtfToHtmlSegments(rtfDocument.VisualContent); var anonymited = this.Anonymity.IsChecked.HasValue && this.Anonymity.IsChecked.Value == true; this.Result = new UgcDialogResult(anonymited, htmlSegments); //var deferral = args.GetDeferral(); //deferral.Complete(); }
} // EnsureDestinationDirectory // ---------------------------------------------------------------------- private string InterpretRtf(IRtfGroup rtfStructure) { string text = null; RtfInterpreterListenerFileLogger interpreterLogger = null; try { // logger if (settings.LogInterpreter) { string logFileName = settings.BuildDestinationFileName( settings.LogDirectory, RtfInterpreterListenerFileLogger.DefaultLogFileExtension); interpreterLogger = new RtfInterpreterListenerFileLogger(logFileName); } // text converter RtfTextConverter textConverter = null; if (settings.SaveText) { RtfTextConvertSettings textConvertSettings = new RtfTextConvertSettings(); textConvertSettings.IsShowHiddenText = settings.ShowHiddenText; textConverter = new RtfTextConverter(textConvertSettings); } // image converter RtfImageConverter imageConverter = null; if (settings.SaveImage) { RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter( settings.ImageFileNamePattern, settings.ImageFormat); RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ImagesPath = settings.DestinationDirectory; imageConvertSettings.ScaleImage = settings.ScaleImage; imageConvertSettings.BackgroundColor = settings.ImageBackgroundColor; imageConvertSettings.ScaleImage = !settings.UnscaledImages; if (settings.ExtendedImageScale) { imageConvertSettings.ScaleExtension = 0.5f; } imageConverter = new RtfImageConverter(imageConvertSettings); } // rtf interpreter RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings(); interpreterSettings.IgnoreDuplicatedFonts = settings.IgnoreDuplicatedFonts; interpreterSettings.IgnoreUnknownFonts = settings.IgnoreUnknownFonts; // interpret the rtf structure using the extractors RtfInterpreterTool.Interpret(rtfStructure, interpreterLogger, textConverter, imageConverter); // get the resulting text if (textConverter != null) { text = textConverter.PlainText; } } catch (Exception e) { if (interpreterLogger != null) { interpreterLogger.Dispose(); } Console.WriteLine("error while interpreting rtf: " + e.Message); ExitCode = ProgramExitCode.InterpretRtf; return(null); } return(text); } // InterpretRtf
public static string ConvertRtf2Html(string text, int slideID) { // parser text = text.Replace("\\tab", "$$$$$$$$"); text = text.Replace("li240", "$$$$$$$$$$$$$$$$"); text = text.Replace("li480", "$$$$$$$$$$$"); text = text.Replace("ri480", "$$$$$$$$$$$"); text = text.Replace("li720", "$$$$$$$$$$$$$$"); IRtfGroup rtfStructure = ParseRtf(text); if (rtfStructure == null) { return(string.Empty); } // image converter RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter( Path.GetFileNameWithoutExtension(Form1.currentFile) + "\\Slide" + slideID + "_{0}{1}", ImageFormat.Jpeg); RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ScaleImage = true; // scale images' string filePath = Path.GetDirectoryName(Form1.currentFile); string folder = Path.GetFileNameWithoutExtension(Form1.currentFile); string directoryPath = filePath + "\\" + folder; //string directoryPath = @"C:\Users\Knwal\Documents\Presentation Tool\Presentation Tool\Preview\"; imageConvertSettings.ImagesPath = directoryPath; RtfImageConverter imageConverter = new RtfImageConverter(imageConvertSettings); Stream st = GenerateStreamFromString(text); RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings(); IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, null, imageConverter); // html converter RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings(imageAdapter); htmlConvertSettings.StyleSheetLinks.Add("default.css"); htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.Content; //htmlConvertSettings.GetImageUrl(); RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings); string str = htmlConverter.Convert(); str = str.Replace(" ", " "); str = str.Replace("$$$$$$$$", " "); str = str.Replace("$$$", " "); //str= str.Replace("#1#",""); //str = str.Replace("#2#", ""); //str = str.Replace("#3#", ""); //str = str.Replace("#4#", ""); //str = str.Replace("#5#", ""); //str = str.Replace("#6#", ""); //str = str.Replace("#7#", ""); //str = str.Replace("#8#", ""); //str = str.Replace("#9#", ""); return(str); } // ConvertRtf2Html