Bonjour,
Avez-vous une méthode pour transformer un contenu rtf en html ?
J'ai bien trouvé un source: https://code.msdn.microsoft.com/wind...F-and-aaa02a6e
mais il me donne un problème de:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 Informations supplémentaires : Le thread appelant doit être en mode STA, comme l'exigent de nombreux composants de l'interface utilisateur. Quand je veux utiliser la méthode:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 MarkupConverter markupConverter = new MarkupConverter(); string rtf = string.Empty; string test = string.Empty; System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\c.cavenaile\Downloads\MarkupConverter\MarkupConverter\test.txt"); string line = file.ReadLine(); while ((line = file.ReadLine()) != null) { rtf += line; } file.Close(); test = markupConverter.ConvertRtfToHtml(line); Console.WriteLine(test);J'ai relu la doc j'ai rajouté un thread :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 using System; using System.IO; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace MarkupConverter { public static class RtfToHtmlConverter { private const string FlowDocumentFormat = "<FlowDocument>{0}</FlowDocument>"; public static string ConvertRtfToHtml(string rtfText) { var xamlText = string.Format(FlowDocumentFormat, ConvertRtfToXaml(rtfText)); return HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, false); } private static string ConvertRtfToXaml(string rtfText) { var richTextBox = new RichTextBox(); ==> Problème ici :weird::weird::weird::weird::weird::weird: ==> Le thread appelant doit être en mode STA, comme l'exigent de nombreux composants de l'interface utilisateur. if (string.IsNullOrEmpty(rtfText)) return ""; var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); using (var rtfMemoryStream = new MemoryStream()) { using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream)) { rtfStreamWriter.Write(rtfText); rtfStreamWriter.Flush(); rtfMemoryStream.Seek(0, SeekOrigin.Begin); textRange.Load(rtfMemoryStream, DataFormats.Rtf); } } using (var rtfMemoryStream = new MemoryStream()) { textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); textRange.Save(rtfMemoryStream, DataFormats.Xaml); rtfMemoryStream.Seek(0, SeekOrigin.Begin); using (var rtfStreamReader = new StreamReader(rtfMemoryStream)) { return rtfStreamReader.ReadToEnd(); } } } } }
Mais je sèche, je n'arrive pas a afficher le html ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using MarkupConverter; namespace MarkupConverter { class Class1 { static void Main(string[] args) { string rtf2 = string.Empty; string test = string.Empty; System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\c.cavenaile\Downloads\MarkupConverter\MarkupConverter\test.txt"); string line = file.ReadLine(); while ((line = file.ReadLine()) != null) { rtf2 += line; } file.Close(); var thread = new Thread(ConvertRtfInSTAThread); var threadData = new ConvertRtfThreadData { RtfText = rtf2 }; Console.WriteLine(threadData.HtmlText); Console.ReadLine(); //Console.WriteLine(test); } public static void ConvertRtfInSTAThread(object rtf) { MarkupConverter markupConverter = new MarkupConverter(); var threadData = rtf as ConvertRtfThreadData; threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText); Console.WriteLine("ok"); Console.ReadLine(); } public class ConvertRtfThreadData { public string RtfText { get; set; } public string HtmlText { get; set; } } } }
D'avance merci
[EDIT1]
Au final, j'ai ceci :
et je n'arrive pas a afficher le html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using MarkupConverter; namespace MarkupConverter { class Class1 { MarkupConverter markupConverter = new MarkupConverter(); static void Main(string[] args) { string rtf2 = string.Empty; string test = string.Empty; System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\c.cavenaile\Downloads\MarkupConverter\MarkupConverter\test.txt"); string line = file.ReadLine(); while ((line = file.ReadLine()) != null) { rtf2 += line; } file.Close(); } private string ConvertRtfToHtml(string rtfText) { var thread = new Thread(ConvertRtfInSTAThread); var threadData = new ConvertRtfThreadData { RtfText = rtfText }; thread.SetApartmentState(ApartmentState.STA); thread.Start(threadData); thread.Join(); return threadData.HtmlText; } private void ConvertRtfInSTAThread(object rtf) { var threadData = rtf as ConvertRtfThreadData; threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText); } private class ConvertRtfThreadData { public string RtfText { get; set; } public string HtmlText { get; set; } } } }
[EDIT2]
Autre solution c'est de passé du RTF en xaml pour ensuite le convertir en html.
Pour la première étape, j'ai trouvé facilement, il me reste maintenant la conversion du xaml en html, avez vous une idée ?
Merci
Partager