Home : Can DotImage Open/Render a Text Document (plaintext)
Q10440 - FAQ: Can DotImage Open/Render a Text Document (plaintext)

Over the years, we've had quite a few requests to be able to open/render a text file in DotImage.

DotImage is an imaging SDK and is really meant for working with images. We've never had a TextDecoder to convert text files to images, so when customers have asked if this is possible, the answer is that they'll need to find a means of converting their text file to an image format we support.

With the introduction of our OfficeDecoder, we now do have the ability (with the OffideDecoder add-on) to read RTF files. RTF is Microsoft Rich Text Format, and although it doesn't directly support plain text files, with a little bit of modification, it's fairly straightforward to take any given simple text string and convert it to a workable RTF file in .NET code

This is made possible because RTF itself is a markup format, so by adding the correct "header" information, escaping a few needed escape characters and converting the CRLFs to the correct RTF paragraph breaks, we can convert an incoming string of ascii (with linebreaks etc..) into RTF.

Brute Force Text to RTF

private string PlainTextToRtf(string plainText)
{
    string escapedPlainText = plainText.Replace(@"\", @"
\\").Replace("{", @"\{").Replace("}", @"\}");
    string rtf = @"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard ";
    rtf += escapedPlainText.Replace(Environment.NewLine, @" \par ");
    rtf += " }";
    return rtf;
}

Please note: credit for this block of code goes to user Antonio Bakula on StackOverflow. His original reply to the question of converting text to rtf can be found here:
http://stackoverflow.com/questions/10725438/converting-txt-to-rtf

So, now that you have RTF, our OfficeDecoder will be able to decode it, you just need to get the string into either a file or into a stream.. so building on our existing method here:


private MemoryStream TextStringToRtfStream(string plainText)
{
    MemoryStream ms = new MemoryStream();
    string rtfString = PlainTextToRtf(plainText);
 
    StreamWriter writer = new StreamWriter(ms);
    writer.Write(rtfString);
    writer.Flush();
    rtfString = null;
   
    // Important!! rewind the stream before returning it so it's ready to be used
    ms.Seek(0, SeekOrigin.Begin);
    return ms;
}

The MemoryStream returned by this method is an RTF file which may end up being one or more pages worth of text, so you want to treat it like any incoming multipage file.

The following code example assumes that you have an OfficeDecoder properly licensed, referenced and added to your RegisteredDecoders.Decoders collection. Please see
Q10439 - HOWTO: Safely Change / Set Resolution of OfficeDecoder

For more details


int framecount = RegisteredDecoders.GetImageInfo(rtfStream).FrameCount;
rtfStream.Seek(0, SeekOrigin.Begin);
for(int i=0; i < frameCount; i++)
{
    AtalaImage currentFrame = new AtalaImage(rtfStream, i, null);
    // your code here to work with currentFrame
 
    // when done, we need to reset and clean memory
    currentFrame.Dispose();
    rtfStream.Seek(0, SeekOrigin.Begin);
}

Alternately, you can use the rtfStream with a StreamImageSource
see the following KB for details
Q10424 - HOWTO: StreamImageSource - The Missing ImageSource option

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved