如何在Windows窗体上显示办公室和/或pdf内容? [英] How do I display office and/or pdf content on a windows form?

查看:111
本文介绍了如何在Windows窗体上显示办公室和/或pdf内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序,管理员可以在其中添加内容供其下属查看。他们的要求是它应该能够以不可编辑的方式显示Word,Excel,PowerPoint和PDF文档。

We have an application in which admin members can add content for their subordinates to view. Their requirement is that it should be able to display Word, Excel, PowerPoint and PDF documents in a non-editable manner.

我发现这样做的一个选项是将内容加载到Web浏览器组件中。缺点是它提示用户打开/保存/取消。我们担心下属,主要是电脑文盲,将无法以这种方式打开文档。

The one option that I found for doing this is to have the content loaded into a web browser component. The downside to that is that it prompts the user to open/save/cancel. We are concerned that the subordinates, being mostly computer illiterate, will have trouble opening the documents in this manner.

使用上述方法也意味着Microsoft Office和Adobe Acrobat或另一个启用IE的PDF查看器)需要安装在运行应用程序的所有机器上,这意味着昂贵的许可费用。

Using the above method also means that Microsoft Office and Adobe Acrobat (or another IE enabled PDF viewer) need to be installed on all the machines that will be running the application, which implies expensive licensing fees.

有更好的方法来获取这个内容要在C#中显示在我的表单上?

Is there a better way to get this content to display on my forms in C#?

推荐答案

可能也很有趣:

使用Microsoft Office 2007将文档保存到XPS(或将其打印到XPS打印机)。

Save the documents to XPS using Microsoft Office 2007 (or print them to an XPS printer).

您可以使用只读XPS文档显示只读XPS文档XPS查看器组件或逐页呈现到PNG或JPEG图像中。这个渲染可以很容易地使用.NET 3.5 / WPF来实现。

You can display the read-only XPS document either using the XPS viewer component or render page by page into a PNG or JPEG image. This rendering can be achieved quite easily using .NET 3.5 / WPF.

XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);

FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
const double scaleFactor = 0.8;
for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; pageNum++)
{
    DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);

    // FIX: calling GetPage without calling UpdateLayout causes a memory leak
    ((FixedPage)docPage.Visual).UpdateLayout();

    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)Math.Round(scaleFactor * docPage.Size.Width),
                (int)Math.Round(scaleFactor * docPage.Size.Height), (int)Math.Round(scaleFactor * 96), (int)Math.Round(scaleFactor * 96), PixelFormats.Default);
    renderTarget.Render(docPage.Visual);

    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.QualityLevel = 75;
    // Choose type here ie: JpegBitmapEncoder, etc
    //BitmapEncoder encoder = new PngBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc
    encoder.Frames.Add(BitmapFrame.Create(renderTarget));

    string pageImageFileName = string.Format("{0}-{1}.jpg", Path.Combine(Path.GetDirectoryName(xpsFileName), Path.GetFileNameWithoutExtension(xpsFileName)), pageNum);
            using (FileStream pageOutStream = new FileStream(pageImageFileName, FileMode.Create, FileAccess.Write))
    {
        encoder.Save(pageOutStream);
    }
}

此代码需要引用PresentationCore,PresentationFramework和ReachFramework程序集。

This code needs references to the PresentationCore, PresentationFramework and ReachFramework assemblies.

编辑:上面的代码包含内存泄漏(请参阅打开.Net中的XPS文档导致内存泄漏)。该示例中已插入了解决方法。

The code above contained a memory leak (see Opening XPS document in .Net causes a memory leak). The workaround has been been inserted in the example.

这篇关于如何在Windows窗体上显示办公室和/或pdf内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