This is a migrated thread and some comments may be shown as answers.

Export PDF Column Width

9 Answers 703 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 11 Feb 2009, 04:56 AM
Hi All,

Hopefully someone can put me out of my misery with this one:

Basically we are trying to format a RadGrid to export to PDF, we have followed the export tips and tricks and everything else is working correctly.  The only thing we do want to be able to do is make sure that the first column is a bit wider,  we have tried a few different ways of adding a width and it seems that all of the ways we try get ignored.

So if anyone has successfully modified the width of a column for PDF export please let me know and if possible post some code showing it working.

Thanks heaps

Sean



9 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 13 Feb 2009, 07:05 PM
Hello Sean,

You can set the HeaderItem style in the declaration:
<telerik:GridBoundColumn> 
    <HeaderStyle Width="100px" /> 

or in code-behind:
RadGrid1.MasterTableView.GetColumn("yourColumn").HeaderStyle.Width = Unit.Pixel(200) 

Let us know whether this helps

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Phil
Top achievements
Rank 1
answered on 19 Feb 2009, 05:28 PM
Worked for me... Thanks!
0
Sean
Top achievements
Rank 1
answered on 19 Feb 2009, 11:13 PM
Sorry for not replying sooner,

This worked a treat for us, the mistake we were making was not to set Unit.Pixel.

Thanks
Sean
0
Darius
Top achievements
Rank 1
answered on 21 Aug 2009, 03:12 PM
Hello, this works but it also sets columns to predefined widths. The problem here is that since I don't know the screen size, I want the screen display to be dynamic (i.e. the sizes not set), and since I do know the PDF page size I want the column widths to be set when they are being exported. Can this be accomplished?
0
Daniel
Telerik team
answered on 21 Aug 2009, 03:39 PM
Hello Darius,

You can set the column widths at any point. This approach may be suitable for your scenario:
protected void btnExport_Click(object sender, EventArgs e) 
    DefineColumnWidths(); 
    RadGrid1.MasterTableView.ExportToPdf(); 

protected void DefineColumnWidths() 
    foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
    { 
        switch (col.UniqueName) 
        { 
            case "Column1": col.HeaderStyle.Width = Unit.Pixel(35); break
            case "Column2": col.HeaderStyle.Width = Unit.Pixel(90); break
            case "Column3": col.HeaderStyle.Width = Unit.Pixel(200); break
        } 
    } 

Notice that this won't affect the actual page but only the exported file.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Darius
Top achievements
Rank 1
answered on 24 Aug 2009, 06:33 PM
Thanks. This worked. However, I have one more issue. I'd like my columns to be left-justified. They actually are in design mode. But in the exported PDF they are centered. I thought I could fix it with this code, but it doesn't seem to work:

        For Each colTmp As Telerik.Web.UI.GridColumn In RadGrid1.MasterTableView.RenderColumns  
            colTmp.HeaderStyle.HorizontalAlign = HorizontalAlign.Left 
 Ideas?
0
Daniel
Telerik team
answered on 24 Aug 2009, 08:06 PM
Hello Darius,

Such styles should be applied directly to the corresponding table element. The following code-snippet demonstrate how to traverse all cells in the header item to set some CSS styles.
foreach (TableCell cell in e.Item.Cells) 
    cell.Style["text-align"] = "center"
    cell.Style["font-size"] = "16px"

Our online Export to PDF demo illustrate this approach:
Export to PDF

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Darius
Top achievements
Rank 1
answered on 24 Aug 2009, 08:18 PM
OK, two things.

One, the demo sets the properties in RadGrid1_ItemCreated. Wouldn't this change the appearance of the grid in general and not just of the exported PDF?

Two, why doesn't the export honor the horizontal alignment of headers? Mine are left-justified and they change to centered in the PDF.

Thanks.
0
Daniel
Telerik team
answered on 27 Aug 2009, 04:34 PM
Hello Darius,

1) Yes, it would affect your RadGrid. But in this particular case it just duplicate the styles and thus the user don't feel any difference. You could use the following approach to customize the appearance only when exporting:
bool isExport = false
protected void Button1_Click(object sender, EventArgs e) 
    isExport = true
    RadGrid1.ExportSettings.IgnorePaging = true
    RadGrid1.MasterTableView.ExportToPdf(); 

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    if (isExport) ApplyStyles(e.Item); 

private void ApplyStyles(GridItem item) 
    if (item is GridDataItem)     item.Style["color"] = "#050599"
    if (item is GridHeaderItem)   item.Style["color"] = "#990505"

2) PDF export engine respects the horizontal align only when it is applied on the TH element.

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Sean
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Phil
Top achievements
Rank 1
Sean
Top achievements
Rank 1
Darius
Top achievements
Rank 1
Share this question
or