PRINT PREVIEW FOR SFDATAGRID

1. Print Preview for SFDatagrid is not available ? How to get the same ?




11 Replies

FP Farjana Parveen Ayubb Syncfusion Team August 10, 2018 12:09 PM UTC

Hi Deepak, 

Thanks for contacting Syncfusion support. 

Currently SfDataGrid don’t have printing (also print preview) option in SfDataGrid. But we have a work around to achieve your requirement by using Pdf exporting support and PrintPreviewDialog. Please refer the below code example and sample in the following location, 

Code Example: 
 
void PrintPreview(object sender, System.EventArgs e) 
{ 
    PrintPreviewDialog printdialog = new PrintPreviewDialog(); 
    PdfViewerControl pdfviewer = new PdfViewerControl(); 
    MemoryStream pdfstream = new MemoryStream(); 
    var document = sfDataGrid1.ExportToPdf(); 
    document.Save(pdfstream); 
    pdfviewer.Load(pdfstream); 
    printdialog.Document = pdfviewer.PrintDocument; 
    printdialog.ShowDialog();    
} 
 
  
Regards, 
Farjana Parveen A 



DE Deepak September 9, 2018 01:22 PM UTC

As per data in SFDATAGRID, all columns exported to PDF with different size of pdf width (size as per columns), was unable to open in printpreviewdialog box.

For example, my current pdf size is 45.16 x 8.26 inch, which was unable to view properly as per pdf, it is showing center of the pdf page.

How we can view the full pdf with any size in print preview dialog box ?




AK Adhikesevan Kothandaraman Syncfusion Team September 10, 2018 01:07 PM UTC

Hi Deepak, 

Thanks for your update. 

By default all the content will be displayed in the Print Preview dialog based on the page size. If the size of the columns is exceeds the page size, It will automatically load the remaining columns to the next page. 
So that you can view all the columns by navigating the pages. Refer to the following screen shot, 

 
 
 


Since we have little unclear with your reported scenario, please share your required scenario in the print preview dialog as the screen shot. So that we can provide the solution at the earliest. 

Regards, 
Adhi 



DE Deepak September 10, 2018 03:54 PM UTC

Hi Adhi,

Thanks for your reply.

As per your suggestion, By default all the content will be displayed in the Print Preview dialog based on the page size. How Can I Change Page Size of Print Preview ?

Following is the Code

 private void PrintPreview(object sender, System.EventArgs e)
        {

            PrintPreviewDialog printdialog = new PrintPreviewDialog();
            PdfViewerControl pdfviewer = new PdfViewerControl();
            MemoryStream pdfstream = new MemoryStream();
           
            //start
            PdfExportingOptions options = new PdfExportingOptions();
            options.HeaderFooterExporting += options_HeaderFooterExporting;
            options.AutoColumnWidth = true;
            options.AutoRowHeight = true;
            options.RepeatHeaders = true;
            options.ExportGroupSummary = true;
            options.ExportFormat = true;
            options.FitAllColumnsInOnePage = false;
            options.CellExporting += OnCellExporting;
            options.ExportTableSummary = true;
            //options.PageHeaderFooterEventHandler = PdfHeaderFooterEventHandler;              
            this.sfDataGrid1.AutoSizeController.ResetAutoSizeWidthForAllColumns();
            this.sfDataGrid1.AutoSizeController.Refresh();
            options.Exporting += options_Exporting;

            //Set document information.
            Syncfusion.Pdf.PdfDocument document = new Syncfusion.Pdf.PdfDocument();
            //document.PageSettings.Orientation = PdfPageOrientation.Landscape;
          
            // set pagewidth
            double width = 0;
            foreach (var columns in sfDataGrid1.Columns)
                width += columns.ActualWidth;
            Graphics g = this.sfDataGrid1.CreateGraphics();
            width = (float)width * 60 / g.DpiX;
            g.Dispose();
            document.PageSettings.Width = (float)width;
            var page = document.Pages.Add();

            foreach (var columns in sfDataGrid1.Columns)
            {
                if (!columns.Visible)
                    options.ExcludeColumns.Add(columns.MappingName);
            }

            //Header and footer-start
            RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 50);
            RectangleF bounds1 = new RectangleF(0, 20, document.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
            document.Template.Top = header;
            Syncfusion.Pdf.Graphics.PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 16f, PdfFontStyle.Regular);
            //header.Graphics.DrawString("NAME OF THE COMPANY", font, PdfPens.Black, 0, 0);
            header.Graphics.DrawString("NAME OF THE COMPANY", font, PdfPens.Black, bounds);
            Syncfusion.Pdf.Graphics.PdfFont font1 = new PdfStandardFont(PdfFontFamily.Courier, 12f, PdfFontStyle.Regular);
            header.Graphics.DrawString("Name of the Report", font1, PdfPens.Black, bounds1);
            //Header and footer-End

            //document = sfDataGrid1.ExportToPdf(options); ////column widht setting,not follwed
            var pdfGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, options); ////column widht setting,followed
            PdfGridLayoutResult layoutResult = pdfGrid.Draw(page, new PointF());
            //layoutResult.Page.Graphics.DrawRectangle(PdfPens.Red, layoutResult.Bounds);
            pdfGrid.Draw(page, new PointF());
            //document.Save(pdfstream);
            document.Save("c://1.pdf");
            // End

            /*var document = sfDataGrid1.ExportToPdf();
            document.Save(pdfstream);*/
           
            //pdfviewer.Load(pdfstream);
            pdfviewer.Load("C://1.pdf");
            printdialog.Document = pdfviewer.PrintDocument;
            printdialog.ShowDialog();
        }

