|
PdfDocument document = new PdfDocument();
document.PageSettings.Width = (float)width;
PdfExportingOptions pdfOptions = new PdfExportingOptions();
var page = document.Pages.Add();
var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, pdfOptions);
PDFGrid.Draw(page, new PointF(0, 30));
RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 50);
PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
header.Graphics.DrawString("Order Details", font, PdfPens.Black, 70, 3);
document.Template.Top = header; |
Hi everybody,
had the same problem with 23.1.40. Tried your provided sample. Works OK ONLY on the first page. But not on the following pages.
regards
Uwe
Hi Uwe Hein,
Based on the reported scenario, the ExportToPDFGrid method is not recommended when you are using a DataGrid. It creates a default PDF header and footer. For DataGrid, we suggest using the ExportToPDF method, as it's the proper way to add a header and footer by utilizing the HeaderFooterExporting event.
You can also refer our online user guide documentation regarding the same by the following link and also we have attached the sample for your reference.
UG link: https://help.syncfusion.com/windowsforms/datagrid/exporttopdf
We hope this helps. Please let us know, if need any further assistance.
Regards,
Chidanand M.
Hi Chidanand Murugaiah,
thank you for your really fast answer. This explains a lot. Can you please tell me how to get a landscape pdf output when using ExportToPDF Method ?
regards
Uwe
Hi Deepak,
If you want to change the page settings, you should use ExportToPDFGrid method because page settings are not handled from SfDataGrid. When using ExportToPDFGrid you can also achieve the header and footer by adding the header before call PDFGrid.Draw method. We modified the sample based on your requirement.
Kindly refer the below code snippet
|
void ExportDataGrid(object sender, System.EventArgs e) { double width = 0; foreach (var columns in sfDataGrid1.Columns) width += columns.ActualWidth;
document.PageSettings.Width = (float)width; PdfExportingOptions pdfOptions = new PdfExportingOptions(); document.PageSettings.Orientation = PdfPageOrientation.Landscape; var page = document.Pages.Add();
RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 50); PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold); header.Graphics.DrawString("Order Details", font, PdfPens.Black, 70, 3); document.Template.Top = header;
var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, pdfOptions); PDFGrid.Draw(page, new PointF(0, 30));
SaveFileDialog sfd = new SaveFileDialog { Filter = "PDF Files(*.pdf)|*.pdf", FileName = "document1" }; if (sfd.ShowDialog() == DialogResult.OK) { using (Stream stream = sfd.OpenFile()) document.Save(stream); if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK) System.Diagnostics.Process.Start(sfd.FileName); } } |
We hope this helps. Please let us know, if need any further assistance.
Regards,
Chidanand M.
Hi Chidanand Murugaiah,
thank you very much - that was a really fast response. This is just, what I needed.
Do you have any idea, why nobody answered my question regarding the sfDataGrid text property from Oct 9, 2023 06:44 PM UTC ?
regards and thanks again
Uwe
Hi Uwe,
Sorry for the inconvenience.
Our website has recently experienced an unexpected downtime due to some technical issues. So we have missed your new thread on our end. Now we have updated the response in the corresponding forum.
We appreciate your understanding. Please let us know your concerns. We will be happy to assist you.
Regards,
Chidanand M.