C# WPF发票打印
C# WPF發(fā)票打印
內(nèi)容目錄
實(shí)現(xiàn)效果
業(yè)務(wù)場(chǎng)景
編碼實(shí)現(xiàn)
本文參考
源碼下載
1.實(shí)現(xiàn)效果
發(fā)票界面
PDF打印結(jié)果
2.業(yè)務(wù)場(chǎng)景
界面作為發(fā)票預(yù)覽,按客戶需求可打印成發(fā)票紙張給客戶。
3.編碼實(shí)現(xiàn)
3.1 添加Nuget庫(kù)
站長(zhǎng)使用 .Net Core 3.1 創(chuàng)建的WPF工程,創(chuàng)建“Invoice”解決方案后,需要添加兩個(gè)Nuget庫(kù):MaterialDesignThemes和MaterialDesignColors,上圖的效果是使用該控件庫(kù)實(shí)現(xiàn)的,非常強(qiáng)大。
3.2 工程結(jié)構(gòu)
不需要截圖,只修改了兩個(gè)文件,App.xaml添加MD控件樣式,MainWindow主窗口實(shí)現(xiàn)效果。
3.3 App.xaml引入MD控件樣式
<Application x:Class="Invoice.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:Invoice"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /></ResourceDictionary.MergedDictionaries><!--PRIMARY--><SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#349fda"/><SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#333333"/><SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#FF62013C"/><SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFFFFF"/><SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#FF46052C"/><SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/><!--ACCENT--><SolidColorBrush x:Key="SecondaryAccentBrush" Color="#FFFBE188"/><SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="#FFFFFF"/></ResourceDictionary></Application.Resources> </Application>3.4 主窗體 MainWindow.xaml
整體布局,看上圖加上下面的界面代碼,本文基本就是布局 + 打印操作,全在這個(gè)界面,直接看代碼吧,不細(xì)說了:
<Window x:Class="Invoice.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:Invoice"mc:Ignorable="d"xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"Title="發(fā)票" Height="600" Width="535.5"WindowStartupLocation="CenterScreen" ResizeMode="NoResize"WindowStyle="None"FontFamily="Roboto"><ScrollViewer><Grid><Button HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20" Click="Button_Click"><materialDesign:PackIcon Kind="Printer"/></Button><Grid x:Name="print"><Grid.RowDefinitions><RowDefinition Height="120"/><RowDefinition Height="400"/><RowDefinition Height="200"/></Grid.RowDefinitions><StackPanel Orientation="Horizontal" Margin="30" Grid.Row="0"><Image Source="https://img.dotnet9.com/logo.png" Width="100" Height="100" VerticalAlignment="Center"/><TextBlock Text="發(fā)票" VerticalAlignment="Center" FontSize="35" Foreground="{StaticResource SecondaryAccentBrush}" FontWeight="Bold"/></StackPanel><StackPanel Grid.Row="1" HorizontalAlignment="Stretch" Margin="20"><Grid HorizontalAlignment="Stretch"><Grid.ColumnDefinitions><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><StackPanel Grid.Column="0" Margin="10"><TextBlock Text="客戶名稱" FontSize="20" Margin="5"/><StackPanel Orientation="Horizontal"><TextBlock Text="簽發(fā)日期:" FontSize="12" Margin="5"/><TextBlock Text="7月19號(hào)" FontSize="12" Margin="5" FontWeight="DemiBold"/></StackPanel><StackPanel Orientation="Horizontal"><TextBlock Text="發(fā)票編號(hào):" FontSize="12" Margin="5"/><TextBlock Text="0001" FontSize="12" Margin="5" FontWeight="DemiBold"/></StackPanel></StackPanel><StackPanel Grid.Column="1" Margin="10"><TextBlock Text="你的名稱" FontSize="12" Margin="5" HorizontalAlignment="Right"/><TextBlock Text="你的地址" FontSize="12" Margin="5" HorizontalAlignment="Right"/><TextBlock Text="鎮(zhèn),城市" FontSize="12" Margin="5" HorizontalAlignment="Right"/><TextBlock Text="郵編" FontSize="12" Margin="5" HorizontalAlignment="Right"/></StackPanel></Grid><Grid Margin="10 30 10 0"><Grid.ColumnDefinitions><ColumnDefinition Width="3*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Text="描述" FontSize="12" FontWeight="ExtraLight"/><TextBlock Grid.Column="1" Text="費(fèi)用" FontSize="12" FontWeight="ExtraLight" HorizontalAlignment="Center"/><TextBlock Grid.Column="2" Text="小時(shí)" FontSize="12" FontWeight="ExtraLight" HorizontalAlignment="Center"/><TextBlock Grid.Column="3" Text="小計(jì)" FontSize="12" FontWeight="ExtraLight" HorizontalAlignment="Right"/></Grid><Rectangle Fill="Black" Height="1" Margin="5 2" Opacity="0.5"/><ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled"><ListViewItem><Grid Width="460"><Grid.ColumnDefinitions><ColumnDefinition Width="3*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Text="網(wǎng)站設(shè)計(jì)"/><TextBlock Grid.Column="1" Text="¥ 45.00" HorizontalAlignment="Center"/><TextBlock Grid.Column="2" Text="10" HorizontalAlignment="Center"/><TextBlock Grid.Column="3" Text="¥ 450.00" Foreground="{StaticResource PrimaryHueMidBrush}"/></Grid></ListViewItem><ListViewItem><Grid Width="460"><Grid.ColumnDefinitions><ColumnDefinition Width="3*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Text="Logo設(shè)計(jì)"/><TextBlock Grid.Column="1" Text="¥ 30.00" HorizontalAlignment="Center"/><TextBlock Grid.Column="2" Text="20" HorizontalAlignment="Center"/><TextBlock Grid.Column="3" Text="¥ 600.00" Foreground="{StaticResource PrimaryHueMidBrush}"/></Grid></ListViewItem><ListViewItem><Grid Width="460"><Grid.ColumnDefinitions><ColumnDefinition Width="3*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Text="背景設(shè)計(jì)"/><TextBlock Grid.Column="1" Text="¥ 40.00" HorizontalAlignment="Center"/><TextBlock Grid.Column="2" Text="12" HorizontalAlignment="Center"/><TextBlock Grid.Column="3" Text="¥ 480.00" Foreground="{StaticResource PrimaryHueMidBrush}"/></Grid></ListViewItem></ListView></StackPanel><StackPanel Grid.Row="2" Margin="20"><Grid Margin="10 20 10 0"><Grid.ColumnDefinitions><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Text="銀行信息" FontSize="12" FontWeight="ExtraLight"/><TextBlock Grid.Column="1" Text="應(yīng)繳款" FontSize="12" FontWeight="ExtraLight"/><TextBlock Grid.Column="2" Text="總應(yīng)繳款" FontSize="12" FontWeight="ExtraLight" HorizontalAlignment="Right"/></Grid><Rectangle Fill="Black" Height="1" Margin="5 2" Opacity="0.5"/><Grid Margin="10 20 10 0"><Grid.ColumnDefinitions><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><StackPanel Grid.Column="0"><StackPanel Orientation="Horizontal"><TextBlock Text="賬號(hào)編號(hào):" FontSize="10" Margin="5"/><TextBlock Text="123 456 789" FontSize="10" Margin="5" FontWeight="DemiBold"/></StackPanel><StackPanel Orientation="Horizontal"><TextBlock Text="排序編號(hào):" FontSize="10" Margin="5"/><TextBlock Text="01 23 45" FontSize="10" Margin="5" FontWeight="DemiBold"/></StackPanel></StackPanel><TextBlock Text="7月19號(hào)" Grid.Column="1" FontSize="25" Margin="5"/><TextBlock Grid.Column="2" Text="¥ 1,590.00" FontSize="25" Margin="5" HorizontalAlignment="Right" Foreground="{StaticResource PrimaryHueMidBrush}" FontWeight="Bold"/></Grid><Rectangle Fill="Black" Height="1" Margin="5 2" Opacity="0.5"/><Grid Margin="0 20" HorizontalAlignment="Stretch"><StackPanel><materialDesign:PackIcon Margin="5" VerticalAlignment="Center" Kind="Heart" Foreground="{StaticResource PrimaryHueMidBrush}" Width="20" Height="20"/><TextBlock Text="謝謝!" VerticalAlignment="Center" Margin="5" FontSize="20" FontWeight="ExtraBold"/></StackPanel><StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom"><TextBlock Text="632871194@qq.com" FontSize="8" Margin="5" FontWeight="Light"/><TextBlock Text="99 999999-9999999" FontSize="8" Margin="5" FontWeight="Light"/><TextBlock Text="https://dotnet9.com" FontSize="8" Margin="5" FontWeight="Light"/></StackPanel></Grid></StackPanel></Grid></Grid></ScrollViewer> </Window>后臺(tái)發(fā)票打印操作
private void Button_Click(object sender, RoutedEventArgs e) {try{this.IsEnabled = false;PrintDialog printDialog = new PrintDialog();if (printDialog.ShowDialog() == true){printDialog.PrintVisual(print, "發(fā)票");}}finally{this.IsEnabled = true;} }4.本文參考
Design com WPF大神的學(xué)習(xí)視頻::Invoice Screen and Print to PDF
開源控件庫(kù):MaterialDesignInXamlToolkit
本站對(duì)MD開源控件庫(kù)的介紹:控件介紹
5.代碼下載</>
文章中代碼已經(jīng)全部貼出,添加Nuget包,復(fù)制文中代碼就可以運(yùn)行了。
除非注明,文章均由?Dotnet9?整理發(fā)布,歡迎轉(zhuǎn)載。
轉(zhuǎn)載請(qǐng)注明本文地址:https://dotnet9.com/6798.html
歡迎掃描下方二維碼關(guān)注?Dotnet9?的微信公眾號(hào),本站會(huì)及時(shí)推送最新技術(shù)文章(微信公眾號(hào)“dotnet9_com”):
總結(jié)
以上是生活随笔為你收集整理的C# WPF发票打印的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020年你应该学习 .Net Core
- 下一篇: SummerBoot,将SpringBo