当我尝试做 printdocument1.print(); 系统显示一个小模型弹出作为文件名和系统保持沉默而不会抛出任何错误
这是我的 C# 代码背后的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Management;
using System.Diagnostics;
namespace csvform
{
public partial class Form3 : Form
{
private PrintDocument printDocument1 = new PrintDocument();
private string stringToPrint;
public Form3()
{
InitializeComponent();
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
}
private void ReadFile()
{
string docName = "testPage.txt";
string docPath = @"c:\";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath +
docName, FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
stringToPrint = reader.ReadToEnd();
}
}
private void printDocument1_PrintPage(object sender,
PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
Font nf = new Font(new FontFamily("Arial"), 10,
System.Drawing.FontStyle.Bold);
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint,nf,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint,nf, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}
private void button1_Click(object sender, EventArgs e)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
string printerName = "";
foreach (ManagementObject printer in searcher.Get())
{
printerName = printer["Name"].ToString().ToLower();
if (printerName.Equals(@"\\chenraqdc2.raqmiyat.local\hp color laserjet cp1510"))
{
// printDocument1.Print();
try
{
// Assumes the default printer.
ReadFile();
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while printing", ex.ToString());
}
}
}
}
}
}