Wpf 单据分页套打、自定义纸张大小 完美解决

本文介绍了如何使用WPF解决包含emoji表情的单据分页套打问题,包括创建WPF项目、编写打印代码、生成DLL供第三方语言调用,并详细说明了设置自定义纸张大小的步骤,最终实现完美打印效果。
摘要由CSDN通过智能技术生成

扯淡

打印功能其实也不简单,开始使用的是Winform打印,主要是打印公司的订单,但是有个问题用户在备注的时候会加入emoji表情,比如 😔😪🤤😴 等等,因为套打需要定位每一个内容的位置,Winform使用的是Graphics对象的DrawString方法,但是遇到emoji表情确不支持,没办法困扰了很长时间,后来测试wpf的打印功能支持emoji表情,我对wpf不熟悉,只是简单的实现打印,对分页,位置控制等等完全搞不定,找了好长时间的资料都没有搞定,最后终于解决了,到最后发现真的太简单了,根本都不需要有多么高深的技术,只要方向对了,小白都能搞定专业的事情。请继续往下看。

我以为打印预览什么的正常了就可以了,实际上打印机测试 才发现,效果都不忍直视,不过好在花了几天时间,最终完美解决。

思路 

创建Wpf项目 -> 编写打印代码  -> 生成 Dll文件  -> 提供给第三方语言调用(我最终是提供给JS调用打印的,项目是基于WEB端)

项目

 1.创建一个WPF的项目

2.创建PrintTools.cs

其实可以使用 xaml 作为打印模板,但是我对xaml布局不太熟悉,用xaml还要设计到数据绑定,调用等等感觉非常麻烦也很难,所以就直接使用代码生成了。 

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfPrint
{
    /// <summary>
    /// 打印工具类(注意要用public修饰)
    /// </summary>
    public class PrintTools
    {

        /// <summary>
        /// 对外暴露的方法
        /// </summary>
        /// <param name="preview">true:打印预览  false:直接打印</param>
        public static void Print(bool preview = false)
        {
            if (preview)
            {

                var viewer = new DocumentViewer
                {
                    Document = CreateDocument()
                };
                Window wnd = new Window
                {
                    Content = viewer,
                    Title = "打印预览"
                };
                wnd.ShowDialog();
            }
            else
            {
                PrintDialog printDialog = new PrintDialog();
                printDialog.PrintDocument(((IDocumentPaginatorSource)CreateDocument()).DocumentPaginator, "Wpf打印");

            }
        }

        private static SolidColorBrush brush = new SolidColorBrush
        {
            Color = Color.FromArgb(126, 56, 86, 0)
        };

        /// <summary>
        /// 创建文档
        /// </summary>
        /// <returns></returns>
        private static FixedDocument CreateDocument()
        {
            FixedDocument fd = new FixedDocument();

            FixedPage fp = new FixedPage();
            PageContent pc = new PageContent();


            fp.Width = 200;
            fp.Height = 100;

            TextBlock tb = new TextBlock();

            //add some text to a TextBox object
            tb.Text = "This is some test text🤤😴";
            //add the text box to the FixedPage
            fp.Children.Add(tb);
            //add the FixedPage to the PageContent 
            pc.Child = fp;
            //add the PageContent to the FixedDocument
            fd.Pages.Add(pc);

            FixedPage fp2
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值