Convert All RTF Pages to Separate JPEG Image Files using C# .NET | Save JPEG Streams in SQL Database Table

I’m attempting to convert RTF data in a SQL Server Table into JPG data and save it in a SQL Server Table. The code is working fine but for only one page RTF documents. If the RTF data spans more than one page it only saves off the first page. Any ideas or work arounds? Is this just the behavior of JPG image files?

@bragan,

Yes, this is the expected behavior of JPG image i.e. it will contain the first page of RTF document only. However, you can convert all pages in RTF document to separate JPEG images by using the following code:

Document doc = new Document("E:\\temp\\input.rtf");

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg);
options.PageCount = 1;
int pageCountDoc = doc.PageCount;
for (int pageCount = 0; pageCount < pageCountDoc; pageCount++)
{
    options.PageIndex = pageCount;
    doc.Save(MyDir + "out_" + pageCount + ".jpg", options);
}

Or you can try converting the whole RTF file to TIFF Image by using the following code:

Document doc = new Document("E:\\temp\\input.rtf");
doc.Save("E:\\Temp\\20.6.tiff", SaveFormat.Tiff);

In this case, the TIFF image will contain the content of all pages in separate frames.

Thank you for the quick response!

While trying to implement your first code example I’m getting a reference not found issue with ImageSaveOptions. Do you have any suggestions on what reference I’m missing in my code?

Also, tiff will not work for what I’m trying to do due to limitations with SQL Server Reporting Services.

I figured out what was wrong with my reference. I need to include more of the namespace. I’m using Aspose.Words.Saving.ImageSaveOptions. Once included the code you provided worked! I only had to tweak it a little since I’m doing the conversion in a memory stream and then adding it to a database. Thanks again!

@bragan,

Yes, you need to add Aspose.Words.Saving namespace at the top for ImageSaveOptions class to work. It is great that you are now able to save JPEG (streams or byte[]) to SQL database. Please let us know any time you may have any further queries in future.