C# WPF发票打印

微信公众号:Dotnet9,网站:Dotnet9。问题或建议,请网站留言;
如果您觉得Dotnet9对您有帮助,欢迎赞赏

C# WPF发票打印

内容目录

  1. 实现效果
  2. 业务场景
  3. 编码实现
  4. 本文参考
  5. 源码下载

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="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>

后台发票打印操作

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 整理发布,欢迎转载。

转载请注明:
作者:Dotnet9
链接:https://dotnet9.com/6798.html
来源:Dotnet9
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

登录后才能评论