使用Print Document在C#中打印文件

时间:2016-12-16 10:54:13

标签: c# printing printdocument

我正在创建C#应用程序,我在其中使用外部类来打印dataGridView。使用此类我能够打印文档pdf但我无法将文件保存到磁盘。我知道这可以通过printDocument1.PrinterSettings.PrintToFile=trueprintDocument1.PrinterSettings.PrintFileName = path来实现,但正如我提到的那样,我正在使用外部类,因为我无法做到这一点。我正在发布用于打印网格的代码,并且还发布了指向外部类的链接。请帮我如何根据我的需要进行调整

DGVPrinter printer = new DGVPrinter();
        //if (tbReportSchoolName.Text == string.Empty)
        //{ 
            printer.Title = "The Punjab School"; 
        //}
        //else { printer.Title = tbReportSchoolName.Text.Trim(); } //school name
        //if (tbReportHeader.Text == string.Empty)
        //{ 
            printer.SubTitle = "Absent Student Report | " + dtpTo.Value.ToShortDateString(); 
        //}
        //else { printer.SubTitle = tbReportHeader.Text.Trim() + "|" + dtpTo.Value.ToShortDateString(); }


        printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

        printer.PageNumbers = true;
        printer.PageNumberInHeader = false;
        printer.PorportionalColumns = true;
        printer.HeaderCellAlignment = StringAlignment.Near;
        //if (tbReportFooter.Text == string.Empty)
        //{ 
            printer.Footer = "Allied School, A Project of Punjab Group of Colleges."; 
        //}
        //else { printer.Footer = tbReportFooter.Text.Trim(); }
        printer.FooterSpacing = 15;
        printer.PrintDataGridView(reportGrid);

类名是DGVPrinter,网格名是dgv 链接到课程:

https://drive.google.com/file/d/0BwBxpfm3kmmgci1aSnZ6T0RudGc/view

1 个答案:

答案 0 :(得分:0)

我没有在DGVPrinter类中看到任何代码要打印到PDF或文件。它具有属性AllowPrintToFile属性,但在类中没有使用它。你应该通过扩展这个类来自己编写它,或者你可以尝试下面的其他插件:

How to export PDF from data of datagrid?

http://www.aspsnippets.com/Articles/Export-Windows-Forms-DataGridView-to-PDF-using-iTextSharp-C-and-VBNet.aspx