微信公眾號:Dotnet9,網站:Dotnet9。問題或建議,請網站留言; 如果您覺得Dotnet9對您有幫助,歡迎贊賞 C WPF發票列印 內容目錄 1. 實現效果 2. 業務場景 3. 編碼實現 4. 本文參考 5. 源碼下載 1.實現效果 發票界面 PDF列印結果 2.業務場景 界面作為發票預 ...
微信公眾號:Dotnet9,網站:Dotnet9。問題或建議,請網站留言;
如果您覺得Dotnet9對您有幫助,歡迎贊賞
C# WPF發票列印
內容目錄
1.實現效果
發票界面
PDF列印結果
2.業務場景
界面作為發票預覽,按客戶需求可列印成發票紙張給客戶。
3.編碼實現
3.1 添加Nuget庫
站長使用 .Net Core 3.1 創建的WPF工程,創建“Invoice”解決方案後,需要添加兩個Nuget庫:MaterialDesignThemes和MaterialDesignColors,上圖的效果是使用該控制項庫實現的,非常強大。
3.2 工程結構
不需要截圖,只修改了兩個文件,App.xaml添加MD控制項樣式,MainWindow主視窗實現效果。
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
整體佈局,看上圖加上下麵的界面代碼,本文基本就是佈局 + 列印操作,全在這個界面,直接看代碼吧,不細說了:
<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="發票" 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="發票" 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="簽發日期:" FontSize="12" Margin="5"/>
<TextBlock Text="7月19號" FontSize="12" Margin="5" FontWeight="DemiBold"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="發票編號:" 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="鎮,城市" 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="費用" FontSize="12" FontWeight="ExtraLight" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="2" Text="小時" FontSize="12" FontWeight="ExtraLight" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="3" Text="小計" 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="網站設計"/>
<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設計"/>
<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="背景設計"/>
<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="應繳款" FontSize="12" FontWeight="ExtraLight"/>
<TextBlock Grid.Column="2" Text="總應繳款" 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="賬號編號:" FontSize="10" Margin="5"/>
<TextBlock Text="123 456 789" FontSize="10" Margin="5" FontWeight="DemiBold"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="排序編號:" FontSize="10" Margin="5"/>
<TextBlock Text="01 23 45" FontSize="10" Margin="5" FontWeight="DemiBold"/>
</StackPanel>
</StackPanel>
<TextBlock Text="7月19號" 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="[email protected]" 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>
後臺發票列印操作
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
this.IsEnabled = false;
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintVisual(print, "發票");
}
}
finally
{
this.IsEnabled = true;
}
}
4.本文參考
Design com WPF大神的學習視頻::Invoice Screen and Print to PDF
開源控制項庫:MaterialDesignInXamlToolkit
本站對MD開源控制項庫的介紹:控制項介紹
5.代碼下載</>
文章中代碼已經全部貼出,添加Nuget包,複製文中代碼就可以運行了。
除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載
轉載請註明本文地址:https://dotnet9.com/6777.html
歡迎掃描下方二維碼關註 Dotnet9 的微信公眾號,本站會及時推送最新技術文章