C# WPF发票打印

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

转载请注明本文地址:https://dotnet9.com/6798.html

欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章(微信公众号“dotnet9_com”):

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/312480.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

对全局变量,static静态变量的理解

如果所有的变量按照下面的程序进行定义和声明&#xff0c;那么在main()函数中所有可用的变量为 &#xff08;&#xff09;。 void fun(int x) {static int y;……return; } int z; void main( ) {int a,b;fun(a);…… }A.x,y B.x,y,z C.a,b,z D.a,b,x,y,z static静态变量的值在…

dotNET知音,19年归档

2019年下半年开通公众号&#xff0c;尝试着分享和技术交流&#xff0c;也很高兴认识很多NETer同行。为了方便阅读&#xff0c;进行归档&#xff0c;如果之前有错过的文章&#xff0c;这是一个很好的补课机会。.NETCore3.0&#xff1a;《.Net Core3.0 配置Configuration》《.Net…

顺序表和链表的优缺点理解

若某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算&#xff0c;则利用哪种存储方式最节省时间&#xff1f;D A.双链表 B.单循环链表 C.带头结点的双循环链表 D.顺序表 想要存取任一指定序号的元素&#xff0c;链表实现这个功能的代价很大 本来顺序表的…

阿里如何应对亿级高并发大流量?如何保障高可用和稳定性!

作者&#xff1a;丁浪&#xff0c;目前在创业公司担任高级技术架构师。曾就职于阿里巴巴大文娱和蚂蚁金服。具有丰富的稳定性保障&#xff0c;全链路性能优化的经验。架构师社区特邀嘉宾&#xff01;阅读本文&#xff0c;你将会收获&#xff1a; 高并发、大流量场景的常见问题和…

中缀转后缀

从中序表达式 转换为 后序表达式 例题&#xff1a; 利用栈对表达式19/(8-5)*4求值的过程中&#xff0c;操作数栈的最大容量是多少&#xff1f;&#xff08;B &#xff09;。 A.3 B.4 C.5 D.2 操作数栈&#xff1a;先是/(- &#xff0c; 然后遇到)&#xff0c;则为/ &#xff0…

动手造轮子:写一个日志框架

动手造轮子&#xff1a;写一个日志框架Intro日志框架有很多&#xff0c;比如 log4net / nlog / serilog / microsoft.extensions.logging 等&#xff0c;如何在切换日志框架的时候做到不用修改代码&#xff0c;只需要切换不同的 loggingProvider 就可以了&#xff0c;最低成本的…

对循环队列的理解

循环队列存储在数组A[0…n-1]中&#xff0c;其头尾指针分别为f和r&#xff0c;头指针f总是指向队头元素&#xff0c;尾指针r总是指向队尾元素的下一个位置&#xff0c;则元素e入队时的操作为&#xff08;B &#xff09;。 A.A[r]e; rr1 B.A[r]e; r(r1)%n C.A[r]e;r(r1)%(n1) D.…

【C】@程序员,我们送给你一个成熟的Excel导入导出组件

程序员的显著特点有一天跟一位同事跟我闲聊&#xff0c;讨论起过去若干年软件行业的感受&#xff0c;他问了个问题&#xff1a;你觉得一个好的软件工程师最显著的特点是什么&#xff1f;我想了一会&#xff0c;说&#xff1a;大概是坐得住吧。某种意义上来说&#xff0c;在互联…

SummerBoot,将SpringBoot的先进理念与C#的简洁优雅合二为一

哈哈哈哈&#xff0c;大家好&#xff0c;我就是高产似母猪的三合&#xff0c;好久没写博客了&#xff0c;因为最近几个月在不断的加班&#xff0c;加班时长平均每个月120小时以上。今天是2020年的第一天&#xff0c;作为一条程序汪&#xff0c;觉得不做点啥好像对不起这个特别有…

树的度,结点,叶子结点,二叉树

