SFDATA GRID PDF Settings

After giving this commands, followings pdf functions are not working

 

1.Page size, page orientation is not working

2. Margins not working

3. Export to PDF, how can change the boarder of the box.. currently its gray color with dotted lines which want to change to black color thin line.

4. If Any column in sfdatagrid resized to zero/hide, how can the same column exclude to pdf export ?


Please reply me at the earliest.


commands :


PdfExportingOptions options = new PdfExportingOptions();

 //var page = document.Pages.Add();

options.AutoColumnWidth = true;

options.AutoRowHeight = true;

options.RepeatHeaders = true;

options.ExportFormat = true;

options.FitAllColumnsInOnePage = true;


            foreach (var column in this.sfDataGrid1.Columns)

            {

             

                if (column.Width != 0)

 

                    column.Width = 0;

            //options.ExcludeColumns.Add("CustomerID");                    

            }

 

this.sfDataGrid1.AutoSizeController.ResetAutoSizeWidthForAllColumns();

//this.sfDataGrid1.AutoSizeController.Refresh();

           

var document = sfDataGrid1.ExportToPdf(options);

//Set document information.

document.PageSettings.Size = PdfPageSize.A3;

document.PageSettings.Orientation = PdfPageOrientation.Landscape;

document.PageSettings.Margins.Left = 20;

document.PageSettings.Margins.Right = 20;

document.PageSettings.Margins.Top = 100;

document.PageSettings.Margins.Bottom = 20;

document.PageSettings.Orientation = PdfPageOrientation.Landscape;

//document information ... this is working fine

document.DocumentInformation.Author = "TESTING INDIA ";

document.DocumentInformation.CreationDate = DateTime.Now;

document.DocumentInformation.Creator = "SAMPLE TESTING";

document.DocumentInformation.Keywords = "PDF";

document.DocumentInformation.Subject = "Document information DEMO";

document.DocumentInformation.Title = "Essential PDF Sample";

           

var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, options);

     /*var format = new PdfGridLayoutFormat()

            {

                Layout = PdfLayoutType.Paginate,

                Break = PdfLayoutBreakType.FitPage

            };*/

            //PDFGrid.Draw(page, new PointF(), format);

            SaveFileDialog saveFileDialog = new SaveFileDialog

            {

                Filter = "PDF Files(*.pdf)|*.pdf"

            };

            if (saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                using (Stream stream = saveFileDialog.OpenFile())

                {

                    document.Save(stream);

                }

                //Message box confirmation to view the created Pdf file.

                if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)

                {

                    //Launching the Pdf file using the default Application.

                    System.Diagnostics.Process.Start(saveFileDialog.FileName);

                }

            }






9 Replies

FP Farjana Parveen Ayubb Syncfusion Team August 13, 2018 12:56 PM UTC

Hi Deepak, 
 
Thanks for contacting Syncfusion support. 
 
Please find the updated in the below table, 
 
Query 
Response 
1.Page size, page orientation is not working 
You can change the page orientation by get the exported PdfGrid by using ExportToPdfGrid method and then draw that PdfGrid into a PdfDocument by changing the PageSettings.Orientation property of PdfDocument. 
 
You can able to set the Page size Pdf document when exporting. Please refer the below code example 
 
Code Example 
 
private void ExportDataGridPageSize(object sender, EventArgs e) 
{ 
    PdfDocument document = new PdfDocument(); 
    document.PageSettings.Orientation = PdfPageOrientation.Landscape; 
    document.PageSettings.Size = PdfPageSize.A3; 
    var page = document.Pages.Add(); 
    var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, new PdfExportingOptions()); 
    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); 
    } 
} 
 
 
2. Margins not working 
You can set the margin for exported pdf document, please refer the below code example, 
 
Code Example 
private void ExportDataGridMargin(object sender, EventArgs e) 
{ 
    PdfDocument document = new PdfDocument(); 
    document.PageSettings.Margins.All = 20; 
    var page = document.Pages.Add(); 
    var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, new PdfExportingOptions()); 
    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); 
    } 
} 
 
 
3. Export to PDF, how can change the boarder of the box.. currently its gray color with dotted lines which want to change to black color thin line. 
We are little bit unclear about your query. Can you please provide the following details? 
 
