首页 > 其他分享 >WPF display and host pdf via WebBrowser

WPF display and host pdf via WebBrowser

时间:2024-07-12 11:59:38浏览次数:5  
标签:pdfPath via Windows System host WebBrowser dialog using MainWindow

//xaml
<Window x:Class="WpfApp206.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp206"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ToolBar Grid.Row="0">
            <Button Content="Open" Click="OpenClick"/>
        </ToolBar>
        <WebBrowser Grid.Row="1" x:Name="webBrowser"/>
    </Grid>
</Window>


//cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApp206
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); 
        } 

        private void OpenClick(object sender, RoutedEventArgs e)
        { 
            OpenFileDialog dialog=new OpenFileDialog();
            dialog.Filter = "PDF Files|*.pdf|All Files|*.*";
            if(dialog.ShowDialog()==true)
            { 
                try
                {
                    string pdfPath = dialog.FileName;
                    if (File.Exists(pdfPath))
                    {
                        Uri uri = new Uri(pdfPath, UriKind.RelativeOrAbsolute);
                        webBrowser.Navigate(uri);
                    }
                }
                catch (Exception ex)
                { 
                    MessageBox.Show(ex.Message);
                } 
            }
        }
    }
}

 

 

标签:pdfPath,via,Windows,System,host,WebBrowser,dialog,using,MainWindow
From: https://www.cnblogs.com/Fred1987/p/18298037

相关文章

  • WPF WebBrowser make sure the path or Internet address is correct
      Onepossiblecauseisincludechinesecharacters, //WrongcodeprivatevoidOpenClick(objectsender,RoutedEventArgse){OpenFileDialogdialog=newOpenFileDialog();dialog.Filter="PDFFiles|*.pdf|AllFiles|*.*";i......
  • 解决Github访问速度慢的问题(修改 HOSTS 文件)
    1.查询http://github.com的ip地址链接:http://github.global.ssl.fastly.net.ipaddress.com/#ipinfoIP:140.82.113.32.查询https://github.global.ssl.fastly.net的IP地址链接:https://github.com.ipaddress.com/#ipinfoIP:151.101.1.1943.修改本地hosts文件映......
  • WPF customize DelegateCommand via implementation interface System.Windows.Input.
    publicclassDelCmd:ICommand{privatereadonlyAction<Object>execute;privatereadonlyPredicate<Object>canExecute;publicDelCmd(Action<object>executeValue,Predicate<object>canExecuteValue){execut......
  • 【转】你了解 localhost 与 127.0.0.1 的区别吗?
    引言在信息技术的世界里,localhost和127.0.0.1频繁出现在各种网络及软件开发的场景之中。它们似乎指向同一个意义——那就是你的本地机器。但仔细探究之下,你会发现它们之间其实存在着一些微妙的差异。今天,我们就来深究这两者之间的区别,并揭示它们在实际应用中的重要性。基本概......
  • WPF MouseWheel MouseDown MouseUp MouseMove mapped in mvvm via behavior
    //xaml<Windowx:Class="WpfApp201.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF single instance via mutex
    usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Diagnostics.Eventing.Reader;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Threading;usingSystem.Threading.Tasks;usingS......
  • Towards Accurate and Robust Architectures via Neural Architecture Search
    基于网络架构搜索的准确性与鲁棒性结构研究论文链接:https://arxiv.org/abs/2405.05502项目链接:未开源Abstract为了保护深度神经网络免受对抗性攻击,对抗性训练因其有效性而受到越来越多的关注。然而,对抗训练的准确性和鲁棒性受到体系结构的限制,因为对抗训练通过调整隶属......
  • Bug记录|vivia主题|Hexo+GitHub搭建个人博客
    1.将本地SSH添加到远程github 中,之后关联远程或push出现以下错误:fatal:Notagitrepository(oranyoftheparentdirectories):.git解决方案:执行 gitinit。gitinit2.hexog无法成功运行,出现以下错误:TypeError:C:\Users\Maxence\Desktop\项目\MyBlog\Hexo......
  • 在SelfHost项目中获取客户端IP地址
    在SelfHost项目中,获取客户端的IP地址比OwinSelfHost项目要复杂一些,可以通过以下方法获得:base.Request.Properties["System.ServiceModel.Channels.RemoteEndpointMessageProperty"].Address创建一个SelfHost项目的大概过程:创建名称为SelfHostSample的Windows窗体应用(.NETF......
  • 在OwinSelfHost项目中获取客户端IP地址
    在OwinSelfHost项目中,获取客户端的IP地址可以通过以下方法获得:base.Request.GetOwinContext().Request.RemoteIpAddress创建一个OwinSelfHost项目的大概过程:创建名称为OwinSelfHostSample的Windows窗体应用(.NETFramework)项目;在NuGet包管理器中添加中添加Microsoft.AspNe......