我正在开发 C# 应用程序。在这个 C# 程序中,我想添加打印按钮,以便用户可以使用此按钮打印他们想要的任何文档。我设法通过阅读它来打印一个txt文件。但是如果我选择一个 pdf 文件,程序会在结果中打印一些错误代码。
private void printButton_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader("C:\\test.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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;
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
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 (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
除此之外,还有什么方法可以在文档上打开 ctrl-p 菜单(打印选项)?然后用户可以选择他/她想要的任何打印机。
我会很感激你的帮助。
致以我的问候...