·         Are you mentioning the SfDataGrid control’s outer border in PDF document? If yes, its already drawn with Gray and thin line.  
·         Share any additional details or screenshot about your query. 
 
4. If Any column in sfdatagrid resized to zero/hide, how can the same column exclude to pdf export ? 
You can exclude the hidden columns when export the SfDataGrid to Pdf document by adding the columns in PdfExportingOptions.ExcludeColumns collection, please refer the below code and UG link 
 
Code Example 
 
private void ExportDataGridExcludeColumns(object sender, EventArgs e) 
{ 
    sfDataGrid1.Columns[0].Visible = false; 
    PdfExportingOptions options = new PdfExportingOptions(); 
    foreach (var columns in sfDataGrid1.Columns) 
    { 
        if (!columns.Visible) 
            options.ExcludeColumns.Add(columns.MappingName); 
    } 
 
    var document = sfDataGrid1.ExportToPdf(options); 
    SaveFileDialog saveFileDialog = new SaveFileDialog 
    { 
        Filter = "PDF Files(*.pdf)|*.pdf" 
    }; 
    if (saveFileDialog.ShowDialog() == DialogResult.OK) 
    { 
        using (Stream stream = saveFileDialog.OpenFile()) 
            document.Save(stream); 
        if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
            System.Diagnostics.Process.Start(saveFileDialog.FileName); 
    }  
} 
 
 
 
  
Regards, 
Farjana Parveen A 



DE Deepak replied to Farjana Parveen Ayubb August 14, 2018 03:39 PM UTC

Hi Deepak, 
 
Thanks for contacting Syncfusion support. 
 
Please find the updated in the below table, 
 
Query 
Response 
1.Page size, page orientation is not working 
You can change the page orientation by get the exported PdfGrid by using ExportToPdfGrid method and then draw that PdfGrid into a PdfDocument by changing the PageSettings.Orientation property of PdfDocument. 
 
You can able to set the Page size Pdf document when exporting. Please refer the below code example 
 
Code Example 
 
private void ExportDataGridPageSize(object sender, EventArgs e) 
{ 
    PdfDocument document = new PdfDocument(); 
    document.PageSettings.Orientation = PdfPageOrientation.Landscape; 
    document.PageSettings.Size = PdfPageSize.A3; 
    var page = document.Pages.Add(); 
    var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, new PdfExportingOptions()); 
    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); 
    } 
} 
 
 
2. Margins not working 
You can set the margin for exported pdf document, please refer the below code example, 
 
Code Example 
private void ExportDataGridMargin(object sender, EventArgs e) 
{ 
    PdfDocument document = new PdfDocument(); 
    document.PageSettings.Margins.All = 20; 
    var page = document.Pages.Add(); 
    var PDFGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, new PdfExportingOptions()); 
    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); 
    } 
} 
 
 
3. Export to PDF, how can change the boarder of the box.. currently its gray color with dotted lines which want to change to black color thin line. 
We are little bit unclear about your query. Can you please provide the following details? 
 
·         Are you mentioning the SfDataGrid control’s outer border in PDF document? If yes, its already drawn with Gray and thin line.  
·         Share any additional details or screenshot about your query. 
 
4. If Any column in sfdatagrid resized to zero/hide, how can the same column exclude to pdf export ? 
You can exclude the hidden columns when export the SfDataGrid to Pdf document by adding the columns in PdfExportingOptions.ExcludeColumns collection, please refer the below code and UG link 
 
Code Example 
 
private void ExportDataGridExcludeColumns(object sender, EventArgs e) 
{ 
    sfDataGrid1.Columns[0].Visible = false; 
    PdfExportingOptions options = new PdfExportingOptions(); 
    foreach (var columns in sfDataGrid1.Columns) 
    { 
        if (!columns.Visible) 
            options.ExcludeColumns.Add(columns.MappingName); 
    } 
 
    var document = sfDataGrid1.ExportToPdf(options); 
    SaveFileDialog saveFileDialog = new SaveFileDialog 
    { 
        Filter = "PDF Files(*.pdf)|*.pdf" 
    }; 
    if (saveFileDialog.ShowDialog() == DialogResult.OK) 
    { 
        using (Stream stream = saveFileDialog.OpenFile()) 
            document.Save(stream); 
        if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
            System.Diagnostics.Process.Start(saveFileDialog.FileName); 
    }  
} 
 
 
 
  
Regards, 
Farjana Parveen A 


