zoukankan      html  css  js  c++  java
  • C#中打印拼接的字符串

    实例化打印文档

    //声明打印对象
    PrintDocument pd = new PrintDocument();
    int ilvPreviewIndex = 0; 

    在打印事件中设置基本属性

    private void btnPrint_Click(object sender, EventArgs e)
    {
        //获取和设置标签的高宽和边距
        decimal dLabelHeight = nudLabelHeight.Value;
        decimal dLabelWidth = nudLabelWidth.Value;
        decimal dTopMargin = nudTopMargin.Value;
        decimal dLeftMargin = nudLeftMargin.Value;
        //设置边距
        Margins margin = new Margins(0, 0, 0, 0);
        pd.DefaultPageSettings.Margins = margin;
        pd.DefaultPageSettings.Margins = margin;
        //横向打印
        //pd.DefaultPageSettings.Landscape = true;
        //循环打印
        for(; ilvPreviewIndex < dgvPreview.Rows.Count; ilvPreviewIndex++)
        {
            //页面尺寸
            pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", Utility.GetPixelByWidth(dLabelWidth), Utility.GetPixelByWidth(dLabelHeight));
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            pd.Print();
        }
    }

    打印事件处理

    /// <summary>
    /// 打印事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        //排架号中的行间距
        int iMiddle = int.Parse(Utility.ConfigGetItem("ShelfMiddle"));
        //获取和设置标签的高宽和边距
        decimal dLabelHeight = nudLabelHeight.Value;
        decimal dLabelWidth = nudLabelWidth.Value;
        decimal dTopMargin = nudTopMargin.Value;
        decimal dLeftMargin = nudLeftMargin.Value;
        //获取排架号
        string sBeginCallNo = dgvPreview.Rows[ilvPreviewIndex].Cells[0].Value.ToString();
        string sConnectSymbol = txtConnectSymbol.Text.Trim();
        string sEndCallNo = dgvPreview.Rows[ilvPreviewIndex].Cells[1].Value.ToString();
        //设置水平文字对齐方式
        StringFormat stringFormat = new StringFormat();
        stringFormat.LineAlignment = StringAlignment.Center;
        stringFormat.Alignment = StringAlignment.Center;
        //将排架号进行拼接打印
        Graphics g = e.Graphics;
        float leftMargin = Utility.GetPixelByWidth(dLeftMargin); //左边距
        SolidBrush myBrush = new SolidBrush(Color.Black); //刷子
        float yPosition = Utility.GetPixelByHeight(dTopMargin); //行定位
        Font printFont = new Font("宋体", 12 f, FontStyle.Bold); //设置字体
        g.DrawString(sBeginCallNo, printFont, myBrush, leftMargin, yPosition, stringFormat);
        yPosition += Utility.GetPixelByHeight(iMiddle); //另起一行
        g.DrawString(sConnectSymbol, printFont, myBrush, leftMargin, yPosition, stringFormat);
        yPosition += Utility.GetPixelByHeight(iMiddle); //另起一行
        g.DrawString(sEndCallNo, printFont, myBrush, leftMargin, yPosition, stringFormat);
    }
  • 相关阅读:
    UVALive 7146 (贪心+少许数据结构基础)2014acm/icpc区域赛上海站
    hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场
    【进阶——树状数组】 区间求最值
    软件测试作业4
    软件测试作业三 尝试使用JUnit
    软件测试作业2:读代码回答问题
    软件测试作业1:描述一个曾遇到的错误
    hdu1064Financial Management
    hdu1161Eddy's mistakes
    hdu1020Encoding
  • 原文地址:https://www.cnblogs.com/masonblog/p/12740733.html
Copyright © 2011-2022 走看看