将RTF文档转换为Image或在服务器上打印RTF

时间:2009-03-20 12:55:42

标签: .net rtf page-layout

我有一种情况,我想将RTF 文档转换为图像进行存档和打印。我正在使用.NET 那里有没有可以帮助我进行转换的库吗?

我需要

  • 将RTF转换为服务器上的图像
  • 设置创建图像时需要遵守的纸张尺寸

商业图书馆是一种选择,但我更喜欢操作系统。 如果有客户端方式这样做也是一个有效的答案,但服务器端将非常好。

编辑:

感谢所有出色的答案。由于所有这些都涉及打印RTF文档,因此我有一个跟进问题:

  • 在服务器上打印RTF文档的最佳方法是什么

4 个答案:

答案 0 :(得分:2)

我的建议是使用转储到图像的打印驱动程序 - 这样您就可以使用标准打印功能(如包括纸张尺寸),然后抓取文件并将其用于实际打印或存档。

免费&开源版本是:Virtual Image Printer driver

答案 1 :(得分:0)

我最近不得不处理这件事。我们的应用程序允许用户在标准控件中编辑一些RTF(随Visual Studio一起提供),然后将其转换为图像,以便我们将其发送到另一个不了解RTF的应用程序。

我在网上看起来非常努力,看起来唯一可能是拍摄控件的屏幕截图并将其转换为图像。意味着不会出现在可视区域之外的任何文本(即您必须滚动)。真的让我感到惊讶的是它必须被这样砍掉。

我知道你问过商业图书馆,但我想我会告诉你我对内置控件的体验。

答案 2 :(得分:0)

扩展罗伯特的答案,如果需要,您可以通过选择操作系统附带的“标准”打印机并打印到文件来避免下载标准打印驱动程序。很多驱动程序正在使用标准版本的postscript。通常很容易将postscript文件转换为pdf文件以供查看(如果需要)。打印它们通常也很容易。

这个解决方案比使用输出图像的专用驱动程序稍微多一点。

答案 3 :(得分:0)

我可以通过以下代码片段捕获richtextbox的图片。我认为它也可能适用于您。

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();

    }