C# 如何使用此自定义打印类获取要打印的选项卡?
下面的代码是我试图用来将数据打印到打印机上的代码。数据正在打印,但选项卡未打印。我正在打印的字符串如下所示:C# 如何使用此自定义打印类获取要打印的选项卡?,c#,printing,tabs,C#,Printing,Tabs,下面的代码是我试图用来将数据打印到打印机上的代码。数据正在打印,但选项卡未打印。我正在打印的字符串如下所示: string textToPrint = "Member Number\tAddress\tCity\tState"; 但是当它打印时,它看起来像是成员编号DressCityState 我在互联网上找到了这个printer类,除了打印标签外,它似乎还能工作(在报告上打印字符)。如何确保标签打印 using System; using System.Collections.Generic
string textToPrint = "Member Number\tAddress\tCity\tState";
但是当它打印时,它看起来像是成员编号DressCityState
我在互联网上找到了这个printer类,除了打印标签外,它似乎还能工作(在报告上打印字符)。如何确保标签打印
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
namespace HighLowReport
{
public class PCPrint : System.Drawing.Printing.PrintDocument
{
//Property variable for the font the user wishes to use
private Font _font;
//Property variable for the text to be printed
private string _text;
//Property to hold the text that is to be printed
public string TextToPrint
{
get { return _text; }
set { _text = value; }
}
//Property to hold the font the user wishes to use
public Font PrinterFont
{
get { return _font; }
set { _font = value; }
}
// Static variable to hold the current character
// we're currently dealing with.
static int curChar;
// Empty constructor
public PCPrint()
: base()
{
// set the file stream
// Instantiate out Text property to an empty string
_text = string.Empty;
}
// Constructor to initialize our printer object
// and the text it's supposed to be printing
public PCPrint(string str)
: base()
{
// Set the file stream
// Set our text property value
_text = str;
}
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
{
// Run base code
base.OnBeginPrint(e);
// Check to see if the user provided a font
// if they didn't the we default to Times New Roman
if (_font == null)
{
_font = new Font("Times New Roman", 10);
}
}
// Override the default OnPrintPage method of the PrintDocument
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
{
// Run base code
base.OnPrintPage(e);
// declare local variables needed
int printHeight;
int printWidth;
int leftMargin;
int rightMargin;
Int32 lines;
Int32 chars;
// Set print area size and margins
{
printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
printWidth = base.DefaultPageSettings.PaperSize.Width = base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
leftMargin = base.DefaultPageSettings.Margins.Left; //X
rightMargin = base.DefaultPageSettings.Margins.Right; //Y
}
// Check if the user selected to print in Landscape mode
// if they did then we need to swap height/width parameters
if (base.DefaultPageSettings.Landscape)
{
int tmp;
tmp = printHeight;
printHeight = printWidth;
printWidth = tmp;
}
// Now we need to determine the total number of lines
// we're going to be printing
Int32 numLines = (int)printHeight / PrinterFont.Height;
// Create a rectangle printing area for our document
RectangleF printArea = new RectangleF(leftMargin, rightMargin, printWidth, printHeight);
// Use StringFormat class for the text layout of our document
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
// Fit as many characters as we can into the print area
e.Graphics.MeasureString(_text.Substring(RemoveZeros(ref curChar)), PrinterFont, new SizeF(printWidth, printHeight), format, out chars, out lines);
// Print the page
e.Graphics.DrawString(_text.Substring(RemoveZeros(ref curChar)), PrinterFont, Brushes.Black, printArea, format);
// Increase current char count
curChar += chars;
// Determine if there is more text to print, if
// there is then tell the printer there is more coming
if (curChar < _text.Length)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
curChar = 0;
}
}
// Function to replace any zeros in the size to a 1
// Zeros will mess up the printing area
public int RemoveZeros(ref int value)
{
// Check the value passed into the function
// If the value is a 0 (zero) then return a 1,
// otherwise return the value passed in
while (_text[value] == '\0')
{
value++;
}
return value;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统图;
使用系统、绘图、打印;
命名空间HighLowReport
{
公共类PCPrint:System.Drawing.Printing.PrintDocument
{
//用户希望使用的字体的属性变量
专用字体(u字体),;
//要打印的文本的属性变量
私有字符串_文本;
//属性保存要打印的文本
公共字符串TextToPrint
{
获取{return\u text;}
设置{u text=value;}
}
//属性来保存用户希望使用的字体
公共字体打印字体
{
获取{return\u font;}
设置{u font=value;}
}
//用于保存当前字符的静态变量
//我们目前正在处理。
静态int-curChar;
//空构造函数
公共印刷品()
:base()
{
//设置文件流
//将out Text属性实例化为空字符串
_text=string.Empty;
}
//构造函数初始化打印机对象
//还有应该打印的文本
公共PCPrint(字符串str)
:base()
{
//设置文件流
//设置文本属性值
_text=str;
}
受保护的覆盖无效OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
{
//运行基本代码
基于BeginPrint(e);
//检查用户是否提供了字体
//如果他们不这么做,我们就默认了《新罗马时报》
如果(_font==null)
{
_字体=新字体(“新罗马时代”,10);
}
}
//覆盖PrintDocument的默认OnPrintPage方法
PrintPage上受保护的覆盖无效(System.Drawing.Printing.PrintPageEventArgs e)
{
//运行基本代码
基于打印页(e);
//声明所需的局部变量
int打印高度;
int打印宽度;
int左边距;
int右边距;
Int32线;
Int32字符;
//设置打印区域大小和边距
{
printHeight=base.DefaultPageSettings.PaperSize.Height-base.DefaultPageSettings.Margins.Top-base.DefaultPageSettings.Margins.Bottom;
printWidth=base.DefaultPageSettings.PaperSize.Width=base.DefaultPageSettings.Margins.Left-base.DefaultPageSettings.Margins.Right;
leftMargin=base.DefaultPageSettings.Margins.Left;//X
rightMargin=base.DefaultPageSettings.Margins.Right;//Y
}
//检查用户是否选择以横向模式打印
//如果他们这样做了,那么我们需要交换高度/宽度参数
if(base.DefaultPageSettings.Landscape)
{
int tmp;
tmp=打印高度;
打印高度=打印宽度;
打印宽度=tmp;
}
//现在我们需要确定行的总数
//我们要印刷了
Int32 numLines=(int)printerheight/PrinterFont.Height;
//为文档创建一个矩形打印区域
矩形F打印区域=新矩形F(左边距、右边距、打印宽度、打印高度);
//将StringFormat类用于文档的文本布局
StringFormat格式=新的StringFormat(StringFormatFlags.LineLimit);
//将尽可能多的字符放入打印区域
e、 图形.MeasureString(_text.Substring(删除零(ref curChar))、PrinterFont、新SizeF(printWidth、printHeight)、格式、输出字符、输出行);
//打印页面
e、 Graphics.DrawString(_text.Substring(RemoveZeros(ref curChar)),PrinterFont,Brushes.Black,printArea,format);
//增加当前字符数
curChar+=chars;
//确定是否有更多文本要打印,如果
//然后告诉打印机还有更多
如果(curChar<_text.Length)
{
e、 HasMorePages=true;
}
其他的
{
e、 HasMorePages=false;
curChar=0;
}
}
//函数将大小中的任何零替换为1
//零会弄乱打印区域
公共整型删除零(参考整型值)
{
//检查传入函数的值
//如果值为0(零),则返回1,
//否则,返回传入的值
而(_text[value]=='\0')
{
值++;
}
返回值;
}
}
}
对于format StringFormat变量,必须添加制表位数组:
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
float[] formatTabs = { 10.0f, 20.0f };
format.SetTabStops(0.0f, formatTabs);
工作得很有魅力。非常感谢!