RTF (Rich Text Format) records text and layout instructions as plain text, so rich text can move between operating systems and word processors. Compared with Docx, RTF does not depend on a ZIP package or an XML structure, and older office software and some business systems can read it directly; compared with plain text, RTF still retains fonts, font sizes, colors, tables and images. Exchanging formatted documents between heterogeneous systems commonly requires converting between Word and RTF.
Spire.Doc for C++ writes RTF with FileFormat::Rtf in Document->SaveToFile(), loads RTF with Document->LoadFromFile() or Document->LoadRtf(), inserts an RTF fragment into a paragraph with Paragraph->AppendRTF(), and can save a loaded RTF directly as Word or PDF. All processing takes place in the local process, and the document does not need to be uploaded to any service.
This article covers four features:
The examples use the "Skylark" Customer Service Center 2026 Third-Quarter Operations Report (WordToRtf.docx). The document runs to two pages and contains five sections, two tables, one bar chart, and a header and footer; the body text is set in Arial:

The reverse conversion uses the Equipment Inspection Record (Legacy.rtf). The file was produced by saving the corresponding Word document as RTF and runs to two pages with four tables and a header and footer; it is used to check how much content survives the RTF-to-Word conversion.
Install Spire.Doc for C++
There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet; the other is to download the package from our website and copy the libraries into your program. Installing through NuGet is simpler and is the recommended approach.
- In Visual Studio, open the NuGet Package Manager, search for
Spire.Doc.Cpp, and install it. - Alternatively, run
nuget install Spire.Doc.Cppin the project directory, and then add the package'sincludeandlibfolders to the project.
An integrated project needs to reference the corresponding import library and to ensure that dependency files such as SpireDocCpp.dll and Spire.Doc.Base.dll are present in the run directory. For detailed configuration instructions, refer to How to Integrate Spire.Doc for C++ in a C++ Application.
Convert Word to RTF
Saving a Word document as RTF requires only specifying FileFormat::Rtf at save time. At load time, LoadFromFile() recognizes the Word file by its extension, and at save time the format enumeration determines the output type. Fonts, font sizes, colors, paragraph alignment, tables and images in the RTF are written as control words and groups, and the recipient can open the file without installing Spire.Doc.
| Operation | Main call |
|---|---|
| Load a Word document | Document->LoadFromFile(fileName) |
| Save as RTF | Document->SaveToFile(fileName, FileFormat::Rtf) |
| Save as PDF | Document->SaveToFile(fileName, FileFormat::PDF) |
| Load an RTF file | Document->LoadFromFile(fileName, FileFormat::Rtf) or Document->LoadRtf(fileName) |
| Insert an RTF snippet | Paragraph->AppendRTF(rtfCode) |
The detailed steps are as follows.
- Create a Document object.
- Load the sample Word file with the Document->LoadFromFile() method.
- Call Document->SaveToFile() and save the document with
FileFormat::Rtf. - Call Document->Close() to release the document.
The code for converting Word to RTF is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
int main() {
// Specify the input file path
string inputFile = "Data/WordToRtf.docx";
// Specify the output file path and name
string outputPath = "Output/";
string outputFile = outputPath + "WordToRtf.rtf";
// Create a Document object and load the Word document
intrusive_ptr<Document> document = new Document();
document->LoadFromFile(inputFile);
// Save the document as RTF
document->SaveToFile(outputFile, FileFormat::Rtf);
document->Close();
cout << "Saved: " << outputFile << endl;
}
Opened in Word, the converted RTF still runs to two pages, and the header and footer, the two tables and the bar chart are all retained:

Change in File Size
The sample Word document is 65,773 bytes, and the converted RTF is 3,748,230 bytes, about 57 times the original. Most of the growth comes from images: when writing a bitmap, RTF stores both a PNG image and a WMF preview, so the same chart is written twice; in addition, characters outside the code page are written as \uN escapes or \'hh hex escapes, and layout attributes are recorded one control word at a time.
For a text-heavy document, the growth comes mainly from formatting control words and character escapes and is noticeably smaller than for a document that contains images. When the recipient has a file-size limit, compress the images in the source document or reduce their number without affecting the content, rather than compressing the RTF after conversion.
What a Reloaded RTF Contains
After the generated RTF is loaded again, the document still runs to two pages, and the two tables, the header and footer, and the chart are all present. The chart is stored as a picture object in RTF rather than as Docx DrawingML; when the document is saved as Docx again, it is written as a VML picture. The paragraph count is one higher than in the original document because of the evaluation paragraph written at save time, which does not appear in a licensed environment.
This shows that RTF preserves structured content well and is suitable as an intermediate format for exchange between systems. If the document will continue to be edited in Word, keep the original Docx and do not treat the RTF as the only archive.
Convert RTF to Word
Converting RTF to Word follows the same process in reverse; the difference is that FileFormat::Rtf is specified at load time and FileFormat::Docx2013 at save time. Spire.Doc provides three ways to load RTF, and all three produce the same document object.
| Load method | Description |
|---|---|
Document->LoadFromFile(fileName, FileFormat::Rtf) |
Specifies the input format explicitly |
Document->LoadFromFile(fileName) |
Recognizes the file as RTF by its extension |
Document->LoadRtf(fileName) |
Uses the dedicated RTF load method |
The detailed steps are as follows.
- Create a Document object.
- Load the RTF file with the Document->LoadFromFile() method, specifying
FileFormat::Rtf. - Call Document->SaveToFile() and save the document with
FileFormat::Docx2013. - Call Document->Close() to release the document.
The code for converting RTF to Word is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
int main() {
// Specify the input file path
string inputFile = "Data/Legacy.rtf";
// Specify the output file path and name
string outputPath = "Output/";
string outputFile = outputPath + "Legacy.docx";
// Create a Document object and load the file as RTF
intrusive_ptr<Document> document = new Document();
document->LoadFromFile(inputFile, FileFormat::Rtf);
// Save the document as Word (docx)
document->SaveToFile(outputFile, FileFormat::Docx2013);
document->Close();
cout << "Saved: " << outputFile << endl;
}
An RTF file is plain text. The beginning records the RTF version, the character set, the default font and the font table, followed by the color table, the style sheet and the body content; characters outside the code page are written as \' or \u escapes:

Opened in Word, the file runs to two pages, with four tables and the header and footer intact. The converted Word document also runs to two pages, and the row and column structure of the tables, the cell shading, the header and footer, and the inspection route map are all retained:

What Content Is Preserved
The sample RTF contains 19 body paragraphs and 4 tables. The converted Docx keeps all the paragraphs and tables; the cell shading in the tables is written as w:shd in 47 places, exactly the same as in the Docx from which the RTF was generated. The inspection route map in the document is stored as a VML picture rather than in a w:drawing node, so a tool that counts only DrawingML pictures may report zero, but Word displays it normally.
At the style level, RTF does not carry the Docx style sheet, and the converted paragraphs are rebuilt from the formatting attributes in the RTF. A program that relies on built-in style names for subsequent processing should re-read the paragraph formatting after conversion rather than assume that the style names are unchanged.
Equivalence of the Three Load Methods
Loading the same RTF with the explicit format, by extension, and with LoadRtf() produces documents that are all two pages long and contain 4 tables, 47 instances of cell shading and 1 picture object. Which method is used does not affect the conversion result; the difference lies only in the call form: the explicit format suits files with non-standard extensions, and LoadRtf() suits callers that already know the input is RTF.
Convert RTF to PDF
PDF is a fixed-layout format suited to archiving, printing and distribution across devices. Converting RTF to PDF requires only changing the save format to FileFormat::PDF; the steps for loading the RTF are the same as in the previous section. During conversion, Spire.Doc lays out the document according to its page setup and embeds the fonts it uses in the output file.
The detailed steps are as follows.
- Create a Document object and load the RTF file.
- Call Document->SaveToFile() and save the document with
FileFormat::PDF. - Read Document->GetPageCount() to obtain the page count after conversion.
- Call Document->Close() to release the document.
The code for converting RTF to PDF is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
int main() {
// Specify the input file path
string inputFile = "Data/Legacy.rtf";
// Specify the output file path and name
string outputPath = "Output/";
string outputFile = outputPath + "Legacy.pdf";
// Create a Document object and load the RTF file
intrusive_ptr<Document> document = new Document();
document->LoadFromFile(inputFile, FileFormat::Rtf);
// Save the document as PDF
document->SaveToFile(outputFile, FileFormat::PDF);
// Print the page count after conversion
cout << "Pages: " << document->GetPageCount() << endl;
document->Close();
cout << "Saved: " << outputFile << endl;
}
The output is a two-page A4 document, and the header and footer, the tables and the route map all appear in the PDF:

