0

我有这个问题 iTextsharp 没有呈现以下控制器 asp:Literal & asp:DataList 有人可以帮我解决这个问题页面标记:

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pdfPrintPage.aspx.cs" EnableEventValidation="false" Inherits="Compudata_ProjectManager.pdfPrintPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
            .style1
            {
                width: 100%;
            }
        </style>
    </head>
    <body>
         <form id="form1" runat="server">
     <asp:PlaceHolder ID="ph_pdfPrint" runat="server">
    <div>
            &nbsp;<table class="style1">
                <tr>
                    <td>
                        Task ID: #<asp:Label ID="lbl_taskID" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Created on:
                        <asp:Label ID="lbl_CreatedOn" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Defualt Tech:
                        <asp:Label ID="lbl_DefultTech" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Status:
                        <asp:Label ID="lbl_Status" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Priority:
                        <asp:Label ID="lbl_priorty" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Est. Time :
                        <asp:Label ID="lbl_estTime" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Assigned to :
                        <asp:ListBox ID="lb_AssignedTo" runat="server"></asp:ListBox>

                    </td>
                </tr>
                <tr>
                    <td>
                        Default Tech: <asp:Label ID="lbl_DefaultTech" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Description :
                    </td>
                </tr>
                            <tr>
                    <td>
                       <asp:Literal ID="lit_description" runat="server"></asp:Literal>
                    </td>
                </tr>
            </table>
        </div>
            <h4>Entry</h4>
        <asp:DataList ID="DataList1" runat="server" DataKeyField="billingID" 
            >
            <ItemTemplate>
                billingID:
                <asp:Label ID="billingIDLabel" runat="server" Text='<%# Eval("billingID") %>' />
                <br />
                User Name:
                <asp:Label ID="User_NameLabel" runat="server" 
                    Text='<%# Eval("[User Name]") %>' />
                <br />
                taskID:
                <asp:Label ID="taskIDLabel" runat="server" Text='<%# Eval("taskID") %>' />
                <br />
                actualTotalTime:
                <asp:Label ID="actualTotalTimeLabel" runat="server" 
                    Text='<%# Eval("actualTotalTime") %>' />
                <br />
                createdOn:
                <asp:Label ID="createdOnLabel" runat="server" Text='<%# Eval("createdOn") %>' />
                <br />
                billableTypeID:
                <asp:Label ID="billableTypeIDLabel" runat="server" 
                    Text='<%# Eval("billableTypeID") %>' />
                <br />
                billingNumber:
                <asp:Label ID="billingNumberLabel" runat="server" 
                    Text='<%# Eval("billingNumber") %>' />
                <br />
                summary:
                <asp:Label ID="summaryLabel" runat="server" Text='<%# Eval("summary") %>' />
                <br />
                isCompleted:
                <asp:Label ID="isCompletedLabel" runat="server" 
                    Text='<%# Eval("isCompleted") %>' />
                <br />
                note:
                <asp:Label ID="noteLabel" runat="server" Text='<%# Eval("note") %>' />
    <br />

                dateOfService:
                <asp:Label ID="dateOfServiceLabel" runat="server" 
                    Text='<%# Eval("dateOfService") %>' />
                <br />
                <br />

            </ItemTemplate>
        </asp:DataList>




          </asp:PlaceHolder>                 
    <asp:SqlDataSource ID="assignedToUserList" runat="server" ConnectionString="<%$ ConnectionStrings:Compudata_ProjectManagerConnection %>"
                            SelectCommand="GetAllAssignedUserToTask" SelectCommandType="StoredProcedure">
                            <SelectParameters>
                                <asp:QueryStringParameter Name="taskID" QueryStringField="taskID" Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                            <asp:SqlDataSource ID="SqlDS_TaskBilling" runat="server" SelectCommand="GetAllbillingsTimesForTask"
                SelectCommandType="StoredProcedure" 
            ConnectionString="<%$ ConnectionStrings:Compudata_ProjectManagerConnection %>">
            <SelectParameters>
                <asp:QueryStringParameter DbType="Int32" Name="TaskID" 
                    QueryStringField="taskID" />
            </SelectParameters>
        </asp:SqlDataSource>
        </form>

    </body>
    </html>

C# code 


 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
    using System.Drawing;
    using Compudata_ProjectManager.CodeFile.BOL;

    namespace Compudata_ProjectManager
    {
        public partial class pdfPrintPage : System.Web.UI.Page
        {
           protected void Page_Load(object sender, EventArgs e)
            {
                int taskID = Convert.ToInt32(Request.QueryString["taskID"]);
                populatePage(taskID);

                bindListBox();
                bindDataList();
                System.Threading.Thread.Sleep(3000);
                PDF_Export();
            }

           private void bindDataList()
           {
               DataList1.DataSource = SqlDS_TaskBilling;
               DataList1.DataBind();
           }

           private void bindListBox()
           {
               lb_AssignedTo.DataSource = assignedToUserList;
               lb_AssignedTo.DataTextField = "UserName";
               lb_AssignedTo.DataValueField = "UserId";
               lb_AssignedTo.DataBind();
           }

            private void populatePage(int taskID )
            {
                Task task = Task.GetTaskDetail(taskID);
                lbl_taskID.Text = task.TaskID.ToString();
                lbl_CreatedOn.Text = task.CreatedOn.ToString();
                lbl_DefultTech.Text = task.DefaultTeck;
                lbl_Status.Text = task.Status.ToString();
                lbl_priorty.Text = task.Priority.ToString();
                lbl_estTime.Text = task.EstTime.ToString();
                lbl_DefaultTech.Text = task.DefaultTeck.ToString();
                lit_description.Text = task.Description.Replace("\r\n", "<br/>").ToString();
            }

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[4].Text);
                    e.Row.Cells[4].Text = decodedText;

                }
            }



            private void PDF_Export()
            {
                int taskID = Convert.ToInt32(Request.QueryString["taskID"]);
                Task task = Task.GetTaskDetail(taskID);
                //Set content type in response stream
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=" + task.TasKName + ".pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //Render PlaceHolder to temporary stream
                System.IO.StringWriter stringWrite = new StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                ph_pdfPrint.RenderControl(htmlWrite);
                StringReader reader = new StringReader(stringWrite.ToString());
                //Create PDF document
                Document doc = new Document(PageSize.A4);
                HTMLWorker parser = new HTMLWorker(doc);
                PdfWriter.GetInstance(doc, Response.OutputStream);
                doc.Open();
                try
                {
                    ////Create a footer that will display page number
                    parser.Parse(reader);
                }
                catch (Exception ex)
                {
                    //Display parser errors in PDF.
                    //Parser errors will also be wisible in Debug.Output window in VS
                    Paragraph paragraph = new Paragraph("Error! " + ex.Message);
                    //paragraph.SetAlignment("center");
                    Chunk text = paragraph.Chunks[0] as Chunk;
                    doc.Add(paragraph);
                }
                finally
                {
                    doc.Close();
                    Response.Write(doc);
                }

            }

            public override void VerifyRenderingInServerForm(Control control)
            {
            }
        }
    }
4

2 回答 2

1

只要找到答案。我必须添加这 2 行

    lit_description.RenderControl(htmlWrite);
    DataList1.RenderControl(htmlWrite); 
于 2013-01-22T20:06:28.450 回答
-1

对于遇到相同问题的其他人,请记住,当使用 HTMLworker 尝试解析包含<hr>标签的 HTML 时,iTextSharp 会引发空异常。

于 2013-08-23T09:23:39.543 回答