Hi,

Thanks for your fast reply.

More info about the queries

Query no. 3. How can I change color and thin line of SfDataGrid control’s outer border in PDF document which drawn with Gray and thin line ?

Query 4.  : If Columns mapping name is not given then How Can I exclude columns ?  As per your below suggestion there was mapping name.

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




AA Arulraj A Syncfusion Team August 16, 2018 11:42 AM UTC

Hi Deepak, 

Thanks for your update. 

Query 
Response 
How can I change color and thin line of SfDataGrid control’s outer border in PDF document which drawn with Gray and thin line ? 
You can achieve your requirement by drawing the border with the use of the PdfGridLayoutResult. Please make use of the following code. 

Code Example: 
PdfGridLayoutResult layoutResult = pdfGrid.Draw(page, new PointF()); 
layoutResult.Page.Graphics.DrawRectangle(PdfPens.Blue, layoutResult.Bounds); 
 
Sample Location: 
If Columns mapping name is not given then How Can I exclude columns ?  As per your below suggestion there was mapping name. 
MappingName for a column must be given for binding columns in SfDataGrid, otherwise default mapping names with column index will be initialized. So MappingName will not be empty at any point of execution, so you can exclude columns with its MappingName safely. 

Please let us know if you have any further queries. 

Regards, 



DE Deepak August 16, 2018 03:05 PM UTC

Hi,
Thanks for your reply.

1. SFDataGrid Control's outer border as per your sample is working, but in the same way inner boarder how can I change ?

2. Layout results all commands list required.. so that I can change my layout as per my requirements.

3. MappingName for a column must be given for binding columns in SfDataGrid, otherwise default mapping names with column index will be initialized. So MappingName will not be empty at any point of execution, so you can exclude columns with its MappingName safely. In my app, default mapping names are initialized, Is it Field name is the default mapping name ? How auto mapping names are initialized ?





AA Arulraj A Syncfusion Team August 17, 2018 01:04 PM UTC

Hi Deepak, 

Thanks for your update. 

Query 
Response 
SFDataGrid Control's outer border as per your sample is working, but in the same way inner boarder how can I change ? 
If you want to change the border color of the exported grid cell, you can use the CellExporting event. In which you can change the border color for the exported pdf cell. 

Code Sample: 
private void btnExportBorderColor_Click(object sender, EventArgs e) 
{ 
    PdfExportingOptions options = new PdfExportingOptions(); 
    options.CellExporting += OnCellExporting; 
    var document = sfDataGrid1.ExportToPdf(options); 
    SaveFileDialog saveFileDialog = new SaveFileDialog 
    { 
        Filter = "PDF Files(*.pdf)|*.pdf" 
    }; 
    if (saveFileDialog.ShowDialog() == DialogResult.OK) 
    { 
        using (Stream stream = saveFileDialog.OpenFile()) 
            document.Save(stream); 
        if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
            System.Diagnostics.Process.Start(saveFileDialog.FileName); 
    }  
} 
 
void OnCellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    //Set the border color for the pdf cell 
    e.PdfGridCell.Style.Borders.All = new PdfPen(Color.Blue); 
} 
 
Sample:  
Layout results all commands list required.. so that I can change my layout as per my requirements. 
Refer to the following API reference link for the PDFGridLayoutResult
In my app, default mapping names are initialized, Is it Field name is the default mapping name ? How auto mapping names are initialized ? 
If the AutoGenerateColumns is set to true, the SfDataGrid will create the columns with the property name as the mapping name. 

If you are binding the collection of entity object to the SfDataGrid. (eg: List<OrderInfo>). The columns will be generated for all the public properties with the property name as the mapping name.  

