Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving and loading rtf documents
QtWS25 Last Chance

Saving and loading rtf documents

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 2 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AsimovA Offline
    AsimovA Offline
    Asimov
    wrote on last edited by
    #1

    I know this has been asked a few times because I have been trawling through the posts for a couple of days looking for an answer.

    I would like to write an app that loads and saves rtf documents.

    The reason to use rtf is that most word processors new and old can load this format.

    I wish to write book writing software.
    The software I used to use is Liquid Story binder and I wish to write something like that, but improve it.

    However it would be handy if I could save and load in rtf, and maybe other formats later.

    I know that QT cannot do this, so is there some kind of special libary I can get that will let me achieve this.

    I know that I can't be the only one that wants to do this, and the only post I have found on here on the subject is from 2012.

    Saving to html is not an option because people who are used to Liquid Story binder expect it to save in rtf, and I want it to have the same functionality.

    mrjjM 1 Reply Last reply
    0
    • AsimovA Asimov

      I know this has been asked a few times because I have been trawling through the posts for a couple of days looking for an answer.

      I would like to write an app that loads and saves rtf documents.

      The reason to use rtf is that most word processors new and old can load this format.

      I wish to write book writing software.
      The software I used to use is Liquid Story binder and I wish to write something like that, but improve it.

      However it would be handy if I could save and load in rtf, and maybe other formats later.

      I know that QT cannot do this, so is there some kind of special libary I can get that will let me achieve this.

      I know that I can't be the only one that wants to do this, and the only post I have found on here on the subject is from 2012.

      Saving to html is not an option because people who are used to Liquid Story binder expect it to save in rtf, and I want it to have the same functionality.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @Asimov

      The big question is what verion/spec format of RTF do you mean?
      If the good old MS one, then
      http://librtf.sourceforge.net/
      or
      https://www.codeproject.com/Articles/98062/RTF-Document-Constructor-Library
      could maybe be interesting.

      The RTF (old) ones are not very complex
      http://www.biblioscape.com/rtf15_spec.htm
      But would take long time to write your own since you need to support all constructs.

      Also since you are planning a full editor app, i would suggest to look at existing ones
      like
      https://cgit.kde.org/koffice.git/tree/filters/kword/rtf
      or maybe from Libra office.

      1 Reply Last reply
      2
      • AsimovA Offline
        AsimovA Offline
        Asimov
        wrote on last edited by
        #3

        The librtf library looks interesting, and should be able to make rtfs.
        What I don't know is how I would take the rtf and display i t in a QTextview, or how to save a QTextview to the rtf.
        It looks like a standard C++ library, but I am not sure how I would use that with the QTextview.

        You see people who write books like to use software for this kind of thing, but they have to know if they software fails for some reason, or they decide to change their operating system that they can get access to their work without the software if you know what I mean.

        I like QT and I have only made 2 applications with it yet, but this seems a little bit difficult heh heh.
        I wonder if anyone else on this board has done this before and maybe has some sample code I could look at?

        mrjjM 1 Reply Last reply
        0
        • AsimovA Asimov

          The librtf library looks interesting, and should be able to make rtfs.
          What I don't know is how I would take the rtf and display i t in a QTextview, or how to save a QTextview to the rtf.
          It looks like a standard C++ library, but I am not sure how I would use that with the QTextview.

          You see people who write books like to use software for this kind of thing, but they have to know if they software fails for some reason, or they decide to change their operating system that they can get access to their work without the software if you know what I mean.

          I like QT and I have only made 2 applications with it yet, but this seems a little bit difficult heh heh.
          I wonder if anyone else on this board has done this before and maybe has some sample code I could look at?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          That why ask i ask what "RTF" u are talking about as RTF just means rich text.
          Microsoft have a really know RTF format but Qt uses HTML for rich text
          http://doc.qt.io/qt-5/richtext-html-subset.html

          So its hard to use MS RTF directly as you would need to convert back and forth between
          .rtf and HTML. And its not super easy. Many details.

          I could not find anything that DRAWS an ms.RTF

          • You see people who write books like to use software for this kind of thing, but they have to know if they software fails for some reason, or they decide to change their operating system that they can get access to their work without the software if you know what I mean.

          Yes, i worked for a publishing house for 10 years. Its critical to use a common format.

          If you plan on making such super book editor then maybe look at open source editors to get an
          idea. Its huge work.

          1 Reply Last reply
          2
          • AsimovA Offline
            AsimovA Offline
            Asimov
            wrote on last edited by
            #5

            Yes I meant the rtf file format and not the Rich Text Format that QT uses.
            I think that was the MS rtf. I know it can be opened in MS office and open office in linux, so I thought it would be the best format to use.

            I spent a few years using Liquid Story Binder, but the software is getting old now, but a lot of people still like it. There is also Scrivener which is ok, but not quite as good as LSB, so I thought I would try and build something in-between the two.

            So before I start this task, I just needed to know if it is possible to export rtf format documents, and later to be able to export other doc formats. MS RTF would be the main one though.

            I have been programming in many languages over the years, so I know I can make the app ok, but the problem is that I don't know much about document formats.

            I knew this part would be the difficult part of designing it, and I was hoping there was a pre-built library for saving and loading doc formats into Qtextview.

            I would be happy right now just to get ms rtf to save and load.

            I think Qtextview can save in html, and pdf at the moment, but what other formats can it save as standard?

            mrjjM 1 Reply Last reply
            0
            • AsimovA Asimov

              Yes I meant the rtf file format and not the Rich Text Format that QT uses.
              I think that was the MS rtf. I know it can be opened in MS office and open office in linux, so I thought it would be the best format to use.

              I spent a few years using Liquid Story Binder, but the software is getting old now, but a lot of people still like it. There is also Scrivener which is ok, but not quite as good as LSB, so I thought I would try and build something in-between the two.

              So before I start this task, I just needed to know if it is possible to export rtf format documents, and later to be able to export other doc formats. MS RTF would be the main one though.

              I have been programming in many languages over the years, so I know I can make the app ok, but the problem is that I don't know much about document formats.

              I knew this part would be the difficult part of designing it, and I was hoping there was a pre-built library for saving and loading doc formats into Qtextview.

              I would be happy right now just to get ms rtf to save and load.

              I think Qtextview can save in html, and pdf at the moment, but what other formats can it save as standard?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Asimov
              as far as i know only save it html. ( and print to pdf)
              Maybe you cheat a bit. there should be rtf to html converts etc that might help.
              http://docfrac.net/wordpress/

              But yes the format will be a huge work. And then all the other features.

              1 Reply Last reply
              1
              • AsimovA Offline
                AsimovA Offline
                Asimov
                wrote on last edited by
                #7

                I have to say I like QT, but I am a little disappointed that there isn't an easier way of doing this.
                I didn't want to put a lot of work into this until I knew I could do the hard part which is the saving and the loading.

                I think my only option in this regard is to learn how rtf files are made, and then take the html from a QTextview and work out how to change it to rtf encoding.
                Probably will be a nightmare.

                Now I am not sure what to do, because it isn't really solved. Do I leave this as unsolved, or is there a setting to say it can't be solved heh heh.

                mrjjM 1 Reply Last reply
                0
                • AsimovA Asimov

                  I have to say I like QT, but I am a little disappointed that there isn't an easier way of doing this.
                  I didn't want to put a lot of work into this until I knew I could do the hard part which is the saving and the loading.

                  I think my only option in this regard is to learn how rtf files are made, and then take the html from a QTextview and work out how to change it to rtf encoding.
                  Probably will be a nightmare.

                  Now I am not sure what to do, because it isn't really solved. Do I leave this as unsolved, or is there a setting to say it can't be solved heh heh.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Asimov
                  Well the MS RTF format is a old school now in the internet age
                  so i guess thats why html was chosen for the universal format.

                  However, many tools have been developed over the time and you can most likely get most
                  as reusable code
                  Like
                  https://github.com/lvu/rtf2html
                  But you also need the other way

                  So yes, using ms.RTF is not super easy as it not really used in Qt.

                  You can just leave it unsolved for some days.

                  There might be others with inputs or even code.

                  You could also dig up source code for kword
                  https://en.wikipedia.org/wiki/KWord
                  it handled rtf as far as remember.
                  And its Qt.

                  1 Reply Last reply
                  2
                  • AsimovA Offline
                    AsimovA Offline
                    Asimov
                    wrote on last edited by
                    #9

                    @mrjj
                    I have just come across this page
                    Page

                    ...and if you scroll down and look at individual formats it says it can do Microsoft's "RTF" format
                    Although if you click on it there is no information.

                    So you could think from looking at this page that QT can do ms rtf, or is the page wrong?

                    mrjjM 1 Reply Last reply
                    0
                    • AsimovA Asimov

                      @mrjj
                      I have just come across this page
                      Page

                      ...and if you scroll down and look at individual formats it says it can do Microsoft's "RTF" format
                      Although if you click on it there is no information.

                      So you could think from looking at this page that QT can do ms rtf, or is the page wrong?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Asimov
                      Well, i think it was just listed by accident
                      I have not seen Qt been able to read rtf.

                      When i first started with with Qt i though the rich text would be ms.rtf so i tried to stuff a file
                      into textedit and in that regard learned that "RTF" dont have to mean ms. rtf.

                      I checked with ancient Qt3 and its still html so it dont look like it was ever a format that Qt could load.

                      But I could be wrong :)

                      1 Reply Last reply
                      0
                      • AsimovA Offline
                        AsimovA Offline
                        Asimov
                        wrote on last edited by Asimov
                        #11

                        Well the good news is that the file loader will load an rtf eg

                        void MainWindow::on_pushButton_clicked()
                        {
                            QString file = QFileDialog::getOpenFileName(this,"Open a file");
                            if(!file.isEmpty()){
                                QFile sFile(file);
                                if(sFile.open(QFile::ReadOnly | QFile::Text)){
                                    QTextStream in(&sFile);
                                    QString text = in.readAll();
                                    sFile.close();
                        
                                    ui->textEdit->setAcceptRichText(true);
                                 ui->textEdit->setPlainText(text);
                                // ui->textEdit->setHtml(text);
                                }
                            }
                        }
                        

                        Obviously it loads all the rtf tags straight in. So if I was going to write a rtf decoder I would have to take each tag and convert it into the html equivalent, which I know won't be easy, but I have the whole file in the variable text.

                        I went into ms word and saved one line as an rtf, and for some reason when you look through the tags in the rtf document it seems to be setting the font about 20 times LOL

                        Wouldn't it be cool if I could just do

                        ui->textEdit->setRTF(text);
                        

                        Can you add your own commands to QT?

                        Anyway in theory you could find out the rtf command for bold, then read it and then set it bold using html before sending it to the Qtextedit window, but this could be rather difficult heh heh.

                        So I am going to do a lot more research. If I find an answer I will post it here. Would be cool if I could mark this as solved LOL

                        mrjjM 1 Reply Last reply
                        0
                        • AsimovA Asimov

                          Well the good news is that the file loader will load an rtf eg

                          void MainWindow::on_pushButton_clicked()
                          {
                              QString file = QFileDialog::getOpenFileName(this,"Open a file");
                              if(!file.isEmpty()){
                                  QFile sFile(file);
                                  if(sFile.open(QFile::ReadOnly | QFile::Text)){
                                      QTextStream in(&sFile);
                                      QString text = in.readAll();
                                      sFile.close();
                          
                                      ui->textEdit->setAcceptRichText(true);
                                   ui->textEdit->setPlainText(text);
                                  // ui->textEdit->setHtml(text);
                                  }
                              }
                          }
                          

                          Obviously it loads all the rtf tags straight in. So if I was going to write a rtf decoder I would have to take each tag and convert it into the html equivalent, which I know won't be easy, but I have the whole file in the variable text.

                          I went into ms word and saved one line as an rtf, and for some reason when you look through the tags in the rtf document it seems to be setting the font about 20 times LOL

                          Wouldn't it be cool if I could just do

                          ui->textEdit->setRTF(text);
                          

                          Can you add your own commands to QT?

                          Anyway in theory you could find out the rtf command for bold, then read it and then set it bold using html before sending it to the Qtextedit window, but this could be rather difficult heh heh.

                          So I am going to do a lot more research. If I find an answer I will post it here. Would be cool if I could mark this as solved LOL

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Asimov said in Saving and loading rtf documents:

                          setPlainText

                          The function will accept any text. including rtf as it just sees it as text.

                          • Can you add your own commands to QT?
                            Yes. ofc.
                            OFten you just subclass textEdit and add it.

                          You can get much of the code for convert from here
                          https://github.com/lvu/rtf2html

                          1 Reply Last reply
                          2
                          • AsimovA Offline
                            AsimovA Offline
                            Asimov
                            wrote on last edited by Asimov
                            #13

                            @mrjj
                            I looked at rtf2html, but there was a hell of a lot of h file and cpp files and it was hard for me to decypher what was needed and what wasn't.

                            I have found a handy little aricle for vb.net on code project

                            Article

                            This guy has broken down simply some of the commands needed to make an rtf. Seems he has only scraped the surface, but he has the main tags, eg bold, underline and stuff.

                            I have got to thinking that I don't need to know all the rtf commands there are. I just need to know the ones I am going to use to generate my rtf.

                            For instance I need to know how to make text bold,italic, new paragraph and the like and then I can generate an rtf document that word can read.

                            The only thing I have to work out now is how to parse the html commands so that I can turn them into the equivalent rtf tags.

                            I am thinking of some kind of array to hold all the html commands, and then some how when I get an html tag for bold swap it for the rtf tag for bold.

                            This could be difficult as I haven't done much string parsing in c++, but I have done this kind of thing in php.

                            PS. Probably not going to be as simple as I thought as the QT html markup is overkill
                            For instance I put one text line in and the html output was this

                            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
                            <html><head><meta name="qrichtext" content="1" /><style type="text/css">
                            p, li { white-space: pre-wrap; }
                            </style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
                            <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">kjkjhkjhkhkhk</p></body></html>
                            

                            That is what I call overkill html markup. If I was putting this in an actual webpage I would just do <p>fsdfsfsdf</p> so I am not sure why there is all this margin stuff. If you want a margin of zero that is usually the default.

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Hi
                              How much of RTF you need to support ?
                              Table also ?
                              Images ?
                              What about style sheets/styles

                              if you look in
                              https://github.com/lvu/rtf2html/blob/master/rtf_keyword.cpp
                              It has many features and i thinks its very important to set the goal of how much of it you want to support before
                              starting to think about coding it.

                              • but there was a hell of a lot of h file and cpp files and it was hard for me to decypher what was needed and what wasn't.

                              Yes a rtf parser is not trivial. All the rtf_xx files are needed. You could also just use it as is.
                              use QProcess to call it do behind the scene conversion.

                              1 Reply Last reply
                              0
                              • AsimovA Offline
                                AsimovA Offline
                                Asimov
                                wrote on last edited by
                                #15

                                At first I only want a few functions like bold,italic,underline. Later I may want to add images as well, but I would be happy with just text at first, after all it is writing software.

                                I have made simple rtfs just with coding as a test, but the problem isn't that. It is parsing the QT Html, as it seems to add a lot of markup which isn't needed, and it also has markup which doesn't seem to exist in normal html like

                                qt-block-indent:0;
                                

                                So even if I got a c++ libary to work it would never have that command in there, as it isn't standard html.

                                I am slowly thinking maybe I should start writing the software with using the html save at first, and then working out if I can add rtf later. I was going to read in rtf and save out rtf, but perhaps I can limit the rtf to an export button or something.

                                Reading in an rtf I created wouldn't be so bad. I can read the code for bold in rtf and turn it into <b> for Html, but to be honest I only wanted to add rtf support so that the files are compatible with ms word, or any other word processor.

                                Stupid thing is that I found out that word can actually load Html files (something I didn't know before.)
                                But people who use this kind of software have no idea that you can load an html into word, and they would probably cry that there is no rtf export LOL.

                                mrjjM 1 Reply Last reply
                                0
                                • AsimovA Asimov

                                  At first I only want a few functions like bold,italic,underline. Later I may want to add images as well, but I would be happy with just text at first, after all it is writing software.

                                  I have made simple rtfs just with coding as a test, but the problem isn't that. It is parsing the QT Html, as it seems to add a lot of markup which isn't needed, and it also has markup which doesn't seem to exist in normal html like

                                  qt-block-indent:0;
                                  

                                  So even if I got a c++ libary to work it would never have that command in there, as it isn't standard html.

                                  I am slowly thinking maybe I should start writing the software with using the html save at first, and then working out if I can add rtf later. I was going to read in rtf and save out rtf, but perhaps I can limit the rtf to an export button or something.

                                  Reading in an rtf I created wouldn't be so bad. I can read the code for bold in rtf and turn it into <b> for Html, but to be honest I only wanted to add rtf support so that the files are compatible with ms word, or any other word processor.

                                  Stupid thing is that I found out that word can actually load Html files (something I didn't know before.)
                                  But people who use this kind of software have no idea that you can load an html into word, and they would probably cry that there is no rtf export LOL.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @Asimov
                                  AS far as i Know its 100% valid HTML
                                  http://doc.qt.io/qt-5/richtext-html-subset.html
                                  But i never test it though :)

                                  I agree since it will be html internally, then you might as well wait with the RTF stuff as its likely export function.
                                  Also u can just cheat and call a external commandline app. plenty exists.

                                  Btw you could also generate MS xml word files. but then again, u if you need to import it again , that will be complicated also.
                                  Yes word have loaded html since 2003 :) (also before but that was awful)

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved