这非常接近工作。不知道为什么我在 document.Close() 中收到错误,或者为什么我在删除 document.Close() 时收到损坏的 pdf。
它似乎没有向 pdf 发布任何信息。
编码:
带有用户单击按钮的主页:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EventList.ascx.cs"
Inherits="Objects_EventList" %>
<asp:ListView runat="server" ID="lstvwEvents"
OnItemDataBound="lstvwEvents_OnItemDataBound">
<LayoutTemplate>
<asp:ImageButton BorderStyle="0" CssClass="submitbutton" runat="server" AlternateText="Get PDF" ID="LinkButton1" OnClick="btnGenerateReport" />
<div class="eventtease" style="width: 249px;border-bottom: 1px solid #c0c06b;padding-right: 10px;padding-top: 10px;height: 300px;overflow: auto;">
<asp:Literal runat="server" ID="itemPlaceholder" />
<div style="clear: both;"></div>
</div>
</LayoutTemplate>
<ItemTemplate>
<h3 style="clear: both;border-top: 1px solid #c0c06b;padding-top: 10px;"><asp:Literal runat="server" ID="ltrlShortDate" /><br /><%# Eval("EventName").ToString().ToUpper() %><br />(<asp:Literal runat="server" ID="ltrlTimes" />)</h3>
<p class="eventdescription" style="font: normal normal normal 7.5pt/normal Arial, Sans-Serif;margin-top: 3px;">
<%# Eval("Description") %>
</p>
<asp:HyperLink runat="server" ID="lnkLearnMore" Text="LEARN MORE" CssClass="learnmore" Visible="false" />
</ItemTemplate>
这是后面的代码:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
public partial class Objects_EventList : System.Web.UI.UserControl
{
public string city;
public int showcount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Load_Events();
};
}
protected void Load_Events()
{
EventsDataContext edc = new EventsDataContext();
var events = (from e in edc.tblEvents_Cafes
where e.EventDateTime >= DateTime.Now && e.VenueCity.Trim() == city.Trim() && (e.VenueName.Contains("Café") || e.VenueName.Contains("Cafe") )
orderby e.EventDateTime
select new
{
EventName = e.EventName,
EventDate = e.EventDate,
EventTime = e.EventTime,
Description = edc.tblEvents_Cafe_Descriptions.OrderBy(d => d.Priority).Where(d => d.Keywords.ToLower() == e.EventName.ToLower()).Select(d => d.Description).First(), // edc.tblEvents_Cafe_Descriptions.OrderBy(d => d.Priority).Where(d => d.Keywords.ToLower() == e.EventName.ToLower() || d.Keywords.ToLower().CompareTo(e.EventName.ToLower()) >= 0).Select(d => d.Description).First()
}).Take(showcount);
lstvwEvents.DataSource = events;
lstvwEvents.DataBind();
}
protected void lstvwEvents_OnItemDataBound(Object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var tempevent = dataItem.DataItem;
Type t = tempevent.GetType();
DateTime tempdate;
if (DateTime.TryParse((t.GetProperty("EventDate").GetValue(tempevent, null)).ToString(), out tempdate))
{
Literal ltrlShortDate = new Literal();
ltrlShortDate = (Literal)e.Item.FindControl("ltrlShortDate");
ltrlShortDate.Text = tempdate.ToString("MM/dd/yyyy");
}
if (DateTime.TryParse((t.GetProperty("EventTime").GetValue(tempevent, null)).ToString(), out tempdate))
{
Literal ltrlTimes = new Literal();
ltrlTimes = (Literal)e.Item.FindControl("ltrlTimes");
ltrlTimes.Text = tempdate.ToString("hh:mm tt");
}
}
}
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)) {
lstvwEvents.DataBind();
using (StringWriter sWriter = new StringWriter(strB)) {
using (HtmlTextWriter htWriter = new HtmlTextWriter(sWriter)) {
ListView lv1 = lstvwEvents;
lv1.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 btnGenerateReport(object sender, EventArgs e)
{
string fileName = "Calendar.pdf";
GeneratePDF("", fileName, true, "");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Flush();
Response.End();
}
}