How to Convert RTF to PNG in C# .Net Framework

Cloudmersive
2 min readJan 31, 2024

--

There are two major disadvantages we encounter with RTF documents. First and foremost, RTF files aren’t compressed the most modern file formats are, so they take up more than their fair share of space. Second, they’re easily edited, which means RTF contents aren’t as secure as we might like.

Thankfully, using the below code, we can work around these disadvantages by using a free API to convert our RTF documents to PNG images. Unlike RTF files, PNG images are quite small and can’t be tampered with in any meaningful way. This makes PNG a great format to convert our content to before we share it across networks to wider audiences.

To set up our API call, let’s start by installing the SDK. Let’s install via NuGet by running the below command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

Next, let’s acquire a free-tier API key to authorize our API calls. We can get one of those by registering a free account on the Cloudmersive website; our free-tier key will give us a limit of 800 API calls per month with no additional commitments.

Finally, let’s use the below code examples to call the function. We just need to load our file in for a multipart/form-data request, and in our response we’ll receive a “PngRestultPages” object array containing URLs for each PNG file (relative to the RTF page number they represent):

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;

namespace Example
{
public class ConvertDocumentRtfToPngExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.

try
{
// Convert Rich Text Format RTF to PNG image array
RtfToPngResult result = apiInstance.ConvertDocumentRtfToPng(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentRtfToPng: " + e.Message );
}
}
}
}

That’s all the code we’ll need. Not it’s easy to make RTF to PNG conversions with zero hassle!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet