首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

将页眉和页脚添加到 Printing.PrintDocument (.Net 2.0) 的最简单方法?

将页眉和页脚添加到 Printing.PrintDocument (.Net 2.0) 的最简单方法是使用 PrintPage 事件处理程序。在此事件处理程序中,您可以自定义页眉和页脚的内容和样式。以下是一个简单的示例:

  1. 首先,在您的项目中添加 System.Drawing 和 System.Drawing.Printing 命名空间。
代码语言:csharp
复制
using System.Drawing;
using System.Drawing.Printing;
  1. 然后,创建一个 PrintDocument 对象并添加 PrintPage 事件处理程序。
代码语言:csharp
复制
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
  1. 在 PrintPage 事件处理程序中,您可以自定义页眉和页脚的内容和样式。
代码语言:csharp
复制
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    // 页眉
    string headerText = "页眉文本";
    Font headerFont = new Font("Arial", 14);
    float headerX = e.MarginBounds.Left;
    float headerY = e.MarginBounds.Top;
    e.Graphics.DrawString(headerText, headerFont, Brushes.Black, headerX, headerY);

    // 页脚
    string footerText = "页脚文本";
    Font footerFont = new Font("Arial", 12);
    float footerX = e.MarginBounds.Left;
    float footerY = e.MarginBounds.Bottom - footerFont.GetHeight(e.Graphics);
    e.Graphics.DrawString(footerText, footerFont, Brushes.Black, footerX, footerY);

    // 绘制文本内容
    string text = "这是要打印的文本内容。";
    Font textFont = new Font("Arial", 10);
    float textX = e.MarginBounds.Left;
    float textY = headerY + headerFont.GetHeight(e.Graphics) + 10;
    e.Graphics.DrawString(text, textFont, Brushes.Black, textX, textY);

    // 指示已处理完所有页面
    e.HasMorePages = false;
}
  1. 最后,调用 Print 方法启动打印。
代码语言:csharp
复制
printDocument.Print();

这样,您就可以在打印文档时添加页眉和页脚了。请注意,这只是一个简单的示例,您可以根据需要自定义页眉和页脚的内容和样式。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券