public static void ToHtml(RtfSource source, XmlWriter writer, RtfHtmlWriterSettings settings = null) { settings = settings ?? new RtfHtmlWriterSettings(); var content = ParseRtf(source); // Try to extract encoded html from within the rtf (Outlook likes to do this) if (!BuildHtmlContent(content, writer)) { var intSettings = new RtfInterpreterSettings() { IgnoreDuplicatedFonts = true, IgnoreUnknownFonts = true }; var rtfDocument = RtfInterpreterTool.BuildDoc(content, intSettings); var htmlConvertSettings = new RtfHtmlConvertSettings(settings.ObjectVisitor); htmlConvertSettings.IsShowHiddenText = false; htmlConvertSettings.UseNonBreakingSpaces = false; htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.All; var htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings); htmlConverter.Convert(writer); } }
} // ToXmlButtonClick // ---------------------------------------------------------------------- private void ToHtmlButtonClick(object sender, RoutedEventArgs e) { try { TextRange conversionText = ConversionText; using (MemoryStream stream = new MemoryStream()) { conversionText.Save(stream, DataFormats.Rtf); stream.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(stream)) { IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(reader); RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument); textBox.Text = htmlConverter.Convert(); } } } catch (Exception exception) { MessageBox.Show(this, "Error " + exception.Message, Title, MessageBoxButton.OK, MessageBoxImage.Error); } } // ToHtmlButtonClick
public void TextAlignmentTestFixed() { const RtfParserListenerLogger parserLogger = null; //parserLogger = new RtfParserListenerLogger(); IRtfGroup rtfStructure = RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_23.rtf"), parserLogger); Assert.IsNotNull(rtfStructure); const RtfInterpreterListenerLogger interpreterLogger = null; //interpreterLogger = new RtfInterpreterListenerLogger(); IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger); Assert.IsNotNull(rtfDoc); IRtfVisualCollection rtfVisuals = rtfDoc.VisualContent; Assert.AreEqual(2, rtfVisuals.Count); Assert.AreEqual(RtfVisualKind.Text, rtfVisuals[0].Kind); Assert.AreEqual("Simple text", ((IRtfVisualText)rtfVisuals[0]).Text); Assert.AreEqual(RtfTextAlignment.Right, ((IRtfVisualText)rtfVisuals[0]).Format.Alignment); } // TextAlignmentTestFixed
} // EnsureDestinationDirectory // ---------------------------------------------------------------------- private IRtfDocument InterpretRtf(IRtfGroup rtfStructure) { IRtfDocument rtfDocument; RtfInterpreterListenerFileLogger interpreterLogger = null; try { // logger if (settings.LogInterpreter) { string logFileName = settings.BuildDestinationFileName( settings.LogDirectory, RtfInterpreterListenerFileLogger.DefaultLogFileExtension); interpreterLogger = new RtfInterpreterListenerFileLogger(logFileName); } // 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, interpreterLogger); } catch (Exception e) { if (interpreterLogger != null) { interpreterLogger.Dispose(); } Console.WriteLine("error while interpreting rtf: " + e.Message); ExitCode = ProgramExitCode.InterpretRtf; return(null); } return(rtfDocument); } // InterpretRtf
} // ToTextButtonClick // ---------------------------------------------------------------------- private void ToXmlButtonClick(object sender, EventArgs e) { try { IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(ConversionText); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; xmlWriterSettings.IndentChars = (" "); StringBuilder sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, xmlWriterSettings)) { RtfXmlConverter xmlConverter = new RtfXmlConverter(rtfDocument, writer); xmlConverter.Convert(); writer.Flush(); textBox.Text = sb.ToString(); } } catch (Exception exception) { MessageBox.Show(this, "Error " + exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } // ToXmlButtonClick
} // 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(); }
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
/// <summary> /// Process the submissions absolute path /// </summary> /// <returns>Was the submission able to be loaded?</returns> public override bool Process() { _loaded = false; try { #if NETCORE // Add a reference to the NuGet package System.Text.Encoding.CodePages for .Net core only Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); #endif IRtfGroup rtfStructure; RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(new RTFDocumentSource(_target.AbsolutePath)); rtfStructure = structureBuilder.StructureRoot; var intSettings = new RtfInterpreterSettings() { IgnoreDuplicatedFonts = true, IgnoreUnknownFonts = true }; var rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, intSettings); if (rtfDocument.DocumentInfo.Author != null) { _target.MetaCreator = rtfDocument.DocumentInfo.Author; } if (rtfDocument.DocumentInfo.CreationTime != null) { _target.MetaDateCreated = (DateTime)rtfDocument.DocumentInfo.CreationTime; } if (rtfDocument.DocumentInfo.PrintTime != null) { _target.MetaDateLastPrinted = (DateTime)rtfDocument.DocumentInfo.PrintTime; } if (rtfDocument.DocumentInfo.RevisionTime != null) { _target.MetaDateModified = (DateTime)rtfDocument.DocumentInfo.RevisionTime; } // Build our content StringBuilder builder = new StringBuilder(); foreach (IRtfVisual v in rtfDocument.VisualContent) { if (v.Kind == RtfVisualKind.Text) { builder.Append(v.ToString()); } } _target.Content = builder.ToString(); _target.ContentLength = _target.Content.Length; _target.ContentHash = Compare.Hash(_target.Content); _loaded = true; } catch (Exception e) { _target.Logging.Add("- " + Markdown.Bold("An exception occured when processing " + _target.AbsolutePath + ", " + e.Message)); } return(_loaded); }