As per said Commands, 1.PDF is created which is enclosed herewith for your reference. But when the said PDF Is open in print preview dialog box it is not opening properly.

PFA Display in print preview also.

Attachment: 1_ccdac74.zip


MA Mohanram Anbukkarasu Syncfusion Team September 11, 2018 12:00 PM UTC

Hi Deepak, 
 
Thanks for your update. 
 
We have checked the given code and the attachments. To display the content properly in the PrintPreviewDialog, you need to set PdfviewerControl.PrinterSettings.PageSize to Fit and PdfViewerControl1.PrinterSettings.PageOrientation to Landscape. Please refer to the following code example and sample from the given location. 
 
Code Example :  
 
void PrintPreviewButton_Click(object sender, System.EventArgs e) 
{ 
    PrintPreviewDialog printdialog = new PrintPreviewDialog(); 
    PdfViewerControl pdfviewer = new PdfViewerControl(); 
    MemoryStream pdfstream = new MemoryStream(); 
 
    //start 
    PdfExportingOptions options = new PdfExportingOptions(); 
    options.AutoColumnWidth = true; 
    options.AutoRowHeight = true; 
    options.RepeatHeaders = true; 
    options.ExportGroupSummary = true; 
    options.ExportFormat = true; 
    options.FitAllColumnsInOnePage = false; 
    options.ExportTableSummary = true; 
    this.sfDataGrid1.AutoSizeController.ResetAutoSizeWidthForAllColumns(); 
    this.sfDataGrid1.AutoSizeController.Refresh(); 
 
    //Set document information. 
    Syncfusion.Pdf.PdfDocument document = new Syncfusion.Pdf.PdfDocument(); 
    document.PageSettings.Orientation = PdfPageOrientation.Landscape; 
 
    // set pagewidth 
    double width = 0; 
    foreach (var columns in sfDataGrid1.Columns) 
        width += columns.ActualWidth; 
    Graphics g = this.sfDataGrid1.CreateGraphics(); 
    width = (float)width * 60 / g.DpiX; 
    g.Dispose(); 
    document.PageSettings.Width = (float)width; 
    var page = document.Pages.Add(); 
 
    foreach (var columns in sfDataGrid1.Columns) 
    { 
        if (!columns.Visible) 
            options.ExcludeColumns.Add(columns.MappingName); 
    } 
 
    //Header and footer-start 
    RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 50); 
    RectangleF bounds1 = new RectangleF(0, 20, document.Pages[0].GetClientSize().Width, 50); 
    PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); 
    document.Template.Top = header; 
    Syncfusion.Pdf.Graphics.PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 16f, PdfFontStyle.Regular); 
    header.Graphics.DrawString("NAME OF THE COMPANY", font, PdfPens.Black, bounds); 
    Syncfusion.Pdf.Graphics.PdfFont font1 = new PdfStandardFont(PdfFontFamily.Courier, 12f, PdfFontStyle.Regular); 
    header.Graphics.DrawString("Name of the Report", font1, PdfPens.Black, bounds1); 
    //Header and footer-End 
 
    var pdfGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, options); 
    pdfGrid.Draw(page, new PointF()); 
    document.Save("c://1.pdf"); 
    pdfviewer.PrinterSettings.PageSize = Syncfusion.Windows.PdfViewer.PdfViewerPrintSize.Fit; 
    pdfviewer.PrinterSettings.PageOrientation = Syncfusion.Windows.PdfViewer.PdfViewerPrintOrientation.Landscape; 
    pdfviewer.Load("c://1.pdf"); 
    printdialog.Document = pdfviewer.PrintDocument; 
    printdialog.ShowDialog(); 
} 
 
 
UG Links :  
  1. https://help.syncfusion.com/windowsforms/pdfviewer/working-with-pdf-viewer#customizing-print-size
  2. https://help.syncfusion.com/windowsforms/pdfviewer/working-with-pdf-viewer#printing-pdf-document-with-orientation-settings
