利用WPF+XPS完成套打任務

step 1:xps模板建立

爲完成套打任務,首先利用Word建立如下文檔,並保存爲xps模板
在這裏插入圖片描述

step 2 建立項目

建立 WpfPrintDemo 項目,並將如下代碼複製爲新建類

建立 XPSHelper類

using System;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Xps.Packaging;

namespace WpfPrintDemo
{
    public class XPSHelper
    {
        DocumentViewer docView;
        string xpsFile;
        public XPSHelper(string file, DocumentViewer documentViewer)
        {
            docView = documentViewer;
            xpsFile = file;
        }
        public NameValueCollection NameValue { get; set; }

        public void SetPrint()
        {

            XpsDocument xpsDoc = new XpsDocument(xpsFile, FileAccess.Read);
            FixedDocumentSequence fds = xpsDoc.GetFixedDocumentSequence();
            foreach (DocumentReference DocRef in fds.References)
            {
                bool bForceReload = false;
                FixedDocument DocFd = DocRef.GetDocument(bForceReload);
                foreach (PageContent DocFpPc in DocFd.Pages)
                {
                    FixedPage DocFp = DocFpPc.GetPageRoot(bForceReload);
                    Search(DocFp);
                    ((IAddChild)DocFpPc).AddChild(DocFp);
                }
            }
            docView.Document = fds;
            xpsDoc.Close();
        }
        private void Search(FixedPage fixedPage)
        {
            var elements = fixedPage.Children;
            Canvas containCanvas = new Canvas
            {
                Width = fixedPage.Width,
                Height = fixedPage.Height,
                Background = Brushes.Transparent          
            };         
            for (int i = 0; i < elements.Count; i++)
            {
                UIElement element = elements[i];
                if (element is Glyphs)
                {
                    Glyphs gps = (Glyphs)element;
                    string strMark = gps.UnicodeString;

                    strMark = strMark.TrimEnd(new char[] { ':', ':' });
                    if (NameValue.AllKeys.Contains(strMark))//判定當前數據是否爲標籤
                    {
                        TextBlock label = ModiGlyphs(gps, NameValue.Get(strMark));
                        containCanvas.Children.Add(label);
                    }
                }
                else if (element is Canvas)
                {
                    Canvas canvas = element as Canvas;
                    SearchSubItem(canvas);
                }
            }
            fixedPage.Children.Add(containCanvas);//將畫布添加到頁面上
        }

        private void SearchSubItem(Canvas canvas)
        {
            Canvas containCanvas = new Canvas
            {
                Width = canvas.Width,
                Height = canvas.Height,
                Background = Brushes.Transparent         
            };         
            var collection = canvas.Children;

            for (int i = 0; i < canvas.Children.Count; i++)
            {
                var element = canvas.Children[i];
                if (element is Glyphs)
                {
                    Glyphs gps = (Glyphs)element;
                    string strMark = gps.UnicodeString;
                    //System.Diagnostics.Debug.Print(strMark);//觀察用
                    strMark = strMark.TrimEnd(new char[] { ':', ':' });

                    if (NameValue.AllKeys.Contains(strMark))      //判定當前數據是否爲標籤
                    {
                        TextBlock label = ModiGlyphs(gps, NameValue.Get(strMark));
                        containCanvas.Children.Add(label);
                        //canvas.Children.RemoveAt(i);             //移除標籤
                    }

                    canvas.Children.Add(containCanvas);
                }
                else if (element is Canvas)
                {
                    Canvas canvas1 = element as Canvas;
                    SearchSubItem(canvas1);
                }
            }
        }

        private TextBlock ModiGlyphs(Glyphs gps, string content)
        {
            double x = gps.OriginX;
            double y = gps.OriginY;
            double fontSize = gps.FontRenderingEmSize;
            TextBlock label = new TextBlock();

            string strMark = gps.UnicodeString.Trim();
            if (strMark.Last() == ':' || strMark.Last() == ':')
            {
                Canvas.SetLeft(label, x + strMark.Trim().Length * fontSize);
            }
            else
            {
                Canvas.SetLeft(label, x + strMark.Trim().Length * fontSize + fontSize);
            }
            Canvas.SetTop(label, y - fontSize);
            Canvas.SetZIndex(label, 99);
            label.Foreground = Brushes.Red;
            label.FontFamily = new System.Windows.Media.FontFamily("宋體");
            label.FontSize = fontSize;
            label.Text = content;
            return label;
        }

    }
}

新建Print窗口

在窗口中添加DocumentViewer和Button

<Button Grid.Row="1" Height="30" Padding="5" Margin="3" Click="Button_Click">打印</Button>
<DocumentViewer Name="docView"/>

編寫後臺代碼

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            string xpsFile = "出庫單模板.xps";//讀取模板
            XPSHelper helper = new XPSHelper(xpsFile, docView)
            {
                NameValue = new NameValueCollection {
                {"訂貨日期","2020-01-10" },
                {"發貨日期","2020-03-31" },
                {"市場編號","1000010001" },
                {"市場名稱","三里屯" },
                {"公司訂貨熱線","110120130" },
                {"公司訂貨傳真","75757575" },
                {"客戶名稱","有錢的" },
                {"人","發貨的" },  //發貨的
                {"送貨電話","9898998" },
                {"客戶地址"," 北五環之外" },

            }
            };
            helper.SetPrint();
        }

Step 3:觀察打印效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章