微信公众号: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="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 的微信公众号,本站会及时推送最新技术文章