在我的应用程序中,我正在尝试创建一个功能来打印现有的 PDF 或 Doc。如何在 C# 中执行此操作并提供一种机制,以便用户可以选择不同的打印机或其他属性。
我查看了 PrintDialog 但不确定它正在尝试打印什么文件,如果有的话,b/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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string printPath =
System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
System.IO.StreamReader fileToPrint;
fileToPrint= new System.IO.StreamReader(printPath + @"\myFile.txt");
System.Drawing.Font printFont;
printPDF(e);
printDocument1.Print();
fileToPrint.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//printDoc(e);
}
public void printPDF(object sender ,
System.Drawing.Printing.PrintPageEventArgs e))
{
printFont = new System.Drawing.Font("Arial", 10);
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;
}
fileToPrint.Close();
}
public void printDoc()
{
}
}
}