2011-03-02 121 views
0

我一直試圖讓這個工作很長一段時間,現在我希望有人能幫我解決這個問題。我試圖從我的ASCX控件(如下所示合併到我的ASPX頁面)中保存兩個ListView到一個PDF文件。我相信問題在於這兩行代碼,因爲我在保存時遇到'文檔沒有頁面'錯誤。任何想法將不勝感激!在此先感謝...iTextSharp輸出爲PDF

ListView lv1 = (ListView)TagCloudControl1.FindControl("ListView1"); 
ListView lv2 = (ListView)TagCloudControl1.FindControl("ListView2"); 

ASPX頁面:

<%@ Register Src="~/tagcloud.ascx" TagName="TagCloudControl" TagPrefix="TagCloud" %> 
... 
<TagCloud:TagCloudControl ID="TagCloudControl1" runat="server" /> 

C#:

private void GeneratePDF(string path, string fileName, bool download, string text) { 

    var document = new Document(); 

    try{ 

     if (download) { 

      PdfWriter.GetInstance(document, Response.OutputStream); 

     } else { 

      PdfWriter.GetInstance(document, new FileStream(path + fileName, FileMode.Create)); 
     } 

     StringBuilder strB = new StringBuilder(); 
     document.Open(); 

     if (text.Length.Equals(0)) { 

      TagCloudControl1.BindTagCloud(); 
      using (StringWriter sWriter = new StringWriter(strB)) { 

       using (HtmlTextWriter htWriter = new HtmlTextWriter(sWriter)) { 

        //var lv1 = (TagCloudControl)ListView.FindControl("ListView1"); 
        //var lv2 = (TagCloudControl)ListView.FindControl("ListView2"); 

        ListView lv1 = (ListView)TagCloudControl1.FindControl("ListView1"); 
        ListView lv2 = (ListView)TagCloudControl1.FindControl("ListView2"); 

        lv1.RenderControl(htWriter); 
        lv2.RenderControl(htWriter); 
       } 
      } 

     } else { 

      strB.Append(text); 
     } 

     using (TextReader sReader = new StringReader(strB.ToString())) { 

      List<IElement> list = HTMLWorker.ParseToList(sReader, new StyleSheet()); 

      foreach (IElement elm in list) { 

       document.Add(elm); 
      } 
     } 

    } catch (Exception ee) { 

     ee.ToString(); 

    } finally { 

     document.Close(); 
    } 
} 

protected void GeneratePDFAndDownload (object sender, EventArgs e) { 

    string fileName = "RetroCloud_" + proj_name + "_" + DateTime.Now.Ticks + ".pdf"; 
    GeneratePDF("", fileName, true, ""); 

    Response.Clear(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
    Response.Flush(); 
    Response.End(); 
} 

ASCX控制:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="tagcloud.ascx.cs" Inherits="tagcloud" %> 

<div style="padding-left: 25px; padding-right: 25px; text-align: center;"> 
<asp:listview runat="server" ID="ListView1" ItemPlaceholderID="itemPlaceHolder"> 
    <LayoutTemplate> 
     <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <a href='<%# GenerateNegativeStoryDetails(Eval("Tag")) %>' style="color: #ff0000; text-align: center; margin: 15px; line-height: 30px; text-decoration:none; font-size: <%# GetTagSize(Convert.ToDouble(Eval("weight"))) %>"><%# Eval("Tag") %></a> 
    </ItemTemplate> 
    <EmptyDataTemplate> 
     <asp:Label ID="negative_tags" runat="server" style="color: #ff0000;" Text="[NO NEGATIVE TAGS FOUND]"></asp:Label> 
    </EmptyDataTemplate> 
</asp:listview> 
</div> 

<br /> 

<div style="padding-left: 25px; padding-right: 25px; text-align: center;"> 
<asp:listview runat="server" ID="ListView2" ItemPlaceholderID="itemPlaceHolder"> 
    <LayoutTemplate> 
     <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <a href='<%# GeneratePositiveStoryDetails(Eval("Tag")) %>' style="color: #33cc00; text-align: center; margin: 15px; line-height: 30px; text-decoration:none; font-size: <%# GetTagSize(Convert.ToDouble(Eval("weight"))) %>"><%# Eval("Tag") %></a> 
    </ItemTemplate> 
    <EmptyDataTemplate> 
     <asp:Label ID="positive_tags" runat="server" style="color: #33cc00;" Text="[NO POSITIVE TAGS FOUND]"></asp:Label> 
    </EmptyDataTemplate> 
</asp:listview> 
</div> 

ASCX CS FILE:

string proj_id, proj_name, iteration; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    proj_name = Request.QueryString["project"].ToString(); 
    proj_id = Request.QueryString["id"].ToString(); 

    if (String.IsNullOrEmpty((string)Session["iteration"])) 
     iteration = "0"; 
    else 
     iteration = (string)Session["iteration"]; 

    BindTagCloud(); 

} 

private void BindTagCloud() 
{ 

    int pro_id = Convert.ToInt32(proj_id); 
    int iteration_id = Convert.ToInt32(iteration); 

    .... 

    if (iteration_id != 0) 
    { 
     ListView1.DataSource = tagCloudNegativeIteration; 
     ListView1.DataBind(); 

     ListView2.DataSource = tagCloudPositiveIteration; 
     ListView2.DataBind(); 

    } 
    else 
    { 
     ListView1.DataSource = tagCloudNegative; 
     ListView1.DataBind(); 

     ListView2.DataSource = tagCloudPositive; 
     ListView2.DataBind(); 

    } 

回答

0

「文檔沒有頁面」意味着您在致電document.close()之前實際上沒有向Document寫入任何內容。

我懷疑如果你在你撥打document.add()的地方設置斷點,你永遠不會打它。

我建議你花費很長時間,看看輸入和輸出到HTMLWorker.ParseToList()。我懷疑這是你的期望。