} // ValidateProgramSettings // ---------------------------------------------------------------------- private IRtfGroup ParseRtf() { IRtfGroup rtfStructure; try { // rtf parser // open readonly - in case of dominant locks... using (FileStream stream = File.Open(settings.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // parse the rtf structure RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(new RtfSource(stream)); rtfStructure = structureBuilder.StructureRoot; } } catch (Exception e) { Console.WriteLine("error while parsing rtf: " + e.Message); ExitCode = ProgramExitCode.ParseRtf; return(null); } return(rtfStructure); } // ParseRtf
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); }
public static string RtfToHtml(this string str, RtfHtmlConvertSettings htmlConvertSettings, bool throwOnError = false, IRtfParserListener listener = null, string destinationDirectory = null, RtfVisualImageAdapter imageAdapter = null, string imageAdapterLogFile = null, RtfImageConvertSettings imageConvertSettings = null) { IRtfGroup rtfStructure; IRtfDocument rtfDocument = null; try { using (var stream = str.ToStream()) { // parse the rtf structure var structureBuilder = new RtfParserListenerStructureBuilder(); var parser = new RtfParser(structureBuilder); DirectoryInfo destination; if (destinationDirectory != null) { destination = new DirectoryInfo(destinationDirectory); } parser.IgnoreContentAfterRootGroup = true; // support WordPad documents if (listener != null) { parser.AddParserListener(listener); } parser.Parse(new RtfSource(stream)); rtfStructure = structureBuilder.StructureRoot; ThrowOnUnexpectedExitCode(); rtfDocument = InterpretRtf(rtfStructure, imageAdapter, imageAdapterLogFile, imageConvertSettings, throwOnError); if (throwOnError) { ThrowOnUnexpectedExitCode(); } // convert to hmtl string html = ConvertHmtl(rtfDocument, imageAdapter, htmlConvertSettings, throwOnError); if (throwOnError) { ThrowOnUnexpectedExitCode(); } return(html); } } catch (Exception e) { Debug.WriteLine("RtfToHtml parser failed with error: {0}", e.Message); } return(null); }
} // ParseResourcesTest // ---------------------------------------------------------------------- protected override void DoTest(string kind, Stream testRes, string testCaseName) { RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(); //parser.AddParserListener( new RtfParserListenerLogger() ); parser.AddParserListener(structureBuilder); parser.Parse(new RtfSource(testRes)); Assert.IsNotNull(structureBuilder.StructureRoot); } // DoTest
private IRtfGroup ParseRtf(string RtfString) { IRtfGroup rtfStructure; // parse the rtf structure RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents using (TextReader reader = new StringReader(RtfString)) { parser.Parse(new RtfSource(reader)); } rtfStructure = structureBuilder.StructureRoot; return(rtfStructure); }
} // ConvertRtf2Html private static IRtfGroup ParseRtf(string text) { IRtfGroup rtfStructure; using (Stream stream = GenerateStreamFromString(text)) { RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(new RtfSource(stream)); rtfStructure = structureBuilder.StructureRoot; } return(rtfStructure); } // ParseRtf
} // ValidateProgramSettings // ---------------------------------------------------------------------- private IRtfGroup ParseRtf() { IRtfGroup rtfStructure; RtfParserListenerFileLogger parserLogger = null; try { // logger if (settings.LogParser) { string logFileName = settings.BuildDestinationFileName( settings.LogDirectory, RtfParserListenerFileLogger.DefaultLogFileExtension); parserLogger = new RtfParserListenerFileLogger(logFileName); } // rtf parser // open readonly - in case of dominant locks... using (FileStream stream = File.Open(settings.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // parse the rtf structure RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents if (parserLogger != null) { parser.AddParserListener(parserLogger); } parser.Parse(new RtfSource(stream)); rtfStructure = structureBuilder.StructureRoot; } } catch (Exception e) { if (parserLogger != null) { parserLogger.Dispose(); } Console.WriteLine("error while parsing rtf: " + e.Message); ExitCode = ProgramExitCode.ParseRtf; return(null); } return(rtfStructure); } // ParseRtf
} // Parse public static IRtfGroup Parse(IRtfSource rtfTextSource, params IRtfParserListener[] listeners) { var structureBuilder = new RtfParserListenerStructureBuilder(); var parser = new RtfParser(structureBuilder); if (listeners != null) { foreach (var listener in listeners) { if (listener != null) { parser.AddParserListener(listener); } } } parser.Parse(rtfTextSource); return(structureBuilder.StructureRoot); } // Parse
} // ConvertRtf2Html // ---------------------------------------------------------------------- private IRtfGroup ParseRtf(string fileName) { IRtfGroup rtfStructure; using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents if (!string.IsNullOrEmpty(ParserLogFileName)) { parser.AddParserListener(new RtfParserListenerFileLogger(ParserLogFileName)); } parser.Parse(new RtfSource(stream)); rtfStructure = structureBuilder.StructureRoot; } return(rtfStructure); } // ParseRtf
private static IRtfGroup ParseRtf(RtfSource source) { IRtfGroup rtfStructure; try { // parse the rtf structure RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(source); rtfStructure = structureBuilder.StructureRoot; } catch { return(null); } return(rtfStructure); } // ParseRtf
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 void ParseStructureBuilderTest() { RtfParser parser = new RtfParser(); RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); parser.AddParserListener(structureBuilder); parser.Parse(new RtfSource(@"{\rtf1foobar}")); IRtfGroup rtfStructure = structureBuilder.StructureRoot; Assert.IsNotNull(rtfStructure); Assert.AreEqual(RtfElementKind.Group, rtfStructure.Kind); Assert.AreEqual(2, rtfStructure.Contents.Count); Assert.AreEqual(RtfElementKind.Tag, rtfStructure.Contents[0].Kind); Assert.AreEqual("rtf", ((IRtfTag)rtfStructure.Contents[0]).Name); Assert.AreEqual(true, ((IRtfTag)rtfStructure.Contents[0]).HasValue); Assert.AreEqual("1", ((IRtfTag)rtfStructure.Contents[0]).ValueAsText); Assert.AreEqual(1, ((IRtfTag)rtfStructure.Contents[0]).ValueAsNumber); Assert.AreEqual(RtfElementKind.Text, rtfStructure.Contents[1].Kind); Assert.AreEqual("foobar", ((IRtfText)rtfStructure.Contents[1]).Text); } // ParseStructureBuilderTest
/// <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); }