Question

I have a situation where I want to convert a RTF document to image for archiving and printing. I am using .NET.
Are there any libraries out there that could help me with this conversion?

I need to

  • convert a RTF to image on a server
  • set the paper size that needs to be adhered to when creating the image

A commercial library is an option but I prefer OS. If there is a client side way to do that that's a valid answer as well, but server side would be extremely nice.

Edit:

Thanks for all the great answers. Since all of them involve printing a RTF document I have a follow up question:

  • What is the best way to print an RTF document on a server
Was it helpful?

Solution

My recommendation would be to have a print driver that dumps to an image - that way you can use the standard printing features (like including paper size) and then grab the file and use that for actual printing or archiving.

Free & Open source version is: Virtual Image Printer driver

OTHER TIPS

I have had to deal with this recently. Our application would allow the user to edit some RTF in a standard control (comes with visual studio) and then convert it to an image so we could send it to another application that didn't understand RTF.

I looked pretty hard online and it appeared the only possibility was to take a screenshot of the control and convert that to an image. Meaning any text that was outside the viewable area (i.e. you had to scroll) would not appear. Surprised me really that it had to be hacked up like that.

I know you asked about commercial libraries but I thought I'd let you know my experience with the built-in control.

Expanding on Robert's answer, you can, if necessary, avoid downloading a standard print driver by just picking a "standard" printer that comes with the operating system and printing to file. Plenty of the drivers are justing using a standard version of postscript. It is generally pretty easy to convert postscript files to pdf files for viewing if needed. Printing them is generally easy, too.

This solution is slightly more work than just using a specialized driver which outputs an image.

I was able to capture the picture of the richtextbox by following code snippet. i think it may be of your use also.

private void ShowBitmap_btn_Click(object sender, RoutedEventArgs e)
    {
        if (MyTextBox_txt == null)
            return;

        Rect _descendentBounds = VisualTreeHelper.GetDescendantBounds(MyTextBox_txt);
        //RenderTargetBitmap _targetBitmap = new RenderTargetBitmap((Int32)_descendentBounds.Width, 
        //                                                          (Int32)_descendentBounds.Height, 
        //                                                          96, 96, PixelFormats.Pbgra32);

        Rect _tempRect = new Rect(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
                                    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y,
                                    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
                                    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
        RenderTargetBitmap _targetBitmap = new RenderTargetBitmap((Int32)_tempRect.Width,
                                                                            (Int32)_tempRect.Height,
                                                                                96, 96, PixelFormats.Pbgra32);

        DrawingVisual _drawingVisual = new DrawingVisual();

        using (DrawingContext _drwaingContext = _drawingVisual.RenderOpen())
        {
            VisualBrush _visualBrush = new VisualBrush(MyTextBox_txt);
            _drwaingContext.DrawRectangle(_visualBrush, null, new Rect(new Point(), _tempRect.Size));
        }

        _targetBitmap.Render(_drawingVisual);

        PngBitmapEncoder _png = new PngBitmapEncoder();

        _png.Frames.Add(BitmapFrame.Create(_targetBitmap));
        Stream _fileStream;
        _fileStream = File.Create(@"E:\sample1.png");

        _png.Save(_fileStream);

        System.Drawing.Bitmap _tempBitmap = new System.Drawing.Bitmap(_fileStream);
        _tempBitmap.Save(@"E:\sample1.bmp");

        _fileStream.Close();
        _fileStream.Dispose();

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top