Convert RTF to Word in C#
Nutrient .NET SDK (formerly GdPicture.NET) includes the ability to convert any supported file type into Word.
To save an RTF document to a Word document (DOCX), first use the LoadFromFile method of the GdPictureDocumentConverter class to load it. Then use the SaveAsDOCX method to convert it to a DOCX.
The LoadFromFile method uses the following parameter:
FilePath— The source RTF file path.DocumentFormat— A member of theDocumentFormatenumeration. UseDocumentFormat.DocumentFormatRTFfor RTF input.
The SaveAsDOCX method uses the following parameter:
Stream, or the overloadFilePath— A stream or file path where the currently loaded document is saved as a DOCX file. When using a stream, it must be open for reading, writing, and seeking.
When you use the stream overload, the stream should be open for both reading and writing, and it should be closed/disposed of by the user once processing is complete using the CloseDocument method.
How to convert RTF to DOCX
- Create a
GdPictureDocumentConverterobject. - Load the source RTF file with
GdPictureDocumentConverter.LoadFromFile(String, DocumentFormat)usingDocumentFormat.DocumentFormatRTF. - Save the loaded document as a DOCX using
SaveAsDOCX.
The following example converts and saves an RTF document to a DOCX file:
using GdPictureDocumentConverter converter = new();
GdPictureStatus status = converter.LoadFromFile(@"input.rtf", GdPicture14.DocumentFormat.DocumentFormatRTF);if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
status = converter.SaveAsDOCX("output.docx");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
status = converter.CloseDocument();if (status != GdPictureStatus.OK && status != GdPictureStatus.Aborted){ throw new Exception(status.ToString());}
Console.WriteLine("The input document has been converted to a docx file");Optional DOCX configuration properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter object:
DocxImageQualityis an integer from 0 to 100 that specifies the quality used to compress images embedded in the resulting DOCX document.RasterizationDPIis an integer that specifies the resolution, expressed in dots per inch (DPI), used when vector content must be rasterized during conversion to DOCX.