ExportToPDF Formatting Ignored

9 posts, 3 answers
  1. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 19 Aug 2009 Link to this post

    Hey another quandary with the ExportToPDF method.
    I've been going though the examples and forums learning how to apply some styling to the PDF as it's being exported. Currently I'm able to apply the styles I want for the most part, however whenever I set the 'ignorePaging' setting to True on the export settings, it also seems to ignore my applied styles and also truncates the last column from even being shown.

    Here's my export settings:
    <ExportSettings Pdf-PageWidth="350mm" Pdf-PaperSize="C5_Envelope" IgnorePaging="true" ExportOnlyData="true"></ExportSettings> 

    and here's how I apply my styles (via a ASP button click event):

    protected void btnExport_Click(object sender, EventArgs e) 
            {      
                ApplyPDFStyles(); 
                RadGrid1.MasterTableView.ExportToPdf();    
            } 
     protected void ApplyPDFStyles() 
            { 
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
                { 
                    if (col.UniqueName == "exCol"
                    { 
                        col.Visible = false//this is a manual expand/collapse collumn I'm hiding. 
                    } 
                } 
                GridItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0]; 
                headerItem.Style["horizaontal-align"] = "left"
                headerItem.Style["background-color"] = "#777"
                RadGrid1.MasterTableView.GetColumn("ItemNumber").HeaderStyle.Width = Unit.Pixel(35); 
                RadGrid1.MasterTableView.GetColumn("ItemNumber").HeaderStyle.HorizontalAlign = HorizontalAlign.Left; 
                RadGrid1.MasterTableView.GetColumn("COURSE_ID").HeaderStyle.Width = Unit.Pixel(70); 
                RadGrid1.MasterTableView.GetColumn("COURSE_ID").HeaderStyle.HorizontalAlign = HorizontalAlign.Left; 
                RadGrid1.MasterTableView.GetColumn("COURSE_TITLE").HeaderStyle.Width = Unit.Pixel(130); 
     
    // 
    // many more column styles removed for simplicity...these all apply when ignorePaging is set to false 
    // 
     
                foreach (TableCell cell in headerItem.Cells) 
                { 
                    cell.Style["text-align"] = "left"
                } 
            } 

  2. Answer
    Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 20 Aug 2009 Link to this post

    Hello Adam,

    Actually your styles aren't ignored, they just aren't persisted because the IgnorePaging setting will cause RadGrid to rebind itself before export. In such cases, a better place to apply styles would be the ItemCreated event as demonstrated in the following example:
    Export to PDF

    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. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 20 Aug 2009 Link to this post

    Hum, I did actually see the examples already along with the Tips and Tricks document, but even trying it that way didn't seem to fix the problem. However I might be doing something incorrectly, here is the version I have using the ItemCreated event-

    protected void btnExport_Click(object sender, EventArgs e) 
            { 
                isExport = true
                RadGrid1.MasterTableView.ExportToPdf(); 
                isExport = false
            } 
    protected void ApplyPDFStyles() 
            { 
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
                { 
                    if (col.UniqueName == "exCol"
                    { 
                        col.Visible = false//this is a manual expand/collapse collumn I'm hiding. 
                    } 
                } 
                GridItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0]; 
                headerItem.Style["horizaontal-align"] = "left"
                headerItem.Style["background-color"] = "#777"
                RadGrid1.MasterTableView.GetColumn("ItemNumber").HeaderStyle.Width = Unit.Pixel(35); 
                ///ect ect 
     protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
            { 
                if (isExport) 
                { 
                    ApplyPDFStyles(); 
                } 
            } 

    I'm doing it slightly different than verbatim from the examples, I realize it's probably inefficient to apply all the styles over again on every item creation but for the sake of simplicity it think it should technically work...

    However, the above code doesn't fix the problem... Any advice?

  5. Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 21 Aug 2009 Link to this post

    Hello Adam,

    I created a sample project based on your code-snippet. Please test it on your end and let me know if you need further assistance.

    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.
  6. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 21 Aug 2009 Link to this post

    Thanks for the help Daniel,
    The methodology in the sample worked great for applying the styles. The only remaining issue is that the exported PDF seems to still cut off my last column.  If you modify the sample you gave me to include say 12 columns you can see the same thing happen.-

    protected void Page_Load(object o, EventArgs e) 
        { 
            DataTable table = new DataTable(); 
            table.Columns.Add("ItemNumber"typeof(int)); 
            table.Columns.Add("Other"); 
            table.Columns.Add("Other1"); 
            table.Columns.Add("Other2"); 
            table.Columns.Add("Other3"); 
            table.Columns.Add("Other4"); 
            table.Columns.Add("Other5"); 
            table.Columns.Add("Other6"); 
            table.Columns.Add("Other7"); 
            table.Columns.Add("Other8"); 
            table.Columns.Add("Other9"); 
            table.Columns.Add("Other10"); 
            table.Columns.Add("Other11"); 
            table.Columns.Add("Other12"); 
            table.Columns.Add("Other13"); 
            for (int i = 0; i < 10; i++) 
                table.Rows.Add(i, "Something"); 
            RadGrid1.DataSource = table; 
        } 
    <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCreated="RadGrid1_ItemCreated"
                <ExportSettings Pdf-PageWidth="350mm" Pdf-PaperSize="C5_Envelope" ExportOnlyData="true" IgnorePaging="true" /> 
                <MasterTableView AutoGenerateColumns="false"
                    <Columns> 
                        <telerik:GridExpandColumn UniqueName="exCol" /> 
                        <telerik:GridBoundColumn DataField="ItemNumber" HeaderText="ItemNumber" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other1" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other2" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other3" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other4" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other5" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other6" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other7" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other8" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other9" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other10" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other11" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other12" HeaderText="Other" /> 
                        <telerik:GridBoundColumn HeaderStyle-Width="90px" DataField="Other13" HeaderText="Other" /> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 

    Edit: Additionally, Some items have text longer than the width allowed. I want them to wrap to the next line but currently they bleed into the next cell. This appears to only happen when the text doesn't have any spaces in it, otherwise it will in fact auto-wrap according to the header width.
  7. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 24 Aug 2009 Link to this post

    Self bump here. I'm about out of ideas on my end, I've tried adjusting the header widths and page size to no avail. Another oddity is that changing the header-width for a column seems to actually affect the column before it...If I change the width for the "Other" column via code , it changes the width of the "ItemNumber" column on the exported PDF.
  8. Answer
    Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 27 Aug 2009 Link to this post

    Hello Adam,

    As usually, I attached another demo to this thread. Please notice the following:
    - the width of the exported page is not equal to the width of your screen, so hardcoding the columns' widths may result in unexpected behavior
    - as you already noticed, text that doesn't contain an empty space won't be wrapped

    Best 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.
  9. Answer
    Dessie Lunsford
    Dessie Lunsford avatar
    10 posts
    Member since:
    Oct 2004

    Posted 27 Aug 2009 Link to this post

    Thanks you for the help Daniel, the examples you provided helped me to solve the issue.
    In addition to the problem fixed by using your examples, I would like to note for future users having this issue that in my case, some of the problems stemmed from a Grid_DataBound event handler that I had in the project but forgot to include and associate with the issue at hand. The event handler was set up to put a custom Expand/Collapse column in the grid and was being triggered when export method was called.
    The revised code below:
     protected void RadGrid1_DataBound(object sender, EventArgs e) 
            { 
                if (isExport) 
                    return
                RadGrid1.MasterTableView.GetColumn("ExpandColumn").Display = false
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
                { 
                    if (col.UniqueName == "exCol"
                    { 
                        foreach (GridHeaderItem headeritem in RadGrid1.MasterTableView.GetItems(GridItemType.Header)) 
                        { 
                            headeritem["exCol"].Text = "<a href='javascript: ExpandCollapseAllTables()' id=\"btnEx\">Expand</a>"
                        } 
                    } 
                } 
            } 
    includes the a check (if export then return) to make sure this is not triggered on export. The hiding of both the default and custom expand/collapse column is handled in the ApplyColumnSettings() method as Daniel exemplified in the first attachment. -
    case "exCol": col.Visible = falsebreak
    case "ExpandColumn": col.Visible = falsebreak

    Thanks again!
  10. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 27 Aug 2009 Link to this post

    The previous post was me on the wrong account o.O. Sorry for the confusion.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017