gpt4 book ai didi

c# - 使用模板打印

转载 作者:太空狗 更新时间:2023-10-29 21:17:57 24 4
gpt4 key购买 nike

对于一个简单的收据系统,我需要以某种方式定义一个具有简单格式的模板文档,用数据填充它并在标准的 Windows 打印机上打印它。它必须在 Windows 服务上工作。我应该最好使用什么技术?

编辑:

我尝试使用 PDF 表单。我定义了几个文本框并用 iTextSharp 填充它们。它一直有效,直到我不得不打印它们,这真的很难,因为您实际上必须直接使用阅读器可执行文件。

似乎更好地集成到 .NET 中的替代方法似乎是使用 XPS。 XPS 是否提供类似的功能?

最佳答案

使用 html 或纯文本创建您自己的收据模板。

使用 html 的示例:

HTML

<html>
<head>
<title>Receipt</title>
</head>
<body>
<div>The price: <%Price%></div>
<div>The time: <%Time%></div>
<div>Payment Method: <%PaymentMethod%></div>
</body>
</html>

C#

    static void Main(string[] args)
{
CreateReceipt("€1.50", "09.30", "Cash");
}

private static void CreateReceipt(string price, string time, string paymentMethod)
{
string bodyFile;
string template = System.IO.Directory.GetCurrentDirectory() + "\\template.html";
using (StreamReader reader = new StreamReader(template))
{
bodyFile = reader.ReadToEnd();
bodyFile = bodyFile.Replace("<%Price%>", price);
bodyFile = bodyFile.Replace("<%Time%>", time);
bodyFile = bodyFile.Replace("<%PaymentMethod%>", paymentMethod);
}
FileStream fs = File.OpenWrite(System.IO.Directory.GetCurrentDirectory() + "\\receipt.html");
StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
writer.Write(bodyFile);
writer.Close();
}
}

关于c# - 使用模板打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17789418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com