简体   繁体   中英

iTextSharp output to PDF

I've been trying to get this working for a long time now and I was hoping that someone could help me out with this. I am trying to save two ListViews from my ASCX control (incorporated to my ASPX page as follows) to a PDF file. I believe that the problem lies in these two lines of code, as I am getting an 'the document has no pages' error when saving. Any ideas would be greatly appreciated! Thanks in advance...

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

ASPX page:

<%@ 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:

<%@ 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();

    }

"The Document has no pages" means you haven't actually written anything to the Document before calling document.close() .

I suspect that if you set a breakpoint at the line where you call document.add() , you'll never hit it.

I suggest you take a long, hard look at the input and output to HTMLWorker.ParseToList() . I doubt it's what you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM