【问题标题】:Display XPS Document in WPF在 WPF 中显示 XPS 文档
【发布时间】:2015-09-01 04:36:51
【问题描述】:

我需要将数据库中的数据显示到 WPF 应用程序中并将其保存为 XPS 文档。我想将其显示为主窗口的一部分(带有工具栏、菜单和状态栏),而不是作为新窗口。

我应该使用什么控件?目前,我正在查看 FixedDocument 和 FlowDocument。我在正确的轨道上吗?有什么好的入门资料吗?

【问题讨论】:

    标签: wpf xps


    【解决方案1】:

    改进上面斯蒂芬的回答...

    假设您已在项目中添加了一个文档文件夹:

    创建一个 GetDocument 方法,其中 GetFilePath 方法引用上述文件夹中的文件夹/文件名。

        private void GetDocument()
        {     
            string fileName = Environment.CurrentDirectory.GetFilePath("Documents\\Title.xps");
            Debugger.Break();
            XpsDocument doc = new XpsDocument(fileName, FileAccess.Read);
            XDocViewer.Document = doc.GetFixedDocumentSequence();
        }
    

    GetFilePath 是一个扩展方法,如下所示:

    public static class StringExtensions
        {
            public static string GetFilePath(
                this string EnvironmentCurrentDirectory, string FolderAndFileName)
            {
                //Split on path characters
                var CurrentDirectory = 
                    EnvironmentCurrentDirectory
                    .Split("\\".ToArray())
                    .ToList();
                //Get rid of bin/debug (last two folders)
                var CurrentDirectoryNoBinDebugFolder = 
                    CurrentDirectory
                    .Take(CurrentDirectory.Count() - 2)
                    .ToList();
                //Convert list above to array for Join
                var JoinableStringArray = 
                    CurrentDirectoryNoBinDebugFolder.ToArray();
                //Join and add folder filename passed in
                var RejoinedString = 
                    string.Join("\\", JoinableStringArray) + "\\";
                var final = RejoinedString + FolderAndFileName;
                return final;
            }
        }
    

    【讨论】:

      【解决方案2】:

      在 XAML 中添加文档查看器

      并在cs文件中添加这段代码:

      string fileName = null;
      string appPath= System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(DocumentWindow)).CodeBase);
      fileName = appPath + @"\Documents\Help.xps";
      fileName = fileName.Remove(0, 6);
      XpsDocument doc = new XpsDocument(fileName, FileAccess.Read);
      docView.Document = doc.GetFixedDocumentSequence();
      

      【讨论】:

      • 谢谢。在保存之前,我需要先预览文档。因此,XPS 文档尚未保存。我正在寻找可以显示数据并稍后将其保存为 XPS 文档的控件。
      • 勾选此项以将您的文档显示并保存为 XPS stackoverflow.com/questions/502198/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 2011-02-04
      • 2010-12-26
      • 1970-01-01
      • 2010-10-04
      • 2011-06-15
      • 2011-01-21
      相关资源
      最近更新 更多