Regards, 
Mohanram A. 



DE Deepak October 10, 2018 01:00 PM UTC

In recent updated version 16.3.0.21, In print preview (PDF Viewer) Image DPI change, due to which unable to read the file/report.

As per your last sample, after updating the Syncfusion version to 16.3.0.21 facing the problems.

How to change the DPI of Image ? 

PFA Report which is non readable in pdf viewer.

Please also send new commands updated  in the said new version.

Thanks

Attachment: pdfviewererror_5275e455.zip


DY Deivaselvan Y Syncfusion Team October 11, 2018 11:16 AM UTC

Hi Deepak,

We regret for the inconvenience.

We were able to reproduce the issue by using the provided code snippet in our latest version 16.3.0.21.

Regarding “16.2.0.50 it works properly”:
Until 16.2.0.50 essential studio version, the default rendering engine of PdfViewercontrol is SfPdf rendering engine. While using SfPdf rendering the document is viewed properly in PrintPreviewDialog. From the essential studio version 16.3.0.21, the default rendering engine of PdfViewerControl is changed into Pdfium from SfPdf. Due to this reason the document is viewed with less quality in PrintPreviewDialog while using the same code snippet. Please refer the below release notes for the changes in 16.3.0.21

https://help.syncfusion.com/windowsforms/release-notes/v16.3.0.21?type=all#pdfviewercontrol
 
 
Kindly set the RenderingEngine property of PdfViewerControl as SfPdf in your application to get this issue resolved. Please find the code snippet to set the rendering engine as SfPdf. 
pdfviewer.RenderingEngine = PdfRenderingEngine.SfPdf; 
 
The exported pdf document will be viewed properly in PrintPreviewDialog by using the below code snippet with 16.3.0.21 version. 
PrintPreviewDialog printdialog = new PrintPreviewDialog(); 
PdfViewerControl pdfviewer = new PdfViewerControl(); 
pdfviewer.RenderingEngine = PdfRenderingEngine.SfPdf; 
pdfviewer.PrinterSettings.PageSize = Syncfusion.Windows.PdfViewer.PdfViewerPrintSize.Fit; 
pdfviewer.Load("1.pdf"); 
printdialog.Document = pdfviewer.PrintDocument; 
printdialog.ShowDialog(); 
 
Meanwhile, we will consider improving the “Quality while previewing the document using PrintPreviewDialog in Pdfium rendering engine”.

Please let us know if you have any other questions.

Regards,
Deivaselvan
 



DE Deepak October 11, 2018 01:24 PM UTC

Thanks for your reply.

In PrintPreviewDialog it is showing the pages, but while giving print it does not support. 

Printing is not working.. Blank pages are printing.

Instead of my coding, check the Sample file send by Mohanram Anbukkarasu [Syncfusion] on Replied On September 11, 2018 08:00 AM, But in the sample also, it doe's print anything.

Pls check it and reply how to take printout
Thanks
Deepak


DY Deivaselvan Y Syncfusion Team October 12, 2018 02:42 PM UTC

Hi Deepak,

Thanks for the update.

We can reproduce the reported issue from our side. The issue is raised due to the document is not resets properly when printing the document using the print button in the PrintPreviewDialog box.  
Please find the code snippet to resets the Printdocument of PrintPreviewDialog in BeginPrint event and trigger the event before calling the PrintPreviewDialog.ShowDialog method. 
PrintPreviewDialog printdialog = new PrintPreviewDialog(); 
pdfviewer = new PdfViewerControl(); 
pdfviewer.PrinterSettings.PageSize = Syncfusion.Windows.PdfViewer.PdfViewerPrintSize.Fit; 
pdfviewer.RenderingEngine = PdfRenderingEngine.SfPdf; 
pdfviewer.Load("1.pdf"); 
printdialog.Document = pdfviewer.PrintDocument; 
printdialog.Document.BeginPrint += Document_BeginPrint; 
printdialog.ShowDialog(); 
 
private void Document_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 
{ 
        if (e.PrintAction == PrintAction.PrintToPrinter) 
            { 
                sender = pdfviewer.PrintDocument; 
            } 
} 

Please find the sample for the same from below link,
http://www.syncfusion.com/downloads/support/forum/139187/ze/PrintPreviewDemo2128551256

Regards,
Deivaselvan 



JI Jignesh October 19, 2018 01:20 PM UTC

Dear Sir,

As Print Preview is not available for SFDATAGRID, We are trying to generate PDF as report and view the same in PDF Viewer as report viewer..

But we are facing the following issues.

