【问题标题】:Convert a RTF document to Image or print RTF on server将 RTF 文档转换为图像或在服务器上打印 RTF
【发布时间】:2010-10-14 12:58:35
【问题描述】:

我想将 RTF 文档转换为图像 以进行存档和打印。我正在使用 .NET。
是否有任何库可以帮助我进行这种转换?

我需要

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

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

编辑:

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

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

【问题讨论】:

    标签: .net rtf page-layout


    【解决方案1】:

    我可以通过下面的代码 sn-p 来捕获富文本框的图片。我想它也可能对你有用。

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

    【讨论】:

      【解决方案2】:

      扩展 Robert 的答案,如有必要,您可以通过选择操作系统随附的“标准”打印机并打印到文件来避免下载标准打印驱动程序。许多司机都在使用标准版本的后记。如果需要,将 postscript 文件转换为 pdf 文件以供查看通常非常容易。打印它们通常也很容易。

      与仅使用输出图像的专用驱动程序相比,此解决方案的工作量略多。

      【讨论】:

        【解决方案3】:

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

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

        我知道您询问了商业库,但我想我会让您知道我对内置控件的体验。

        【讨论】:

          【解决方案4】:

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

          免费开源版本为:Virtual Image Printer driver

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-04-04
            • 1970-01-01
            • 2011-07-27
            • 2012-07-22
            • 1970-01-01
            • 2012-08-31
            • 2011-06-07
            相关资源
            最近更新 更多