单击链接后,如何在浏览器窗口中打开 C# 项目中包含的 PDF? [英] How do I open a PDF included in my C# project in a browser window upon link click?

查看:49
本文介绍了单击链接后,如何在浏览器窗口中打开 C# 项目中包含的 PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个名为Guides"的文件夹.并添加了My Admin Guide.pdf".

I have a folder in my project called "Guides" and have added "My Admin Guide.pdf".

相关的xaml:

<TextBlock Margin="20,20,0,0" TextWrapping="Wrap">
    <Hyperlink x:Name="Documentation_Hyperlink" NavigateUri="/Guides/My Admin Guide.pdf" TargetName="_blank" RequestNavigate="Documentation_Hyperlink_RequestNavigate">
        Click Here for the Admin Guide
    </Hyperlink>
</TextBlock>

在代码隐藏中:

private void DDI_Documentation_Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    Process.Start(e.Uri.ToString());
}

当我测试时,出现异常系统找不到指定的文件".我已经验证 e.Uri.ToString() 的值为/Guides/My Admin Guide.pdf.我该如何正确执行此操作?

When I test, I get an exception "the system cannot find the file specified". I've verified e.Uri.ToString() has a value of /Guides/My Admin Guide.pdf. How do I do this properly?

推荐答案

没有像链接方法那样容易做到这一点,但是一旦实践过的方法并没有那么复杂.首先确保您自己生成的 PDF 文档是作为嵌入资源生成的.然后按照以下步骤操作:

There is no easy as a link method to do the trick but the method once practiced is not so complicated. First of all assure yourself the PDF document is generated as an embedded resource. Then follow those steps :

Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("{your_assembly_name}.{your_folder_and_file_name}");
FileInfo source = new FileInfo("{a_name_for_the_file}");
byte[] b;

using (BinaryReader br = new BinaryReader(stream))
{
  b = br.ReadBytes((int)stream.Length);
}
File.WriteAllBytes(source.FullName, b);

这会将文件保存到用户计算机的文档中,但 File 将包含轻松打开它的路径.如果您更喜欢用户的计算机上没有它,您可以隐藏"它.当您创建 FileInfo 时,它使用 System.IO.Path.GetTempPath() 到临时文件夹.

This will save the file into the user's computer, on the document, but the File will contain the path to open it easily. If you rather like the user don't have it on his computer you can "hide" it to a temp folder using System.IO.Path.GetTempPath() when you create the FileInfo.

希望能帮到你.

这篇关于单击链接后,如何在浏览器窗口中打开 C# 项目中包含的 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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