How to Resize Columns to Fit in a Page while Exporting to PDF

Dear Sir,

Following Code is not working..

Unable to get
currentPercent

Please send me sample file for the same




GridPDFConverter
pdfConvertor = new GridPDFConverter(); //Range of rows to be exported in a PDF file pdfConvertor.ExportRange = 40; //Merged Export. pdfConvertor.ExportToPdfWithMerge(ref doc, this.gridGroupingControl1.TableControl ); //Resize the column to fit in PDF. float tempPercent = currentPercent; this.gridGroupingControl1.BeginUpdate(); PdfDocument doc = new PdfDocument(); Hashtable ht = new Hashtable(); for (int i = 0; i < gridGroupingControl1.TableDescriptor.Columns.Count; i++) ht.Add(i, gridGroupingControl1.TableDescriptor.Columns[i].Width) float gridWidth = this.gridGroupingControl1.TableModel.ColWidths.GetTotal(0, gridGroupingControl1.TableDescriptor.Columns.Count); if (gridWidth > doc.PageSettings.Width + 80) { float scale = (gridWidth / (doc.PageSettings.Width + 80)) - currentPercent; zoomGrid(currentPercent - scale); }

3 Replies

MG Mohanraj Gunasekaran Syncfusion Team July 30, 2018 12:19 PM UTC

Hi Jignesh, 
  
Thank you for using Syncfusion products.  
  
Suggestion1 
By default, GridGroupingControl does not have direct support to adjust the page width while exporting  the grid to PDF. In order to resize the column width of the grid while exporting the PDF, Exporting event can be used. In that event , you can zoom the grid while exporting after that your reset the grid. Please refer to the below code example, 
  
Code example 
GridPDFConverter pdfConvertor = new GridPDFConverter(); 
PdfDocument doc = new PdfDocument(); 
//Range of rows to be exported in a PDF file 
pdfConvertor.ExportRange = 40;  
//Merged Export. 
pdfConvertor.Exporting += PdfConvertor_Exporting; 
pdfConvertor.ExportToPdfWithMerge(ref doc, this.gridGroupingControl1.TableControl); 
//Reset the zoomGrid 
zoom.zoomGrid(currentPercent.ToString()); 
  
private void PdfConvertor_Exporting(object sender, PDFExportingEventArgs e) 
{ 
    this.gridGroupingControl1.BeginUpdate(); 
    PdfDocument doc = e.PdfDocument; 
    Hashtable ht = new Hashtable(); 
    for (int i = 0; i < gridGroupingControl1.TableDescriptor.Columns.Count; i++) 
        ht.Add(i, gridGroupingControl1.TableDescriptor.Columns[i].Width); 
    float gridWidth = this.gridGroupingControl1.TableModel.ColWidths.GetTotal(0, gridGroupingControl1.TableDescriptor.Columns.Count); 
    if (gridWidth > doc.PageSettings.Width + 80) 
    { 
        float scale = (gridWidth / (doc.PageSettings.Width+80)); 
        zoomGrid(currentPercent - scale); 
    } 
} 
  
private void zoomGrid(float percent) 
{ 
    ZoomGroupingGrid zoom = new ZoomGroupingGrid(this.gridGroupingControl1); 
    zoom.zoomGrid(percent.ToString()); 
  
} 
  
Sample link: GridGroupingControl 
  
Suggestion2: 
Also, you can customize the page width by using any of the following methods,  
                                              1. Use Width property of PDFDocument   
                                              2. Set the Orientation property as Landscape.   
  
Please make use of below code and refer to the attached sample.  
  
Code snippet 
void pdfConvertor_Exporting(object sender, PDFExportingEventArgs e)  
{  
     //You can use any of the followings  
     e.PdfDocument.PageSettings.Width = this.gridGroupingControl1.TableModel.ColWidths.GetTotal(0,this.gridGroupingControl1.TableModel.ColCount);  
  
     e.PdfDocument.PageSettings.Orientation = Syncfusion.Pdf.PdfPageOrientation.Landscape;   
}  
  
  
  
Sample link:   
  
Regards,  
Mohanraj G 



JI Jignesh August 1, 2018 07:33 AM UTC

Dear sir,

