Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用microsoft print to file以编程方式将datagridview打印为pdf文件,而无需键入文件名_C#_Pdf_Printing_Datagridview_Bitmap - Fatal编程技术网

C#使用microsoft print to file以编程方式将datagridview打印为pdf文件,而无需键入文件名

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,

因此,我想使用Microsoft print to file将我的数据网格视图打印为位图,并自动命名文件和设置目录。将我的datagridview打印到打印机可以工作。每当我试图将它打印到文件中时,我只会得到一个空白页。如何将其打印到文件而不是打印机

//以unix时间戳格式生成文件名作为当前日期/时间

                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);
    }