[WPF 学习] 7.2 模板打印
程序员文章站
2022-06-22 09:35:36
利用FlowDocument进行模板打印 xaml 代码 用String.Replace进行替换,不知道也没有其他办法? ......
利用flowdocument进行模板打印
xaml
<dockpanel > <button content="preview" margin="15" click="button_click_1" dockpanel.dock="bottom" height="76"/> <flowdocumentpageviewer verticalalignment="top" width="300" name="fd" borderbrush="blue" borderthickness="2"> <flowdocument pagepadding="38" > <paragraph textalignment="center"> <run fontfamily="宋体" fontweight="bold">酒店名称:</run> [hotelname] </paragraph> <blockuicontainer> <line x1="0" y1="0" x2="800" y2="0" stroke="black"/> </blockuicontainer> <paragraph> <run fontweight="bold">日期:</run> [date] </paragraph> </flowdocument> </flowdocumentpageviewer> </dockpanel>
代码
private void button_click_1(object sender, routedeventargs e) { var filename = @"test.xaml"; using (filestream fs = new filestream(filename, filemode.openorcreate)) { xamlwriter.save(fd.document, fs); } var sts = file.readalltext(filename); sts = sts.replace("[hotelname]", "naradahotel") .replace("[date]", datetime.now.tolongdatestring()); var d = ((idocumentpaginatorsource)xamlreader.parse(sts)).documentpaginator; printdialog pd = new printdialog(); pd.printdocument(d, "test"); }
用string.replace进行替换,不知道也没有其他办法?