gpt4 book ai didi

c# - 无对话框自动打印

转载 作者:行者123 更新时间:2023-11-30 15:33:22 25 4
gpt4 key购买 nike

我找到了打印代码。但是我想在没有对话框的情况下自动发送到打印机。我知道打印机名称。我从 SQL 表中获取打印机名称。我该怎么做?

// select printer and get printer settings
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() != true)
{
return;
}

// create a document
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

// create a page
FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;

// add some text to the page
TextBlock page1Text = new TextBlock();
page1Text.Text = "This is a text"
page1Text.FontSize = 12; // 30pt text
page1Text.Margin = new Thickness(50); // 1 inch margin
page1.Children.Add(page1Text);

// add the page to the document
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
document.Pages.Add(page1Content);

// and print
pd.PrintDocument(document.DocumentPaginator, "Print");

最佳答案

尝试直接使用 PrintDocument 类而不是 PrintDialog 类,您可以在其中通过名称设置打印机:

using System.Drawing.Printing;

PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "my printer";

遍历可用的打印机名称:

foreach (string s in PrinterSettings.InstalledPrinters) {
//
}

关于c# - 无对话框自动打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17590896/

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