Export PDF Column Width

10 posts, 1 answers
  1. Sean
    Sean avatar
    17 posts
    Member since:
    May 2006

    Posted 10 Feb 2009 Link to this post

    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



  2. Answer
    Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 13 Feb 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Phil
    Phil avatar
    9 posts
    Member since:
    Jul 2006

    Posted 19 Feb 2009 Link to this post

    Worked for me... Thanks!
  5. Sean
    Sean avatar
    17 posts
    Member since:
    May 2006

    Posted 19 Feb 2009 Link to this post

    Sorry for not replying sooner,

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

    Thanks
    Sean
  6. Darius
    Darius avatar
    23 posts
    Member since:
    May 2008

    Posted 21 Aug 2009 Link to this post

    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?
  7. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 21 Aug 2009 Link to this post

    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.
  8. Darius
    Darius avatar
    23 posts
    Member since:
    May 2008

    Posted 24 Aug 2009 Link to this post

    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?
  9. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 24 Aug 2009 Link to this post

    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.
  10. Darius
    Darius avatar
    23 posts
    Member since:
    May 2008

    Posted 24 Aug 2009 Link to this post

    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.
  11. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 27 Aug 2009 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017