Ric*_*ACC 5 .net c# pdf excel ms-word
我们有一个应用程序,管理员可以在其中添加内容供其下属查看.他们的要求是它应该能够以不可编辑的方式显示Word,Excel,PowerPoint和PDF文档.
我发现这样做的一个选项是将内容加载到Web浏览器组件中.其缺点是它会提示用户打开/保存/取消.我们担心的是,大多数计算机文盲的下属都难以以这种方式打开文件.
使用上述方法还意味着需要在将运行该应用程序的所有计算机上安装Microsoft Office和Adobe Acrobat(或其他启用IE的PDF查看器),这意味着需要昂贵的许可费用.
有没有更好的方法让这些内容在C#中的表单上显示?
可能也很有趣:
使用 Microsoft Office 2007 将文档保存到 XPS(或将其打印到 XPS 打印机)。
您可以使用 XPS 查看器组件显示只读 XPS 文档,也可以逐页渲染为 PNG 或 JPEG 图像。使用 .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);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码需要引用PresentationCore、PresentationFramework 和ReachFramework 程序集。
编辑:上面的代码包含内存泄漏(请参阅在.Net 中打开 XPS 文档导致内存泄漏)。解决方法已插入示例中。
| 归档时间: |
|
| 查看次数: |
3133 次 |
| 最近记录: |