我正在try 创建一个使用.NET6WPF打印票证的程序.运行该程序后,文档将出现在打印队列中,但它只打印一张空白页.我也试过使用普通打印机,情况是一样的.

下面是我的测试.

第一个测试

FixedPage fixedPage = new FixedPage();
fixedPage.Width = 320;
fixedPage.Height = 267;

Style textStyle = new Style(typeof(TextBlock));
Setter sWeight = new Setter(TextBlock.FontWeightProperty, FontWeights.Bold);
textStyle.Setters.Add(sWeight);
Setter sFontColor = new Setter(TextBlock.ForegroundProperty, System.Windows.Media.Brushes.Black);
textStyle.Setters.Add(sFontColor);
TextBlock textBlock = new TextBlock();
textBlock.Text = content;
textBlock.FontSize = fontSize;
textBlock.Style = textStyle;
textBlock.FontFamily = new System.Windows.Media.FontFamily(fontFamily);
textBlock.SetValue(Canvas.LeftProperty, (double)x);
textBlock.SetValue(Canvas.TopProperty, (double)y);
fixedPage.Children.Add(textBlock);

PageContent pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
FixedDocument fixedDocument = new FixedDocument();
fixedDocument.Pages.Add(pageContent);
PrintDialog printDialog = new PrintDialog();
printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Ticket");

第二次测试.

FixedPage fixedPage = new FixedPage();
fixedPage.Width = 320;
fixedPage.Height = 267;

Style textStyle = new Style(typeof(TextBlock));
Setter sWeight = new Setter(TextBlock.FontWeightProperty, FontWeights.Bold);
textStyle.Setters.Add(sWeight);
Setter sFontColor = new Setter(TextBlock.ForegroundProperty, System.Windows.Media.Brushes.Black);
textStyle.Setters.Add(sFontColor);
TextBlock textBlock = new TextBlock();
textBlock.Text = content;
textBlock.FontSize = fontSize;
textBlock.Style = textStyle;
textBlock.FontFamily = new System.Windows.Media.FontFamily(fontFamily);
textBlock.SetValue(Canvas.LeftProperty, (double)x);
textBlock.SetValue(Canvas.TopProperty, (double)y);
fixedPage.Children.Add(textBlock);

PrintDialog printDialog = new PrintDialog();
printDialog.PrintVisual(fixedPage, "Ticket");

在打印机上,我可以在打印队列中看到一个名为‘Ticket’的项目,但没有内容.

我不需要任何打印机 Select 对话框;我想直接打印票证.我如何修改它以实现我的目标?

推荐答案

您可以通过将LocalProntServerXpsDocumentWriter一起使用来打印没有PrintDialog的文档.

关于您的代码的一些注释:

  • 您应该考虑使用XAML来填充文档.使用C#创建样式和模板非常笨拙.您可以在XAML中定义DataTemplateContentControl.将样式添加到ContentControl.Resources字典.所有这些都可以放入一个专用的.xaml文件.DataTemplate定义文档布局,例如TextBlock元素等.您可以使用XamlReader将XAML文本转换为对象,例如将ContentControl标记转换为您可以使用的C#对象.然后将表示文档内容的数据模型分配给ContentControl.Content属性,并直接打印或将其添加到FixedDocument属性.你绝对不应该在C#中创建任何可视化树,尤其是样式和模板.

  • IAddChild is an obsolete interface. Don't use it. It could get dropped from the framework any time.
    It's also not necessary to use it as you can simply use the PageContent.Child property:

    //((IAddChild)pageContent).AddChild(fixedPage);
    pageContent.Child = fixedPage;
    
  • The positioning using the attached Canvas.Top and Canvas.Left properties is only relevant when the attaching element is the child of a Canvas. Otherwise, it doesn't have any effect on the layout.
    You also should consider to use the attached property accessors:

    //textBlock.SetValue(Canvas.LeftProperty, (double)x);
    Canvas.SetLeft(textBlock, (double)x);
    

如何在没有PrintDialog的情况下打印的示例:

private void PrintDocument(FixedDocument document)
{
  var printServer = new LocalPrintServer();

  /* 
   * Select a destination printer using one of the following methods 
   */

  PrintQueue destinationPrinter;

  // Method 1: filter the list of available printers.
  // For example:
  PrintQueueCollection availablePrinters = printServer.GetPrintQueues();
  destinationPrinter = availablePrinters.First(printQueue => printQueue.Name.Contains("PDF", StringComparison.OrdinalIgnoreCase));

  // Method 2: use the default printer
  destinationPrinter = printServer.DefaultPrintQueue;

  // Method 3: pick a particular printer by name e.g., the native PDF printer
  string printerName = "Microsoft Print to PDF";
  destinationPrinter = printServer.GetPrintQueue(printerName);

  /* 
   * Start the printing 
   */

  // Create a XpsDocumentWriter that writes to the printer queue
  XpsDocumentWriter documentWriter = PrintQueue.CreateXpsDocumentWriter(destinationPrinter);

  // Handle the 'XpsDocumentWriter.WritingCompleted` event 
  // to continue code after writing has been completed.
  // This is an event based asynchronous API.
  documentWriter.WriteAsync(document);
}

Csharp相关问答推荐

始终保留数组中的最后N个值,丢弃最老的

需要澄清C#的Clean Architecture解决方案模板的AuditableEntityInterceptor类

. NET 8控制台应用程序DI错误无法解析Microsoft. Extension. Logging. ILoggerFactory类型的服务'''

使用Audit.EntityFramework,我如何将外键的值设置为相关实体上的属性?

Azure DEVOPS找不到定制的Nuget包

Azure Redis缓存与Entra ID身份验证

Blazor服务器端的身份验证角色

将现有字段映射到EFCore中的复杂类型

Azure函数中实体框架核心的依赖注入

为什么Regex.IsMatch(";\\t";,";\\t";)返回FALSE而不是TRUE?

在EF Core中,有没有什么方法可以防止在查询中写入相同的条件,而代之以表达式?

NET8 MAUI并部署到真实设备上进行测试

C#DateTime.ParseExact不使用特定日期

ASP.NET Core MVC将值从视图传递到控制器时出现问题

BFF到具有AAD/Entra ID B2C的内部API(.NET/ASP.NET核心/标识.Web)

JsonPath在Newtonsoft.Json';S实现中的赋值

如何从Azure函数使用Graph API(SDK 5.35)中的[FindMeetingTimes]

C#如何获取字符串中引号之间的文本?

毛伊岛.NET 8图片不再适合按钮

如何在C#.NET桌面应用程序中动态更改焦点工具上的后退 colored颜色