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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM