本文整理汇总了C#中System.Drawing.Printing.PrintDocument.Print方法的典型用法代码示例。如果您正苦于以下问题:C# PrintDocument.Print方法的具体用法?C# PrintDocument.Print怎么用?C# PrintDocument.Print使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Printing.PrintDocument
的用法示例。
在下文中一共展示了PrintDocument.Print方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrintingExample
//引入命名空间
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
public class PrintingExample
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public PrintingExample()
{
Printing();
}
// 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) ;
// Iterate over the file, printing each line.
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;
}
// Print the file.
public void Printing()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// This is the main entry point for the application.
public static void Main(string[] args)
{
string sampleName = Environment.GetCommandLineArgs()[0];
if(args.Length != 1)
{
Console.WriteLine("Usage: " + sampleName +" <file path>");
return;
}
filePath = args[0];
new PrintingExample();
}
}
示例2: PrintDocument.Print()
//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class PrintDocumentSubClass : Form
{
public PrintDocumentSubClass()
{
this.cmdPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
//
this.cmdPrint.Location = new System.Drawing.Point(109, 122);
this.cmdPrint.Size = new System.Drawing.Size(75, 23);
this.cmdPrint.Text = "Print";
this.cmdPrint.UseVisualStyleBackColor = true;
this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(278, 259);
this.Controls.Add(this.cmdPrint);
this.Text = "Multi Page Print";
this.ResumeLayout(false);
}
private void cmdPrint_Click(object sender, EventArgs e)
{
PrintDocument doc = new TextDocument();
doc.PrintPage += this.Doc_PrintPage;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
TextDocument doc = (TextDocument)sender;
Font font = new Font("Arial", 10);
float lineHeight = font.GetHeight(e.Graphics);
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
doc.PageNumber += 1;
while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset <= doc.Text.GetUpperBound(0))
{
e.Graphics.DrawString(doc.Text[doc.Offset], font,Brushes.Black, x, y);
doc.Offset += 1;
y += lineHeight;
}
if (doc.Offset < doc.Text.GetUpperBound(0))
{
e.HasMorePages = true;
} else {
doc.Offset = 0;
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PrintDocumentSubClass());
}
private System.Windows.Forms.Button cmdPrint;
}
class TextDocument : PrintDocument{
private string[] text;
public string[] Text;
public int PageNumber;
public int Offset;
public TextDocument()
{
this.Text = new string[100];
for (int i = 0; i < 100; i++)
{
this.Text[i] += "string Text ";
}
}
}