Printing a Document in C#

Hi,

Can anyone help me out with this? I am trying to find a method to print documents which are in the following formats:

  • .doc
  • .docx
  • .pdf
  • .rtf

I have a GridView with a series of CheckBoxes like so:

http://kidsunlimited.co.uk/images/print.jpg

You can see the Print checkBoxes, i have a button at the bottom of the page and when this is clicked i need to check to see which rows are checked and the print the “CV” documents. The CV documents are in the formats i have described above.

Just to show you my front end code, i have this:


<asp:GridView ID="GridView1" runat="server" CellPadding="10" 
        ForeColor="#333333" AllowPaging="True" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        PageSize="30" cssclass="GridViewStyle" 
        onrowdatabound="GridView1_RowDataBound">
        <FooterStyle CssClass="GridViewFooterStyle" />
        <RowStyle CssClass="GridViewRowStyle" />   
        <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
        <PagerStyle CssClass="GridViewPagerStyle" />
        <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
        <HeaderStyle CssClass="GridViewHeaderStyle" />
        <Columns>
            <asp:BoundField DataField="application_id" HeaderText="application_id" InsertVisible="False" ReadOnly="True" SortExpression="application_id" Visible="False" />
            <asp:BoundField DataField="fname" HeaderText="Forename" SortExpression="fname" />
            <asp:BoundField DataField="sname" HeaderText="Surname" SortExpression="sname" />
            <asp:BoundField DataField="cv" HeaderText="CV" SortExpression="cv" ItemStyle-CssClass="cvclass"/>
            <asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
            <asp:BoundField DataField="date_addedd" HeaderText="Date" SortExpression="date_addedd" ItemStyle-CssClass="dateclass" DataFormatString="{0:U}" HtmlEncode="false"/>
                
                <asp:TemplateField HeaderText="Print">
                    <ItemStyle HorizontalAlign="Center" />
                        <ItemTemplate>
                           <asp:CheckBox ID="ApplSelector" runat="server" />
                        </ItemTemplate>
                </asp:TemplateField>    
                            
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center" CssClass="viewclass"/>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" Text='View application'></asp:HyperLink>
                        </ItemTemplate>
                </asp:TemplateField>
                
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center"/>
                        <ItemTemplate>
                         <a id="btnShowPopup" runat="server" class="thickbox" href='<&#37;# Eval("application_id", "edit-more-app2.aspx?AID={0}&TB_iframe=true&height=60&width=400") %>'>Status</a>                
                        </ItemTemplate>
                </asp:TemplateField>
                
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center"/>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink3" runat="server" Text='Delete'></asp:HyperLink>
                        </ItemTemplate>
                </asp:TemplateField>
        </Columns>
    </asp:GridView>

     <asp:Button ID="PrintCVApplications" runat="server" Text="Print Applications" onclick="PrintCVApplications_Click" /> 
     
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SQL2008_673062_kidsConnectionString %>" 
        SelectCommand="SELECT [application_id], [fname], [sname], [cv], [status], [date_addedd] FROM [tbl_apprenticeship_applications]">
    </asp:SqlDataSource>

So i need to access the ‘cv’ row, and print out the relevant documents according to which checkbox is checked.

I assume i would need to loop through the GridView:


    protected void PrintCVApplications_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
              //Print code here...
            }
        }
    }

But i don’t know how to go ahead and print??

Can anyone possible help me figure this out? Or show any tutorials, examples?

Would really appreciate it.

Thanks again

http://forums.asp.net/p/1561011/3861081.aspx

A simple search will solve most of your problems.

Good Luck

Thanks,

I have had a look around and have managed to get to this:


    protected void PrintCVApplications_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                string file = Server.MapPath("~/applications/") + (row.DataItem as DataRowView).Row["cv"].ToString();

                string strHTML = file.ToString();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Write(strHTML);
                HttpContext.Current.Response.Write("<script>window.print();</script>");
                HttpContext.Current.Response.End();
            }
        }       
    }

Now i get an error here:

Object reference not set to an instance of an object.

I think this is because i am not looping thorough to check which CheckBox is checked…

I was thinking i need to add this somewhere:


CheckBox cb = (CheckBox)row.FindControl("ApplSelector");

And somehow include this in the for loop?

Can anyone help?

Thanks again