小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. net编程
  3. 开发实例
  4. 正文

c#中实现Word、Excel、Pdf预览及音频和视频播放

2022年12月6日 4535点热度 1人点赞 0条评论

如果你做的系统和OA有关的,那肯定需要一个功能,就是附件预览。附件可能是text文本文件、image图片文件、Office文件、音频或视频文件等等。如果都能在程序里预览,绝对是系统的一个亮点。今天就来实现这个通用文档预览功能。

文档预览

先定义好一个接口,内置一个方法Preivew,参数是文件的绝对路径。不同的文件预览组件使用Panel控件,里面再内置各种子控件。

//
// 摘要:
//     文档预览接口
public interface IDocumentPreview
{
    //
    // 摘要:
    //     文档预览
    //
    // 参数:
    //   fileFullName:
    //     文件完整名称(含路径)
    //
    //   dicParam:
    //     自定义参数列表
    void Preview(string fileFullName, Dictionary<string, string> dicParam = null);
}

 

Table of Contents

Toggle
  • 1、文档文件
  • 2、图片文件
  • 3、音频文件
  • 4、视频文件
  • 5、Flash文件

1、文档文件

包括文本文件、Office文件、Pdf、CAD。分别说下用什么组件开发的就行了,引用后调用很简单,不过多描述。
1.1、文本文件,用的是DevExpress.MemoEdit控件
1.2、Word文件,用的是DevExpress.RichEditControl控件
word文件预览
1.3、Excel文件,用的是DevExpress.SpreadsheetControl控件
1.4、PPT、Visio、Project等Office文件,用的是Edraw Office Viewer
1.5、PDF文件,用的是DevExpress.PdfViewer控件
pdf文件预览
1.6、CAD文件,这里要特别说明下,在Nuget中找到一个组件叫AnyCAD.Rapid.Net,也有自己的官网(http://www.anycad.cn/),也能引进到Net4.7的项目里,因为没有CAD文件,无法测试,有需求的可以试下;

nuget中引用AnyCAD

如果这个方案不可行,还有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面板里找到引起来就可能

com中引用MediaPlayer

4、视频文件

之前使用的是暴风影音提供的COM组件,后来发现可以使用FFPlayer这个组件,方便很多。

5、Flash文件

这也文件格式也是很老的了,如果想预览也是可以的。就要下载Adobe Falsh组件,安装好后也是通过COM组件引进来的。直接播放就可以

 

相关阅读

c#中使用Aspose.Word组件,将数据和图片导出至Word

c#中使用Aspose.Cells组件,将图片导出至Excel示例

标签: c#教程 Excel预览 net教程 Office预览 Pdf预览 Word预览 文档预览 视频播放 音频播放
最后更新:2022年12月6日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2