设树T的度为4&#xff0c;其中度为1、2、3、4的结点个数分别为4、2、1、1。则T中有多少个叶子结点&#xff1f; A.4 B.6 C.8 D.10 一棵含有n个结点的树&#xff0c;有n-1个分支&#xff0c;即 n 14 22 31 41 1 16; 又由于 n n0 n1 n2 n3 n4 n0 8; n0 8 16&#…

C#刷遍Leetcode面试题系列连载(6):No.372 - 超级次方

点击蓝字“dotNET匠人”关注我哟加个“星标★”&#xff0c;每日 7:15&#xff0c;好文必达&#xff01;前文传送门:C# 刷遍 Leetcode 面试题系列连载&#xff08;1&#xff09; - 入门与工具简介C#刷遍Leetcode面试题系列连载&#xff08;2&#xff09;: No.38 - 报数C#刷遍Le…

堆 最大堆 最小堆

堆是特殊的队列 从堆中取出元素是依照元素的优先级大小&#xff0c;而不是元素进入队列的先后顺序 堆最常用的结构是二叉树表示&#xff0c;不特指的话&#xff0c;它是一棵完全二叉树 因为高度为h的完全二叉树有结点2(h-1) 到2h-1个&#xff0c;且结点排布及其规律&#xff…

使用 Postman 测试你的 API

使用 Postman 测试你的 APIIntro最近想对 API 做一些自动化测试&#xff0c;看了几个工具&#xff0c;最后选择了 postman&#xff0c;感觉 postman 的设计更好一些&#xff0c;我们可以在请求发送之前和请求获取到响应之后都可以自定义脚本&#xff0c;很灵活。而且 postman 的…

散列查找 散列表(哈希表)

哈希表的平均查找长度是&#xff08;&#xff09;的函数。 A.哈希表的长度 B.哈希表的装填因子 C.哈希函数 D.表中元素的多少 装填因子 关键字个数 / 表长 符号表&#xff1a;是 “名字&#xff08;Name&#xff09;–属性&#xff08;Attribute&#xff09;”对的集合 符号表…

使用 postman 给 API 写测试

使用 postman 给 API 写测试Intro上次我们简单介绍了 使用 postman 测试 API&#xff0c;这次主要来写一些测试用例以检查请求的响应是否符合我们的预期以及如何使用脚本测试使用 postman 内置的随机变量postman 内置的有一些产生随机值的变量&#xff0c;在发送请求时随机生成…

图 无向图 有向图

若无向图G &#xff08;V&#xff0c;E&#xff09;中含7个顶点&#xff0c;要保证图G在任何情况下都是连通的&#xff0c;则需要的边数最少是&#xff1a; A.6 B.15 C.16 D.21 (n-1)*(n-2)/21 6 * 5/2116 设无向图的顶点个数为N&#xff0c;则该图最多有多少条边&#xff1f;…

Excel催化剂回顾2019年产出(文章合集),展望2020年

一年结束&#xff0c;大家都对自己一年辛勤耕耘作一个总结&#xff0c;Excel催化剂也不落俗&#xff0c;也来一篇文章合集式的梳理&#xff0c;方便大家对Excel催化剂的产出有一个宏观全局的认识。不断创新中前行Excel催化剂从2018年开波以来&#xff0c;累计到如今已有114个大…

朗文3000词汇表带音标_英语零基础音标语法都不会词汇量3000 学雅思到6/6.5分 需要多久 该如何做?...

谢邀~题主好&#xff01;看来题主的零基础非常纯正。你现在测试的单词量是3000&#xff0c;只能大概展示出你的一些基础&#xff0c;但其实真正的还是要看你会使用&#xff0c;真正理解用法、语境的词汇有多少&#xff0c;而不是为了看起来有多少的那个数字。那么首先题主最好的…

选择排序 插入排序 交换排序

动图演示&#xff1a; https://www.runoob.com/w3cnote/bubble-sort.html 选择排序 简单选择排序 在未排序的序列中&#xff0c;选出最小的元素和序列的首位元素交换&#xff0c;接下来在剩下的未排序序列中&#xff0c;再选出最小元素与序列的第二位元素交换&#xff0c;以此…