PRINT PREVIEW FOR SFDATAGRID
|
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();
} |
{
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();
}
Attachment: 1_ccdac74.zip
|
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();
} |
- https://help.syncfusion.com/windowsforms/pdfviewer/working-with-pdf-viewer#customizing-print-size
- https://help.syncfusion.com/windowsforms/pdfviewer/working-with-pdf-viewer#printing-pdf-document-with-orientation-settings
Attachment: pdfviewererror_5275e455.zip
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
|
pdfviewer.RenderingEngine = PdfRenderingEngine.SfPdf; |
|
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(); |
Please let us know if you have any other questions.
Regards,
Deivaselvan
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.
|
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
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.
|
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();
}
|
- 11 Replies
- 6 Participants
-
DE Deepak
- Aug 10, 2018 08:50 AM UTC
- Oct 22, 2018 12:38 PM UTC