如果您使用的是 mvc,那么您可以使用 Controller 和 iTextSharp 来生成您的 pdf。
首先添加新的 PdfGeneratorController
using iTextSharp.text;
using iTextSharp.text.xml;
using iTextSharp.text.pdf;
public class PdfGeneratorController : Controller
{
/// <summary>
/// Renders an action result to a string. This is done by creating a fake http context
/// and response objects and have that response send the data to a string builder
/// instead of the browser.
/// </summary>
/// <param name="result">The action result to be rendered to string.</param>
/// <returns>The data rendered by the given action result.</returns>
protected string RenderActionResultToString(ActionResult result)
{
// Create memory writer.
var sb = new StringBuilder();
var memWriter = new StringWriter(sb);
// Create fake http context to render the view.
var fakeResponse = new HttpResponse(memWriter);
var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, fakeResponse);
var fakeControllerContext = new ControllerContext(
new HttpContextWrapper(fakeContext),
this.ControllerContext.RouteData,
this.ControllerContext.Controller);
var oldContext = System.Web.HttpContext.Current;
System.Web.HttpContext.Current = fakeContext;
// Render the view.
result.ExecuteResult(fakeControllerContext);
// Restore data.
System.Web.HttpContext.Current = oldContext;
// Flush memory and return output.
memWriter.Flush();
return sb.ToString();
}
/// <summary>
/// Returns a PDF action result. This method renders the view to a string then
/// use that string to generate a PDF file. The generated PDF file is then
/// returned to the browser as binary content. The view associated with this
/// action should render an XML compatible with iTextSharp xml format.
/// </summary>
/// <param name="model">The model to send to the view.</param>
/// <returns>The resulted BinaryContentResult.</returns>
protected ActionResult ViewPdf(object model)
{
// Create the iTextSharp document.
Document doc = new Document();
// Set the document to write to memory.
MemoryStream memStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
writer.CloseStream = false;
doc.Open();
// Render the view xml to a string, then parse that string into an XML dom.
string xmltext = this.RenderActionResultToString(this.View(model));
XmlDocument xmldoc = new XmlDocument();
xmldoc.InnerXml = xmltext.Trim();
// Parse the XML into the iTextSharp document.
ITextHandler textHandler = new ITextHandler(doc);
textHandler.Parse(xmldoc);
// Close and get the resulted binary data.
doc.Close();
byte[] buf = new byte[memStream.Position];
memStream.Position = 0;
memStream.Read(buf, 0, buf.Length);
// Send the binary data to the browser.
return new BinaryContentResult(buf, "application/pdf");
}
}
然后从 PdfGeneratorController 继承您的常规控制器:
public class PrintToPdfController : PdfGeneratorController
{
public ActionResult Index()
{
return ViewPdf(GetModel(userName));
}
}
在您的视图中,您可以添加图像标签:
<%@ Page Language=”C#” Inherits=”System.Web.Mvc.ViewPage<Sample1.Models.Customer>” %>
<%@ Import Namespace=”Sample1.Models” %>
<?xml version=”1.0? encoding=”UTF-8? ?>
<itext creationdate=”2/4/2003 5:49:07 PM” producer=”iTextSharpXML”>
<paragraph leading=”18.0? font=”unknown” size=”16.0? align=”Default”>
<chunk>Orders in PDF</chunk>
</paragraph>
<paragraph leading=”18.0? font=”unknown” size=”10.0? align=”Default”>
<chunk>Customer Name: <%= this.Model.Name %></chunk><newline />
<chunk>Address: <%= this.Model.Address %></chunk><newline />
<chunk><image url="<%=MapPath(Model.UserDetail.Photo)%>" plainwidth="100.0" plainheight="100.0" />
</paragraph>
<paragraph leading=”18.0? font=”unknown” size=”10.0? align=”Default”>
<chunk font=”unknown” size=”12.0?>Orders:</chunk><newline />
<% foreach (Order o in this.Model.Order)
{ %>
<chunk font=”unknown” size=”10.0?><%= o.OrderId %>, <%= o.Description %>,
<%= o.Amount %>, <%= o.Date %></chunk><newline />
<% } %>
</paragraph>
</itext>