Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WPF中显示XPS文档_Wpf_Xps - Fatal编程技术网

在WPF中显示XPS文档

在WPF中显示XPS文档,wpf,xps,Wpf,Xps,我需要将数据库中的数据显示到WPF应用程序中,并将其另存为XPS文档。我想将其显示为主窗口(带有工具栏、菜单和状态栏)的一部分,而不是新窗口 我应该使用什么控件?目前,我正在查看FixedDocument和FlowDocument。我走对了吗?有关于如何开始的好材料吗?在XAML中添加文档查看器 并将此代码添加到cs文件中: string fileName = null; string appPath= System.IO.Path.GetDirectoryName(Assembly.GetAs

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


我应该使用什么控件?目前,我正在查看FixedDocument和FlowDocument。我走对了吗?有关于如何开始的好材料吗?

在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();

改进上面Stehpen的答案

假设您已将文档文件夹添加到项目中:

创建一个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;
        }
    }

谢谢我需要先预览文档,然后再保存它。因此,XPS文档尚未保存。我正在寻找一个控件,可以在其中显示数据,然后将其另存为XPS文档。请选中此选项,以将您的文档显示为XPS文档并将其另存为XPS文档