Pagination May Differ from Word
In the sample, the PDF is two pages, and page 2 begins at section IV, the same point at which the same RTF breaks when opened in Word. The two layout engines still do not handle line height, paragraph spacing and table row splitting in exactly the same way, so pagination can diverge in other documents. When the page breaks are critical, check the page count and break points after conversion, and adjust margins, line spacing or table row heights if necessary, rather than assuming that the PDF matches Word exactly.
Font Handling
An RTF font table records only font names and character sets; the actual glyphs depend on the fonts installed on the current system. When converting to PDF, Spire.Doc looks up each font by name on the local machine and embeds the glyphs it uses as a subset. The recipient does not need the corresponding font installed to open the PDF, and the appearance matches the conversion environment. If a font declared in the RTF is missing locally, a substitute is used, and character widths and line heights may change; confirm that the required fonts are installed before a formal conversion.
Insert an RTF Snippet in a Paragraph
Besides converting whole files, Spire.Doc can insert an RTF fragment into an existing paragraph. Paragraph->AppendRTF() accepts a complete RTF fragment, parses it, and appends the text and formatting in the fragment to the end of the paragraph. This suits embedding prepared rich text in a template or joining RTF data produced by another system into a document.
| Overload | Description |
|---|---|
AppendRTF(rtfCode) |
Appends the fragment to the current paragraph |
AppendRTF(rtfCode, addtolastsection) |
The second parameter controls whether sections in the fragment are merged into the last section of the document |
The detailed steps are as follows.
- Create a Document object and call AddSection() to add a section.
- Add an explanatory paragraph containing the text that precedes the fragment.
- Create a target paragraph to receive the RTF fragment.
- Construct a complete RTF string containing
\rtf1, a font table, a color table and the body. - Call Paragraph->AppendRTF() to insert the fragment.
- Add a comparison paragraph and call Document->SaveToFile() to save the result.
The code for inserting an RTF fragment is shown below.
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Section.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/FileFormat.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Documents;
int main() {
// Create a blank document and a section
intrusive_ptr<Document> document = new Document();
intrusive_ptr<Section> section = document->AddSection();
// Insert the introductory paragraph
intrusive_ptr<Paragraph> intro = section->AddParagraph();
intro->AppendText("The following line is inserted from an RTF snippet:");
// Create the target paragraph and append the RTF snippet to it
intrusive_ptr<Paragraph> target = section->AddParagraph();
string rtf =
"{\\rtf1\\ansi\\deff0"
"{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}"
"{\\colortbl;\\red47\\green85\\blue151;}"
"\\uc1\\f0\\fs28\\b\\cf1 "
"This line was inserted from an RTF snippet."
"\\b0\\cf0\\par}";
target->AppendRTF(rtf);
// Insert a comparison paragraph
intrusive_ptr<Paragraph> tail = section->AddParagraph();
tail->AppendText("This is an ordinary paragraph after the snippet, used to compare font and size.");
// Save the result
document->SaveToFile("Output/AppendRtf.docx", FileFormat::Docx2013);
document->Close();
cout << "Saved: Output/AppendRtf.docx" << endl;
}
The text in the snippet is inserted in bold, 14-point blue Arial, and the paragraphs before and after keep their default formatting:

The Fragment Must Be Complete RTF
AppendRTF() accepts a complete RTF document fragment, not isolated control words. The fragment must start with \rtf1, include a font table \fonttbl, and end its body with \par. Without these structures, parsing may fail or produce only part of the content.
When the text contains characters that cannot be written directly into the RTF string, use \uN? escapes: N is the Unicode code point of the character, and \uc1 declares that each escape is followed by one fallback character, which is the ? in the example. The sample uses plain ASCII, so no escapes are needed, but the declaration is kept so that the fragment can be extended with non-ASCII text. Control words such as \b, \fs28 and \cf1 in the fragment are converted to Word bold, font size and font color when the fragment is inserted and are not displayed as text.
The addtolastsection Parameter
The two-parameter overload specifies whether the content of the fragment is merged into the last section of the document. When the fragment carries section information, setting the second parameter to true avoids creating an additional section; an ordinary fragment contains no section information and can be inserted with the single-parameter overload. After insertion, the paragraph and character formatting in the fragment are merged into the formatting system of the target paragraph, and later changes to the paragraph formatting apply the same rules to the fragment content and the existing content.
Function Comparison
| Function | Main call | Key point |
|---|---|---|
| Word to RTF | SaveToFile(..., FileFormat::Rtf) |
Keeps formatting, tables and images; the file becomes larger |
| RTF to Word | LoadFromFile(..., FileFormat::Rtf) + Docx2013 |
Cell shading and table structure are preserved |
| RTF to PDF | LoadFromFile(..., FileFormat::Rtf) + PDF |
Fixed layout; pagination may differ from Word |
| Insert an RTF snippet | Paragraph->AppendRTF(rtfCode) |
The fragment must be complete; non-ASCII text uses \uN? escapes |
The four operations share the same document object. Whole-file conversion takes place at load and save time, whereas snippet insertion modifies the document content at the paragraph level. Which approach to use depends on whether the input is a complete file or a fragment of rich-text data; for scenarios that require assembling content in a template, AppendRTF() avoids the intermediate step of generating a temporary file and then inserting it.
Get a Free License
If you want to remove the evaluation message from the generated documents, or to get rid of the function limitations, please apply for a 30-day temporary license.
