How do I open a PDF included in my C# project in a browser window upon link click?

前端 未结 1 1271
有刺的猬
有刺的猬 2021-01-28 06:50

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

The relevant xaml:



        
相关标签:
1条回答
  • 2021-01-28 07:09

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

    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.

    Hope it can help you.

    0 讨论(0)
提交回复
热议问题