WPF中查看PDF文件之MoonPdfLib类库

两种PDF文件查看器,MoonPdfLib或者AdobeReader。

一、在网上扒到的很好的WPF中用MoonPdf类库来展示PDF文件。

Sourceforge上下载到MoonPdf编译好的Dll。

https://github.com/reliak/moonpdf这是源码地址

使用非常简单,废话不多说,上码上步骤。

1、程中添加对MoonPdfLib.dll的引用。注意:将libmupdf.dll放置于WPF工程Bin文件下。

2、XMAL源码:

复制代码
<Grid>
<Border Background="#f0f0f0">
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<Button Content="上传" Width="95" Click="FileButton_Click" Margin="5,5,5,375"/>
<Button Content="放大" Width="105" Click="ZoomInButton_Click" Margin="0,5,0,375"/>
<Button Content="缩小" Width="95" Click="ZoomOutButton_Click" Margin="5,5,5,375"/>
<Button Content="100%" Width="95" Click="NormalButton_Click" Margin="0,5,0,375"/>
<Button Content="整页" Width="100" Click="FitToHeightButton_Click" Margin="5,5,5,375"/>
<Button Content="单页" Width="90" Click="SinglePageButton_Click" Margin="0,5,0,375"/>
<Button Content="双页" Width="90" Click="FacingButton_Click" Margin="5,5,5,375"/>
</StackPanel>
</Border>
<Grid HorizontalAlignment="Left" Height="340" Margin="10,70,0,0" VerticalAlignment="Top" Width="155">
<TreeView x:Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Childern}">
<TextBlock Name="text1">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}">
<Binding x:Name="name1" Path="Name" />
<Binding x:Name="name2" Path="FilePath" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
<Border Background="#d3d3d3" Margin="170,70,0,0">
<mpp:MoonPdfPanel x:Name="moonPdfPanel" Background="LightGray" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True"/>
</Border>
</Grid>

3、C#代码:

复制代码
private bool _isLoaded = false;
        public MainWindow()
        {
            InitializeComponent();

            //数据库要存放的路径
            string filePath = "\\File\\ExceptionHandlingVSTO.pdf";
            //获取文件名字
            string fileName = Path.GetFileName(filePath);

            TreeModel loc = new TreeModel()
            {
                Name = "施工前",
                Childern = new List<TreeModel>()
                {
                    new TreeModel()
                    {
                        Name = fileName,
                        FilePath = filePath
                    },
                    new TreeModel()
                    {
                        Name ="测试节点可以无视",
                        FilePath = filePath
                    }
                }
            };
            List<TreeModel> list = new List<TreeModel>() { loc };
            treeView.ItemsSource = list;
            
        }
        private void FileButton_Click(object sender, RoutedEventArgs e)
        {
            form.OpenFileDialog dialog = new form.OpenFileDialog();
            dialog.Multiselect = true;
            if (dialog.ShowDialog() == form.DialogResult.OK)
            {
                string[] filesPath = dialog.FileNames;

                if (filesPath != null && filesPath.Length > 0)
                {
                    foreach (var filePath in filesPath)
                    {
                        Upload(filePath);
                    }
                }
            }
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="filePath">文件原来的路径</param>
        public void Upload(string filePath)
        {
            try
            {
                FileInfo info = new FileInfo(filePath);

                string directory = Directory.GetCurrentDirectory();
                string uploadFile = $"{directory}\\File";
                //文件要上传的路径
                string uploadPath = $"{uploadFile}\\{info.Name}";

                WebClient wc = new WebClient();
                //wc.Credentials = new NetworkCredential("xuyue", "111111");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                byte[] result = br.ReadBytes(Convert.ToInt32(fs.Length));
                Stream s = wc.OpenWrite(new Uri(uploadPath), "PUT");

                if (s.CanWrite)
                {
                    s.Write(result, 0, result.Length);
                }
                s.Flush();
                s.Close();
                fs.Close();




            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void ZoomInButton_Click(object sender, RoutedEventArgs e)
        {
            if (_isLoaded)
            {
                moonPdfPanel.ZoomIn();
            }
        }

        private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
        {
            if (_isLoaded)
            {
                moonPdfPanel.ZoomOut();
            }
        }

        private void NormalButton_Click(object sender, RoutedEventArgs e)
        {
            if (_isLoaded)
            {
                moonPdfPanel.Zoom(1.0);
            }
        }

        private void FitToHeightButton_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.ZoomToHeight();
        }

        private void FacingButton_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.ViewType = MoonPdfLib.ViewType.Facing;
        }

        private void SinglePageButton_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.ViewType = MoonPdfLib.ViewType.SinglePage;
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            //数据库要存放的路径
            string fileName = "\\File\\ExceptionHandlingVSTO.pdf";
            string filePath = $"{Directory.GetCurrentDirectory()}{fileName}";

            try
            {
                moonPdfPanel.OpenFile(filePath);
                _isLoaded = true;
            }
            catch (Exception ex)
            {
                _isLoaded = false;
            }
        }
    }
相关推荐
南风微微吹11 小时前
2015-2025年英语六级历年真题及答案解析电子版PDF(含听力音频)
pdf·英语六级
小为资料库14 小时前
2026年初级社会工作者考试历年真题及答案解析电子版pdf(2010-2025年)
pdf
AI玫瑰助手15 小时前
PDF 转 Markdown 主力方案怎么选:PaddleOCR-VL-1.5、MinerU、HunyuanOCR 与 MonkeyOCR 实测对比
pdf·大模型·知识库·表格·md·文字识别·paddleocr
其实秋天的枫15 小时前
【2026年5月】初级会计师考试真题试卷及答案PDF
经验分享·pdf
小为资料库17 小时前
英语六级听力历年真题汇总电子版PDF(含听力音频mp3、答案解析及听力原文)
pdf·音视频
usdoc文档预览17 小时前
国产化踩坑:Vue3 / React / 小程序如何免插件实现 OFD 及复杂 Office 文档同屏预览
前端·javascript·react.js·小程序·pdf·word·office文件在线预览
小婷资料库17 小时前
河北省中考历年真题及答案解析9科全电子版PDF(2008-2025年)
pdf
优化控制仿真模型18 小时前
【2026年5月最新】初级会计师考试真题试卷及答案PDF
经验分享·pdf
小为资料库18 小时前
英语四级听力历年真题汇总电子版PDF(含听力音频mp3、答案解析及听力原文)
pdf·音视频
小短腿的代码世界18 小时前
KDReports源码深度解析:Qt报表引擎如何做到“所见即所得“?从模板引擎到PDF导出的完整渲染管线揭秘
网络·qt·pdf