我有一种情况,我想将 RTF文档转换为图像以进行存档和打印。我正在使用.NET。
有没有可以帮助我进行这种转换的库?
我需要
- 将 RTF 转换为服务器上的图像
- 设置创建图像时需要遵守的纸张尺寸
商业图书馆是一种选择,但我更喜欢操作系统。如果有客户端方法可以做到这一点,那也是一个有效的答案,但服务器端会非常好。
编辑:
感谢所有伟大的答案。由于所有这些都涉及打印 RTF 文档,因此我有一个后续问题:
- 在服务器上打印 RTF 文档的最佳方法是什么
我有一种情况,我想将 RTF文档转换为图像以进行存档和打印。我正在使用.NET。
有没有可以帮助我进行这种转换的库?
我需要
商业图书馆是一种选择,但我更喜欢操作系统。如果有客户端方法可以做到这一点,那也是一个有效的答案,但服务器端会非常好。
编辑:
感谢所有伟大的答案。由于所有这些都涉及打印 RTF 文档,因此我有一个后续问题:
我的建议是拥有一个转储到图像的打印驱动程序 - 这样您就可以使用标准打印功能(例如包括纸张大小),然后获取文件并将其用于实际打印或存档。
免费和开源版本是:虚拟图像打印机驱动程序
我最近不得不处理这个问题。我们的应用程序将允许用户在标准控件(Visual Studio 附带)中编辑一些 RTF,然后将其转换为图像,以便我们可以将其发送到另一个不理解 RTF 的应用程序。
我在网上看起来很努力,似乎唯一的可能性就是截取控件的屏幕截图并将其转换为图像。这意味着在可视区域之外的任何文本(即您必须滚动)都不会出现。真的让我感到惊讶,它必须被这样破解。
我知道你问过商业图书馆,但我想我会让你知道我对内置控件的体验。
扩展罗伯特的答案,如有必要,您可以通过选择操作系统附带的“标准”打印机并打印到文件来避免下载标准打印驱动程序。许多司机都在使用标准版本的后记。如果需要,将 postscript 文件转换为 pdf 文件以供查看通常非常容易。打印它们通常也很容易。
此解决方案比仅使用输出图像的专用驱动程序稍微多一些工作。
通过以下代码片段,我能够捕获富文本框的图片。我想它也可能对你有用。
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();
}