1

This is working and opening the correct information in a pdf in a new window using itextsharp. But the first column is way too wide and the second, third, and fourth columns are too small. How do I style this?

<form id="f1">
<asp:ImageButton BorderStyle="0" CssClass="submitbutton" runat="server" AlternateText="Get PDF" ID="LinkButton1" OnClick="btnGenerateReport" />

<asp:GridView BackColor="#FFFFFF" ID="gvEventCaf" runat="server" >
</asp:GridView>
</form>

Here is the codebehind for the get pdf onclick:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;

protected void btnGenerateReport(object sender, EventArgs e)
{
    MyPage tmpPage = new MyPage();
    HtmlForm form = new HtmlForm();
    form.Controls.Add(gvEventCaf);
    tmpPage.Controls.Add(form);
    StringWriter sw = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
    form.Controls[0].RenderControl(htmlWriter);
    string htmlContent = sw.ToString();
    Document document = new Document();
    // step 2:
    // we create a writer that listens to the document
    // and directs a PDF-stream to a file
    PdfWriter.GetInstance(document, new FileStream("c:\\users\\My\\Downloads\\Calendar.pdf", FileMode.Create));

    // step 3: we open the document
    document.Open();

    // step 4: we add a paragraph to the document
    //document.Add(new Paragraph(htmlContent.ToString()));

    System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));

    HtmlParser.Parse(document, _xmlr);

    // step 5: we close the document
    document.Close();

    ShowPdf("c:\\users\\Me\\Downloads\\Calendar.pdf");

}

And here's where I see the pdf:

private void ShowPdf(string s)
{
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "inline;filename=" + s);
    Response.ContentType = "application/pdf";
    Response.WriteFile(s);
    Response.Flush();
    Response.Clear();
}

And here is the Mypage.cs helper file I put in App_Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for MyPage
/// </summary>
public class MyPage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
    GridView grid = control as GridView;
    if (grid != null && grid.ID == "gvEventCaf")
        return;
    else
        base.VerifyRenderingInServerForm(control);

}
}
4

1 に答える 1