1. 程式人生 > >WPF中檢視PDF檔案-

WPF中檢視PDF檔案-

       最近研究了兩種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>
View Code

 

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;
            }
        }
    }
View Code