本文主要是介绍利用WPF+XPS完成套打任务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
利用WPF+XPS完成套打任务
- step 1:xps模板建立
- step 2 建立项目
- 建立 XPSHelper类
- 新建Print窗口
- 编写后台代码
- Step 3:观察打印效果
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:观察打印效果
这篇关于利用WPF+XPS完成套打任务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!