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

查看:91
本文介绍了如何显示在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.

用上面的方法也意味着,微软的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查看器组件或呈现逐页为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);
    }
}

这code需要提到的presentationCore,presentationFramework和ReachFramework组件。

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

编辑:上面的code包含了内存泄漏(见<一href="http://stackoverflow.com/questions/218681/opening-xps-document-in-net-causes-a-memory-leak">http://stackoverflow.com/questions/218681/opening-xps-document-in-net-causes-a-memory-leak).解决方法已被插入的例子。

The code above contained a memory leak (see http://stackoverflow.com/questions/218681/opening-xps-document-in-net-causes-a-memory-leak). The workaround has been been inserted in the example.

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

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