为什么printdocument的此代码无法正确打印? [英] Why does this code for printdocument not print correctly?
问题描述
嗨.我是C#的新手,毫无疑问我做错了事.
Hi. I'm pretty new to C# and no doubt I'm doing something wrong.
我有一个只有一行文本的文本文件.没有特殊字符,什么都没有.
I have a textfile that has a single line of text. No special characters, nothing.
我安装了多台打印机.我想将文本文件打印到不是默认打印机的打印机,而不显示PrinterDialog.打印机的选择似乎可行,但是页面上出现了垃圾.垃圾,我的意思是 彼此印刷的字母,有点像是双重印刷.也可以是控制字符,但绝对是文本文件中的单行.我尝试调试,并且知道流文件确实在正确读取行,所以 这只是打印机的输出.我注意到事件中的ev.graphics.drawstring重载,并且StringFormat似乎是可选的,因此我尝试在有和没有最后一个参数"new stringformat()"的情况下运行它.我没什么区别.
I have multiple printers installed. I want to print the textfile to a printer that is not the default printer, without showing a PrinterDialog. The printer choice seems to be working, but I get garbage on the page. By garbage, I mean letters that are printing on each other, sort of a double printing. It could be control characters, too, but it's definitly the single line within the text file. I tried debugging and know the streamfile is indeed reading the line correctly, so it's just the printer output. I noticed the the ev.graphics.drawstring within the event is overloaded and the StringFormat seemed optional, so I've tried running it both with and without the last parameter of "new stringformat()". I saw no difference.
我的代码非常简单,并取自Microsoft的示例.首先,我创建一个新的printdocument实例.然后,我将文档分配给自己选择的打印机.最后,我为printpage事件分配一个事件处理程序. 我直接从代码示例中复制了事件处理程序.知道为什么文本没有打印出我期望的样子吗?
My code is pretty simple and was taken from a Microsoft example. First I create a new instance of a printdocument. Then I assign the document to the printer of my choice. Finally, I assign an event handler for the printpage event. I copied the event handler straight from the code example. Any idea why the text isn't printing what I expected?
谢谢!
黛比·埃里克森
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "Sharp AR-M550N PCL5e";
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
推荐答案
尝试一下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private StreamReader fileToPrint;
private Font printFont;
private PrintDocument printDocument1 = new PrintDocument();
public Form1()
{
InitializeComponent();
printDocument1.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}
private void button1_Click(object sender, EventArgs e)
{
//String printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
fileToPrint = new StreamReader(@"C:\Users\jgrove\Documents\Text\SetDIstinctTraces.txt");
printFont = new System.Drawing.Font("Arial", 10);
printDocument1.Print();
fileToPrint.Close();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float yPos = 0f;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
while (count < linesPerPage)
{
line = fileToPrint.ReadLine();
if (line == null)
{
break;
}
yPos = topMargin + count * printFont.GetHeight(e.Graphics);
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
}
}
}
这篇关于为什么printdocument的此代码无法正确打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!