-2

i am creating C# Application in which i am using external class to print dataGridView. with this class i am able to print document pdf but i am unable to save the file to disk. i know this can be achieved by printDocument1.PrinterSettings.PrintToFile=true and printDocument1.PrinterSettings.PrintFileName = path but as i mentioned that i am using external class for that i am not able to do that. i am posting code that i am using to print the grid and the link to external class is also posted. please help me how to adjust this according to my need

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

Class name is DGVPrinter and grid name is dgv Link to class:

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

Axad
  • 1
  • 7
  • 1
    you can install a pdf printer. – lordkain Dec 16 '16 at 10:56
  • thanks for reply @lordkain, but i am creating an automated application. like this will auto save the file on disk without user click – Axad Dec 16 '16 at 10:58
  • i just need help in pasting those 2 line code, i dont know where in class i have to post to solve my problem – Axad Dec 16 '16 at 11:06

1 Answers1

0

I dont see any code has been written in the DGVPrinter class to print to PDF or to File. It has the property AllowPrintToFile property, but nowhere it is used in the class. You should write it on your own by extending this class or you can try other plugin like below:

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

Community
  • 1
  • 1
sankar
  • 1,656
  • 1
  • 20
  • 36
  • yes there is no code for pdf file but there is code for printing with printDocument class and you know that printDocument has property named as printDocument.PrintToFile that saves the file in disk. please help me use that property – Axad Dec 16 '16 at 11:14
  • OK, I further read the MSDN document and found there is no possibility to do this programmatically. Refer the below link: https://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.printtofile(v=vs.110).aspx _The PrintToFile property is only used by the PrintDialog and cannot be set programmatically. The Print to file option only appears on the PrintDialog when the PrintDialog.AllowPrintToFile property is set to true._ – sankar Dec 16 '16 at 11:20