If you want to restrict some of the properties from the auto generating columns, set the Bindable or AutoGenerateFields attribute as false for that properties.  

Code Sample: 
sfDataGrid.DataSource = new List<OrderInfo>(); 
 
public class OrderInfo 
{ 
    /// <summary> 
    /// Column generated with the mapping name "ProductName" 
    /// </summary> 
    public string ProductName { get; set; } 
 
    /// <summary> 
    /// Column generated with the mapping name "ProductID" 
    /// </summary> 
    public int ProductID { get; set; } 
 
    /// <summary> 
    /// Column will not generated for this property, Since the Bindable attribute is set to false. 
    /// </summary> 
    [Bindable(false)] 
    public int CustomerID { get; set; } 
} 
 

UG Link: 



Arulraj A 



DE Deepak August 18, 2018 01:33 PM UTC

As per your below sample code, the null value exception error occurs when the sfgridcell contains null value from the database. What is the solution for null value error message ?



private void btnExportBorderColor_Click(object sender, EventArgs e) 
{ 
    PdfExportingOptions options = new PdfExportingOptions(); 
    options.CellExporting += OnCellExporting; 
    var document = sfDataGrid1.ExportToPdf(options); 
    SaveFileDialog saveFileDialog = new SaveFileDialog 
    { 
        Filter = "PDF Files(*.pdf)|*.pdf" 
    }; 
    if (saveFileDialog.ShowDialog() == DialogResult.OK) 
    { 
        using (Stream stream = saveFileDialog.OpenFile()) 
            document.Save(stream); 
        if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
            System.Diagnostics.Process.Start(saveFileDialog.FileName); 
    }  
} 
 
void OnCellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    //Set the border color for the pdf cell 
    e.PdfGridCell.Style.Borders.All = new PdfPen(Color.Blue); 
}


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

 
Thanks for your update. 
 
The NullReferenceException that occurs when SfDataGrid contains null values can be avoided by checking the CellExporting event’s DataGridCellPdfExportingEventArgs.CellValue property’s value equals to null and if so assigning an empty string to the cell value. 
Please refer to the following code example and sample from the given location. 
 
Code Example: 
void OnCellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    if (e.CellValue == null) 
        e.CellValue = string.Empty; 
 
    //Set the border color for the pdf cell  
    e.PdfGridCell.Style.Borders.All = new PdfPen(Color.Blue); 
} 
 
 
 
Regards, 
Arulraj A 



DE Deepak August 25, 2018 11:01 AM UTC

Hi,

As per your suggestion  for cell boarder given below code which is working, but not satisfied with the requirements.

If I am not using     e.PdfGridCell.Style.Borders.All = new PdfPen(Color.Brown); command, then it is showing boarder in gray color but with 14 Records in one page(refer pdf-color-11.pdf), and when given the said command, the row size increase due to which getting only 12 records in page (refer pdf-color-10.pdf). For your reference PFA the said two Sample PDF File.

Question : Without changing in width and height of record, how can Change the border color ?

currently by default it is light gray color which when we print, the border is not visible.

  void OnCellExporting(object sender, DataGridCellPdfExportingEventArgs e)
        {

            if (e.CellValue == null)
                e.CellValue = string.Empty;
              e.PdfGridCell.Style.Borders.All = new PdfPen(Color.Black);
        }
for

Attachment: pdfcolors_8779b696.zip


AA Arulraj A Syncfusion Team August 27, 2018 07:25 AM UTC

Hi Deepak, 

Thanks for your update. 

The difference in the row height occurs due to the thickness of the border line. This can be reduced by changing the width of the PdfPen for drawing the PdfGridCell borders. Please refer to the below code example and sample from the given location. 

Code Example : 
void OnCellExporting(object sender, DataGridCellPdfExportingEventArgs e) 
{ 
    if (e.CellValue == null) 
        e.CellValue = string.Empty; 
 
    //Set the border color for the pdf cell  
    e.PdfGridCell.Style.Borders.All = new PdfPen(Color.Black, 0.2f); 
} 
 


Arulraj A 


Loader.
Up arrow icon