IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Convertir un fichier rtf (tx_rtf32) en HTML [Débutant]


Sujet :

C#

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    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);
    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();
                    }
                }
    
            }
    
    
    
    
        }
    }
    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
     
    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; }
            }
        }
    }
    Mais je sèche, je n'arrive pas a afficher le html ?

    D'avance merci

    [EDIT1]
    Au final, j'ai ceci :

    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; }
            }
     
     
        }
    }
    et je n'arrive pas a afficher le html

    [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

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    J'ai trouvé c'est bon

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Convertir un fichier .rtf en .pdf
    Par Aguado dans le forum ASP
    Réponses: 5
    Dernier message: 05/06/2009, 16h06
  2. convertir un fichier RTF en PDF
    Par lokmane dans le forum Documents
    Réponses: 7
    Dernier message: 05/12/2007, 19h22
  3. Convertir un fichier html en rtf
    Par lilou77 dans le forum AWT/Swing
    Réponses: 6
    Dernier message: 08/02/2007, 10h40
  4. Convertir un fichier HTML en fichier CHM en ligne de commande
    Par koKoTis dans le forum Autres Logiciels
    Réponses: 8
    Dernier message: 18/11/2006, 13h39
  5. [JCOM][RTF]Convertir un fichier HTML en RTF
    Par pistache42 dans le forum Documents
    Réponses: 3
    Dernier message: 28/04/2006, 17h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo