如果你做的系统和OA有关的,那肯定需要一个功能,就是附件预览。附件可能是text文本文件、image图片文件、Office文件、音频或视频文件等等。如果都能在程序里预览,绝对是系统的一个亮点。今天就来实现这个通用文档预览功能。
先定义好一个接口,内置一个方法Preivew,参数是文件的绝对路径。不同的文件预览组件使用Panel控件,里面再内置各种子控件。
//
// 摘要:
// 文档预览接口
public interface IDocumentPreview
{
//
// 摘要:
// 文档预览
//
// 参数:
// fileFullName:
// 文件完整名称(含路径)
//
// dicParam:
// 自定义参数列表
void Preview(string fileFullName, Dictionary<string, string> dicParam = null);
}
1、文档文件
包括文本文件、Office文件、Pdf、CAD。分别说下用什么组件开发的就行了,引用后调用很简单,不过多描述。
1.1、文本文件,用的是DevExpress.MemoEdit控件
1.2、Word文件,用的是DevExpress.RichEditControl控件

1.3、Excel文件,用的是DevExpress.SpreadsheetControl控件
1.4、PPT、Visio、Project等Office文件,用的是Edraw Office Viewer
1.5、PDF文件,用的是DevExpress.PdfViewer控件

1.6、CAD文件,这里要特别说明下,在Nuget中找到一个组件叫AnyCAD.Rapid.Net,也有自己的官网(http://www.anycad.cn/),也能引进到Net4.7的项目里,因为没有CAD文件,无法测试,有需求的可以试下;
如果这个方案不可行,还有B计划,就是把CAD文件转成PDF文件,这里就要用到Aspose.CAD这个组件
string folder = SystemHelper.GetSystemPath(SystemFolderName.Desktop); string cadFile = folder + @"\tempfile\testCad.dwg"; Aspose.CAD.Image cadImage1 = Aspose.CAD.Image.Load(cadFile); Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions(); Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions(); pdfOptions.VectorRasterizationOptions = rasterizationOptions; rasterizationOptions.PageHeight = 1600; rasterizationOptions.PageWidth = 1600; rasterizationOptions.DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseObjectColor; string pdfFile = folder + @"\tempfile\testCad.pdf"; cadImage1.Save(pdfFile, pdfOptions);
2、图片文件
这类文件直接使用微软的PictureBox或DevExpress的ImageEdit,都可以,但如果想预览Gif文件,这些组件就不行了。这里可以使用浏览器控件。WebBrowser或WebView2都可以,推荐后者
public partial class ControlImagePreview : Panel, IDocumentPreview
{
private double pagePercent = 10;
private PictureEdit pictureEdit = null;
public ControlImagePreview()
{
InitializeComponent();
this.pictureEdit = new PictureEdit();
this.pictureEdit.Dock = DockStyle.Fill;
this.Controls.Add(this.pictureEdit);
this.pictureEdit.MouseWheel += pictureEdit_MouseWheel;
this.pictureEdit.DoubleClick += pictureEdit_DoubleClick;
//可拖拽
this.pictureEdit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
}
private void pictureEdit_DoubleClick(object sender, EventArgs e)
{
this.pictureEdit.Properties.ZoomPercent = 100;
}
private void pictureEdit_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
{
double oldPercent = this.pictureEdit.Properties.ZoomPercent;
double newPercent = oldPercent + this.pagePercent;
this.pictureEdit.Properties.ZoomPercent = newPercent;
}
else if (e.Delta < 0)
{
double oldPercent = this.pictureEdit.Properties.ZoomPercent;
double newPercent = oldPercent - this.pagePercent;
if (newPercent < 0)
newPercent = 0;
this.pictureEdit.Properties.ZoomPercent = newPercent;
}
}
public void Preview(string fileFullName, Dictionary<string, string> dicParam = null)
{
Image image = Image.FromFile(fileFullName);
this.pictureEdit.Image = image;
}
}
3、音频文件
我用的是系统自带的MediaPlayer,值得欣慰的是,Win10系统仍然可以使用,只不过需要额外安装。安装好后,要到COM面板里找到引起来就可能
4、视频文件
之前使用的是暴风影音提供的COM组件,后来发现可以使用FFPlayer这个组件,方便很多。
5、Flash文件
这也文件格式也是很老的了,如果想预览也是可以的。就要下载Adobe Falsh组件,安装好后也是通过COM组件引进来的。直接播放就可以
相关阅读



文章评论