1. As per Default Printers setup, Page Height and Width of PDF file is not generating. Example If My Default printers Page size is A3, then exported pdf page size in not as per size of A3 paper.

2. for Above Example 1, if page style change from portrait to landscape, the same effect is not showing in the generated pdf file. Example for A3 paper if default page style is portrait then the generated pdf is not portrait. In the vice versa for Landscape also.

3. If My Default printer is A, and now selected printer is B from printdialog box, and default page is A4 set for printer B, then also there is no effect in generated PDF File.

4. In the same way for selected Printer B, portrait and landscape option for exported PDF File is not working.


Please note

1. my .NETframework is v 4.0

2. Page Size must be same as per above setting


Please send me sample file with above all four queries solved.




MA Mohanram Anbukkarasu Syncfusion Team October 22, 2018 12:38 PM UTC

Hi Jignesh, 
 
Thanks for your update. 
 
We are little bit unclear with your requirement. We suspect that your requirement is to set the same size and orientation for the pdf created while printing. For that try setting the Printdialog.PrintPreviewControl.Document.PrinterSettings.DefaultPageSettings property based on your requirement. Please refer to the following code example and sample from the given location. 
 
Code example :  
 
private void button1_Click(object sender, System.EventArgs e) 
{ 
    PrintPreviewDialog printdialog = new PrintPreviewDialog(); 
 
    MemoryStream pdfstream = new MemoryStream(); 
 
    //start 
    PdfExportingOptions options = new PdfExportingOptions(); 
    options.AutoColumnWidth = true; 
    options.AutoRowHeight = true; 
    options.RepeatHeaders = true; 
    options.ExportGroupSummary = true; 
    options.ExportFormat = true; 
    options.FitAllColumnsInOnePage = false; 
    options.ExportTableSummary = true; 
    this.sfDataGrid.AutoSizeController.ResetAutoSizeWidthForAllColumns(); 
    this.sfDataGrid.AutoSizeController.Refresh(); 
 
    //Set document information. 
    Syncfusion.Pdf.PdfDocument document = new Syncfusion.Pdf.PdfDocument(); 
    document.PageSettings.Orientation = PdfPageOrientation.Landscape; 
 
    // set pagewidth 
    double width = 0; 
    foreach (var columns in sfDataGrid.Columns) 
        width += columns.ActualWidth; 
    Graphics g = this.sfDataGrid.CreateGraphics(); 
    width = (float)width * 60 / g.DpiX; 
    g.Dispose(); 
    document.PageSettings.Width = (float)width; 
    var page = document.Pages.Add(); 
 
    foreach (var columns in sfDataGrid.Columns) 
    { 
        if (!columns.Visible) 
            options.ExcludeColumns.Add(columns.MappingName); 
    } 
 
    //Header and footer-start 
    RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 50); 
    RectangleF bounds1 = new RectangleF(0, 20, document.Pages[0].GetClientSize().Width, 50); 
    PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); 
    document.Template.Top = header; 
    Syncfusion.Pdf.Graphics.PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 16f, PdfFontStyle.Regular); 
    header.Graphics.DrawString("NAME OF THE COMPANY", font, PdfPens.Black, bounds); 
    Syncfusion.Pdf.Graphics.PdfFont font1 = new PdfStandardFont(PdfFontFamily.Courier, 12f, PdfFontStyle.Regular); 
    header.Graphics.DrawString("Name of the Report", font1, PdfPens.Black, bounds1); 
    //Header and footer-End 
 
    var pdfGrid = sfDataGrid.ExportToPdfGrid(sfDataGrid.View, options); 
    pdfGrid.Draw(page, new PointF()); 
    document.Save(@"../../1.pdf"); 
    pdfviewer.RenderingEngine = PdfRenderingEngine.SfPdf; 
    pdfviewer.PrinterSettings.PageSize = Syncfusion.Windows.PdfViewer.PdfViewerPrintSize.Fit; 
    pdfviewer.PrinterSettings.PageOrientation = Syncfusion.Windows.PdfViewer.PdfViewerPrintOrientation.Landscape; 
    pdfviewer.Load(@"../../1.pdf"); 
    printdialog.Document = pdfviewer.PrintDocument; 
    printdialog.PrintPreviewControl.Document.PrinterSettings.DefaultPageSettings.Landscape = pdfviewer.PrinterSettings.PageOrientation == Syncfusion.Windows.PdfViewer.PdfViewerPrintOrientation.Landscape; 
    printdialog.ShowDialog(); 
} 
 
 
 
In case we misunderstand your query or the given solution doesn’t meet your requirement, please provide us more clear information about your requirement. So that we can provide you an exact solution for your query. 
 
Regards, 
Mohanram A. 


Loader.
Up arrow icon