WPF:WPF顯示PDF文檔-創(chuàng)新互聯(lián)

簡述

軟件的幫助文檔可借助第三方軟件如PDF Reader、Adobe PDF等顯示,但客戶機上需安裝此類軟件。WPF開發(fā)的軟件可借助第三方庫 MoonPdf 將PDF文檔加載顯示到軟件窗口中(Dll下載地址,GitHub源碼地址)。

十載的克井網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整克井建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“克井網(wǎng)站設(shè)計”,“克井網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

MoonPdf庫使用方式:

  1. 將MoonPdfLib.dll、libmupdf.dll、MouseKeyboardActivityMonitor.dll放置于WPF工程Bin文件下;
  2. 在項目工程中“引用”右鍵添加新引用:MoonPdfLib.dll;
  3. 在.xmal及.cs文件中添加相關(guān)引用,詳見代碼。

代碼

PDFViewer.xaml

<UserControl x:Class="NovelRPS.PdfViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
             mc:Ignorable="d" 
             >
    <Grid>
        <!-- ContextMenu -->
        <Grid.ContextMenu>
            <ContextMenu>
                <MenuItem x:Name="ZoomToOrigin" Header="100%" Click="ZoomToOrigin_Click"></MenuItem>
                <MenuItem x:Name="SinglePage" Header="單頁" Click="SinglePage_Click"></MenuItem>
                <MenuItem x:Name="DoublePage" Header="雙頁" Click="DoublePage_Click"></MenuItem>
            </ContextMenu>
        </Grid.ContextMenu>

        <Border>
            <!-- 參考:https://www.cnblogs.com/yang-fei/p/4885570.html -->
            <mpp:MoonPdfPanel x:Name="pdfViewer" MaxZoomFactor="2.5" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True"></mpp:MoonPdfPanel>
        </Border>
    </Grid>
</UserControl>

PDFViewer.cs

using MoonPdfLib; //記得引用此命名空間

public partial class PdfViewer : UserControl
    {
        private bool m_bLoaded = false;

        public PdfViewer()
        {
            InitializeComponent();
        }

        public bool LoadPdfDoc( string strPdfPath )
        {
            try
            {
                this.pdfViewer.OpenFile( strPdfPath );
                this.pdfViewer.Zoom( 1.0 );
                m_bLoaded = true;
            }
            catch
            {
                m_bLoaded = false;
            }

            return m_bLoaded;
        }

        private void ZoomToOrigin_Click( object sender, RoutedEventArgs e )
        {
            if ( !m_bLoaded )
                return;
            this.pdfViewer.Zoom( 1.0 );
        }

        private void SinglePage_Click( object sender, RoutedEventArgs e )
        {
            if ( !m_bLoaded )
                return;
            this.pdfViewer.ViewType = ViewType.SinglePage;
        }

        private void DoublePage_Click( object sender, RoutedEventArgs e )
        {
            if ( !m_bLoaded )
                return;
            this.pdfViewer.ViewType = ViewType.Facing;
        }
    }

調(diào)用PdfViewer,及HelpWin窗口類:HelpWin.xaml

<Window x:Class="NovelRPS.HelpWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:NovelRPS"
        Title="HelpWin" Background="#FF212121" WindowState="Maximized" Topmost="True" WindowStyle="None" ResizeMode="NoResize">    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="66"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <!-- Title -->
        <Label x:Name="Title" Grid.Row="0" Background="#FF414141" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Padding="0">
            <DockPanel>
                <Image DockPanel.Dock="Left" Source="Images/SubPageLogo.png"></Image>
                <Image x:Name="ImgClose" DockPanel.Dock="Right" Source="Images/Close.png" MouseDown="ImgClose_MouseDown"></Image>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Image Source="Images/Setting/Help.png" Width="30"></Image>
                    <Label Content="幫助" FontSize="26"  FontWeight="Bold"></Label>
                </StackPanel>
            </DockPanel>
        </Label>

        <!-- Content -->
        <Label Grid.Row="1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <local:PdfViewer x:Name="PDFView" Loaded="PDFView_Loaded"/>
        </Label>

    </Grid>
</Window>

HelpWin.cs

public partial class HelpWin : Window
    {
        private string m_strPdfPath = AppDomain.CurrentDomain.BaseDirectory + "help.pdf"; 

        public HelpWin()
        {
            InitializeComponent();
        }

        private void ImgClose_MouseDown( object sender, MouseButtonEventArgs e )
        {
            this.Close();
        }

        private void PDFView_Loaded( object sender, RoutedEventArgs e )
        {
            if ( !this.PDFView.LoadPdfDoc( m_strPdfPath ) )
            {
                MessageBox.Show( "打開幫助文檔失敗,請重試!" );
            }
        }
    }

效果

WPF:WPF顯示PDF文檔

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

標(biāo)題名稱:WPF:WPF顯示PDF文檔-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://muchs.cn/article48/dsgjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站建站公司、網(wǎng)站設(shè)計公司、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)