C#使用microsoft print to file以编程方式将datagridview打印为pdf文件,而无需键入文件名
因此,我想使用Microsoft print to file将我的数据网格视图打印为位图,并自动命名文件和设置目录。将我的datagridview打印到打印机可以工作。每当我试图将它打印到文件中时,我只会得到一个空白页。如何将其打印到文件而不是打印机 //以unix时间戳格式生成文件名作为当前日期/时间C#使用microsoft print to file以编程方式将datagridview打印为pdf文件,而无需键入文件名,c#,pdf,printing,datagridview,bitmap,C#,Pdf,Printing,Datagridview,Bitmap,因此,我想使用Microsoft print to file将我的数据网格视图打印为位图,并自动命名文件和设置目录。将我的datagridview打印到打印机可以工作。每当我试图将它打印到文件中时,我只会得到一个空白页。如何将其打印到文件而不是打印机 //以unix时间戳格式生成文件名作为当前日期/时间 string file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1,
string file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1,
1))).TotalSeconds.ToString();
// the directory to store the output.
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// initialize PrintDocument object
PrintDocument printDocument1 = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = Path.Combine(directory, file + ".pdf"),
}
};
printDocument1.Print();
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgse)
{
Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width,
this.dataGridView1.Height));
e.Graphics.DrawImage(bm, 0, 0);
}