【问题标题】:c# - How to print the text of some labels? [closed]c# - 如何打印一些标签的文本? [关闭]
【发布时间】:2014-10-25 13:55:52
【问题描述】:

我的程序中有一些标签,例如:

Name: Paul
Bought: bike

我只想打印这个。我试图用 PrintDialog 和 PrintDocument 做到这一点,但没有成功。我不知道如何获取这些标签的文本并打印。更具体地说,我不知道如何打印任何东西。

这是我第一次尝试做这样的事情,如果任何知道如何用 C# 打印的人帮助我,我将非常感激。

【问题讨论】:

  • 如果您尝试过,请发布。这不是一个请为我编写代码的网站。

标签: c# .net winforms printing


【解决方案1】:

在 C# 中,打印与仅绘画几乎没有区别。很简单:

public void PrintThemAll()
{
    var document = new PrintDocument();
    document.PrintPage += document_PrintPage;
    document.Print();
}

void document_PrintPage(object sender, PrintPageEventArgs e)
{
    var graphics = e.Graphics;
    var normalFont = new Font("Calibri", 14); 

    var pageBounds = e.MarginBounds;
    var drawingPoint = new PointF(pageBounds.Left, (pageBounds.Top + normalFont.Height));

    graphics.DrawString("Name: Paul", normalFont, Brushes.Black, drawingPoint);

    drawingPoint.Y += normalFont.Height;

    graphics.DrawString("Bought: bike", normalFont, Brushes.Black, drawingPoint);

    e.HasMorePages = false; // No pages after this page.
}

您需要创建 PrintDocument 对象,并为 PrintPage 事件添加处理程序。每次将 HasMorePages 设置为 true 时都会调用 PrintPage 处理程序(如果设置为 false - 当前页面是最后一页)。在处理程序内部,您可以在打印文档中绘制任何您想要的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多