当前位置: 首页 > article >正文

c# 打印字符串

using System;
using System.Drawing;
using System.Drawing.Printing;
 
public class PrinterExample
{
    public static void Main()
    {
        // 创建PrintDocument对象
        PrintDocument printDocument = new PrintDocument();
 
        // 设置打印任务的事件处理程序
        printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
 
        // 打印文档
        try
        {
            printDocument.Print();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
 
    // 打印页面的事件处理程序
    private static void PrintPage(object sender, PrintPageEventArgs e)
    {
        string textToPrint = "Hello, World!"; // 要打印的字符串
        Font printFont = new Font("Arial", 12); // 字体和大小
        Brush brush = new SolidBrush(Color.Black); // 画笔颜色
 
        // 测量字符串的大小
        SizeF textSize = e.Graphics.MeasureString(textToPrint, printFont);
 
        // 在页面上打印字符串(水平居中,垂直居中)
        PointF textLocation = new PointF(
            (e.PageBounds.Width / 2) - (textSize.Width / 2),
            (e.PageBounds.Height / 2) - (textSize.Height / 2));
 
        e.Graphics.DrawString(textToPrint, printFont, brush, textLocation);
    }
}


http://www.kler.cn/a/516745.html

相关文章:

  • 迅为RK3568开发板篇OpenHarmony实操HDF驱动控制LED-添加内核编译
  • C语言常用知识结构深入学习
  • vue项目的创建
  • GPU算力平台|在GPU算力平台部署MedicalGPT医疗大模型的应用教程
  • MyBatis最佳实践:MyBatis 框架的缓存
  • 3、搭建企业知识库:从需求分析到方案设计
  • 配电网的自动化和智能化水平介绍
  • Python中使用Ollama API
  • SpringBoot的Swagger配置
  • Javaweb之css
  • 时序数据库的使用场景
  • openresty(nginx)+lua+kafka实现日志搜集系统
  • 【Redis】事务
  • Windows Docker Desktop安装及使用 Docker 运行 MySQL
  • elasticsearch segment数量对读写性能的影响
  • STM32_SD卡的SDIO通信_基础读写
  • 互联网产品品牌形象构建与开源AI智能名片S2B2C商城小程序的应用研究
  • Lock和Synchronized的区别,源码分析
  • 基于Springboot用axiospost请求接收字符串参数为null的解决方案
  • 【unity游戏开发之InputSystem——02】InputAction的使用介绍(基于unity6开发介绍)