Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri May 29, 2020 9:28 am

Hi,

Good day.

I'm checking the Spire.Doc to convert html to rtf and then save the rtf string into database to be used by report generation application.
I tried saving the rtf to a file and when I opened the file it shows the expected results.
However, when I tried to get the rtf string and save to db and then get the rtf from db and then render it is not the same as the one saved to a file.

Is there a function to get the rtf string (not the text)?
Please advise on how to properly save the rtf to db without losing the rtf format.

Below is the code snippet.
Refer to attached files for the html, saved rtf and rtf saved to db.
Test.zip


Thank you.

Code: Select all
string html = model.InfoHtml; //from html rich text editor

using (MemoryStream inputStream = new MemoryStream())
{
   StreamWriter writer = new StreamWriter(inputStream);
   writer.Write(html);
   writer.Flush();                       

   using (Document doc = new Document())
   {
      doc.LoadFromStream(inputStream, FileFormat.Html, Spire.Doc.Documents.XHTMLValidationType.None);
      doc.SaveToFile(rtfFilename, FileFormat.Rtf);

      using (MemoryStream outputStream = new MemoryStream())
      {
          doc.SaveToStream(outputStream, FileFormat.Rtf);
          model.InfoRtf = Encoding.UTF8.GetString(outputStream.GetBuffer(), 0, (int)outputStream.Length);
          outputStream.Close();
          outputStream.Dispose();
      }

      doc.Close();
      doc.Dispose();
   }

   writer.Close();
   writer.Dispose();
   inputStream.Close();
   inputStream.Dispose();
}

Charllemagne
 
Posts: 6
Joined: Fri Jul 05, 2019 6:16 am

Fri May 29, 2020 10:36 am

Hello,

Thanks for your inquiry.
How do you convert rtf string to the rtf format document? I tested the below code with the latest Spire.Doc Pack(hot fix) Version:8.5.8, but the format of the two generated rtf documents are the same. Attached are my output files. If you are using an older version, I'd suggest you download the latest version and try again. Looking forward to your response.
Code: Select all
    using (Document doc = new Document())
    {
        doc.LoadFromFile("test.html", FileFormat.Html, Spire.Doc.Documents.XHTMLValidationType.None);
        doc.SaveToFile("HtmlToRtf.rtf", FileFormat.Rtf);

        using (MemoryStream outputStream = new MemoryStream())
        {
            doc.SaveToStream(outputStream, FileFormat.Rtf);
            string rtfString = Encoding.UTF8.GetString(outputStream.GetBuffer(), 0, (int)outputStream.Length);
            File.WriteAllText("RtfStringToRtf.rtf", rtfString);
            outputStream.Close();
            outputStream.Dispose();
        }
        doc.Close();
        doc.Dispose();
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri May 29, 2020 11:05 am

Hi,

Thank you for the reply.

I also tried saving the rtf string to a file as what you did and the output is ok.

How do you convert rtf string to the rtf format document?

I copied the string from db and paste in notepad++ and save as rtf.

Basically my issue is that, when I get the rtf string from db and then paste it to notepad++ and save as rtf then the output is different.
Refer to Test_DB.rtf for the rtf string from DB.

If I copy the rtf string from Test_DB.rtf and paste to notepad++ and save as rtf, the output is ok.
So I'm just wondering why the output is different after saving the rtf string to db.

I am just pasting the rtf string to notepad++ to quickly test.

Regarding my question on the function to get rtf string, what I mean is do Spire.Doc has a function to return the rtf string.
So that I don't need to read the file or from the stream to get the rtf string.

I am using version 7.11.0

Charllemagne
 
Posts: 6
Joined: Fri Jul 05, 2019 6:16 am

Fri May 29, 2020 4:12 pm

Hi,

After looking at the notepad++ for quite sometime, I showed the symbols and I noticed the carriage return and line feed (CRLF) in the rtf document.
When I checked the rtf string, the CRLF was converted into 2 spaces and this is the string which is saved to db.
So when I get the rtf string from db and then renders, it is the additional space that causes the unwanted format.

So to whoever that is planning in saving the rtf to a database to be used for report generation backend, you can check below code snippet.

Code: Select all
        public static string ConvertHtmlToRtf(string html)
        {
            string rtf = string.Empty;
            using (MemoryStream inputStream = new MemoryStream())
            {
                StreamWriter writer = new StreamWriter(inputStream);
                writer.Write(html);
                writer.Flush();

                using (Document doc = new Document())
                {
                    doc.LoadFromStream(inputStream, FileFormat.Html, Spire.Doc.Documents.XHTMLValidationType.None);
                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        doc.SaveToStream(outputStream, FileFormat.Rtf);
                        rtf = Encoding.UTF8.GetString(outputStream.GetBuffer(), 0, (int)outputStream.Length);
                        rtf = rtf.Replace(Environment.NewLine, string.Empty);
                        outputStream.Close();
                        outputStream.Dispose();
                    }

                    doc.Close();
                    doc.Dispose();
                }

                writer.Close();
                writer.Dispose();
                inputStream.Close();
                inputStream.Dispose();

                return rtf;
            }

Charllemagne
 
Posts: 6
Joined: Fri Jul 05, 2019 6:16 am

Mon Jun 01, 2020 7:35 am

Hello,

Thanks for your feedback.
You mean using the new code you provided, the format issue will be resolved, right?
In addition, for the code you provided earlier, I tested your scenario as you described but did not reproduce your issue. Attached are my full test code and output file, please check.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Jun 01, 2020 8:35 am

Hi Rachel,

You mean using the new code you provided, the format issue will be resolved, right?

Yes. I just added rtf = rtf.Replace(Environment.NewLine, string.Empty); so that there will be no unwanted spaces when saving to database.

Basically the previous codes are ok when saving to file.
The issue is on saving to the database as the CRLF is converted into a space thus adding unwanted space character string.
So when the rtf string from db gets rendered into the word document, the format is different already due to the additional spaces.

Just want to raise if you might consider adding in the future a function to return the string of rtf/html/etc.
Currently you have a function to get the Text without the tags.
But would be nice also if you can provide a function that will return the complete rtf/html string so no need to do the stream or file reading operation.
There might be other users that are planning to save certain rtf or html into the db for certain document generation.

Thanks for the feedback ^^

Charllemagne
 
Posts: 6
Joined: Fri Jul 05, 2019 6:16 am

Mon Jun 01, 2020 10:27 am

Hello,

Thanks for your response.
Sorry to tell you that this function is not planned in current upgrade list of our Spire.Doc. If there is anything else we can do for you, please feel free to let us know.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Jun 01, 2020 10:56 am

Hi Rachel,

Free version is limited to 500 paragraphs and 25 tables. This limitation is enforced during reading or writing files.


Regarding the free version, is the 500 paragraphs the accumulated count of all operations done since using Spire.doc or is it per document instance or per file?
Like now I've been reading and converting html to do some testing.
So the paragraph limit will be reached soon?

Charllemagne
 
Posts: 6
Joined: Fri Jul 05, 2019 6:16 am

Tue Jun 02, 2020 1:30 am

Hello,

This limitation is for per file. If you have further questions, please feel free to let us know.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Jun 02, 2020 1:56 am

Hi Rachel,

Thank you for the confirmation.

Charllemagne
 
Posts: 6
Joined: Fri Jul 05, 2019 6:16 am

Tue Jun 02, 2020 2:27 am

Hello,

You are welcome.
If you encounter any issues when testing our products, welcome to contact us. We are always here to help.
Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc