HEADER AND FOOTER NOT WORKING

Dear sir,

As per my below code.. Using method one, header is working where as using method 2, header is not working

private void button11_Click(object sender, EventArgs e)
        {
            {
                double width = 0;
                foreach (var columns in sfDataGrid1.Columns)
                    width += columns.ActualWidth;
                   
                   
                PdfDocument document = new PdfDocument();
                document.PageSettings.Width = (float)width;
                PdfExportingOptions pdfOptions = new PdfExportingOptions();

                pdfOptions.HeaderFooterExporting += options_HeaderFooterExporting;

               //method1
                /*document = sfDataGrid1.ExportToPdf(pdfOptions);*/

                //method2
                var page = document.Pages.Add();
                var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, pdfOptions);
                PDFGrid.Draw(page, new PointF());

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

                }
            }
        }

        private void options_HeaderFooterExporting(object sender, Syncfusion.WinForms.DataGridConverter.Events.PdfHeaderFooterEventArgs e)
        {
            PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
            var width = e.PdfPage.GetClientSize().Width;
            PdfPageTemplateElement header = new PdfPageTemplateElement(width, 38);
            header.Graphics.DrawString("Order Details", font, PdfPens.Black, 70, 3);
            e.PdfDocumentTemplate.Top = header;
        }

7 Replies

AA Arulraj A Syncfusion Team August 20, 2018 12:51 PM UTC

Hi Deepak, 

Thanks for contacting Syncfusion support. 

By default, HeaderFooterExporting event will not be fired for the ExportToPdfGrid method, as it exports only the Grid. But we can draw the header or footer on the PDF document after exporting the grid.  
Please refer to the below code example and sample from the given location. 

Code Example: 
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; 


 
Please let us know if you need any further assistance on this. 

Regards, 
Arulraj A 



UH Uwe Hein replied to Arulraj A October 15, 2023 02:13 PM UTC

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



CM Chidanand Murugaiah Syncfusion Team October 16, 2023 12:24 PM UTC

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.



UH Uwe Hein replied to Chidanand Murugaiah October 16, 2023 03:40 PM UTC

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 

 




CM Chidanand Murugaiah Syncfusion Team October 17, 2023 04:09 PM UTC

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.


Attachment: SfDataGrid_ExportToPdf_d4acd2ff.zip


UH Uwe Hein October 17, 2023 05:56 PM UTC

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   

  



CM Chidanand Murugaiah Syncfusion Team October 18, 2023 09:44 AM UTC

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.



Loader.
Up arrow icon