最近研究了兩種PDF文件查看器,MoonPdfLib或者AdobeReader。git
今天先說第一種,在網上扒到的很好的WPF中用MoonPdf類庫來展現PDF文件。github
在Sourceforge上下載到MoonPdf編譯好的Dll。數據庫
https://github.com/reliak/moonpdf這是源碼地址。ide
使用很是簡單,廢話很少說,上碼上步驟。測試
一、程中添加對MoonPdfLib.dll的引用。注意:將libmupdf.dll放置於WPF工程Bin文件下。spa
二、XMAL源碼:.net
<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>
三、C#代碼:3d
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; } } }