we need solution for following :
1) while exporting grid to pdf in A4 size either in Landscape or Portrait,
the width should be fix & same size and all columns of the grid should appear within page .

2) while exporting grid to pdf, the selected row color and the other row color should be same.


MG Mohanraj Gunasekaran Syncfusion Team August 3, 2018 01:07 PM UTC

Hi Jignesh,  
  
Thanks for your update.  
  
Query  
Solution  
while exporting grid to pdf in A4 size either in Landscape or Portrait,   
the width should be fix & same size and all columns of the grid should appear within page .  
To fit all the columns exactly in a page, you could manually calculate the column width based on the PdfDocument page width then you can assign that calculate width using PrintingMode property in QueryColWidth event. Please refer to the below code example and the sample,  
 
Code example  
GridPDFConverter pdfConvertor = new GridPDFConverter();  
PdfDocument pdfDoc = new PdfDocument();  
rowHeaderWidth = this.PointToPixel(this.gridGroupingControl1.TableModel.ColWidths[0]);  
this.CalculateColumnWidth(pdfDoc);  
pdfConvertor.ExportToPdf(pdfDoc, this.gridGroupingControl1.TableControl);  
pdfDoc.Save("Sample.pdf");  
  
private void CalculateColumnWidth(PdfDocument pdfDoc)  
{  
    int totalColumnWidth = 0;  
    float gridWidth = this.gridGroupingControl1.TableModel.ColWidths.GetTotal(1, gridGroupingControl1.TableDescriptor.Columns.Count);  
    //Convert the pdf document width in pixel format  
    int documentWidthPixel = this.PointToPixel(pdfDoc.PageSettings.Width);  
    widthDifferenciation = (int)(((documentWidthPixel - rowHeaderWidth) - gridWidth) / gridGroupingControl1.TableDescriptor.Columns.Count);  
    for (int i = 0; i < gridGroupingControl1.TableDescriptor.Columns.Count; i++)  
        totalColumnWidth += this.gridGroupingControl1.TableDescriptor.Columns[i].Width + widthDifferenciation;  
  
    lastColumnWidth = documentWidthPixel - rowHeaderWidth - totalColumnWidth;  
}  
  
this.gridGroupingControl1.TableModel.QueryColWidth += TableModel_QueryColWidth;  
private void TableModel_QueryColWidth(object sender, GridRowColSizeEventArgs e)  
{  
    if (this.gridGroupingControl1.TableControl.PrintingMode)  
    {  
        if (e.Index == 0)  
        {  
            e.Size = rowHeaderWidth;  
        }  
        else if (e.Index > 0 && e.Index < this.gridGroupingControl1.TableModel.ColCount - 1)  
        {  
            if (e.Index != this.gridGroupingControl1.TableModel.ColCount - 2)  
            {  
                e.Size = e.Size + widthDifferenciation;  
            }  
            else  
            {  
                e.Size += widthDifferenciation + lastColumnWidth;  
            }  
        }  
    }  
}  
  
while exporting grid to pdf, the selected row color and the other row color should be same.  
To avoid the selection color while printing the grid to pdf, you could handle the PrepareViewStyleInfo event. Please refer to the below code example,   
   
Code example   
this.gridGroupingControl1.TableControl.PrepareViewStyleInfo += GridGroupingControl1_TableControlPrepareViewStyleInfo;   
   
private void GridGroupingControl1_TableControlPrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)   
{   
    if (this.gridGroupingControl1.TableControl.PrintingMode)   
    {   
        GridTableCellStyleInfo style = e.Style as GridTableCellStyleInfo;   
        if (style != null && style.TableCellIdentity.DisplayElement.Kind == Syncfusion.Grouping.DisplayElementKind.Record && style.TableCellIdentity.DisplayElement.GetRecord().IsSelected()   
            && (style.TableCellIdentity.TableCellType != GridTableCellType.RecordRowHeaderCell && style.TableCellIdentity.TableCellType != GridTableCellType.AlternateRecordRowHeaderCell))   
        {   
            e.Cancel = true;   
        }   
    }   
}   
 
Sample link: GridGroupingControl  
 
Regards,  
Mohanraj G  
 


Loader.
Up arrow icon