我正在使用ITextSharp动态生成pdf,然后将其保存到磁盘并使用Frame显示它。
Frame有一个名为src的属性,我传递生成的文件名。
这一切正常,我想要实现的是将生成的pdf文件传递给Frame而不将其保存到磁盘。
HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER);
HtmlPdfPage first = builder.AddPage();
//import an entire sheet
builder.ImportStylesheet(Request.PhysicalApplicationPath + "CSS\\Stylesheet.css");
string coupon = CreateCoupon();
first.AppendHtml(coupon);
byte[] file = builder.RenderPdf();
File.WriteAllBytes(Request.PhysicalApplicationPath+"final.pdf", file);
printable.Attributes["src"] = "final.pdf";
答案 0 :(得分:2)
我完全按照你要做的去做。您将要创建一个处理程序(.ashx)。创建PDF后,使用以下代码将其加载到处理程序中:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MapHandler : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest(HttpContext context) {
CreateImage(context);
}
private void CreateImage(HttpContext context) {
string documentFullname = // Get full name of the PDF you want to display...
if (File.Exists(documentFullname)) {
byte[] buffer;
using (FileStream fileStream = new FileStream(documentFullname, FileMode.Open, FileAccess.Read, FileShare.Read))
using (BinaryReader reader = new BinaryReader(fileStream)) {
buffer = reader.ReadBytes((int)reader.BaseStream.Length);
}
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Length", buffer.Length.ToString());
context.Response.BinaryWrite(buffer);
context.Response.End();
} else {
context.Response.Write("Unable to find the document you requested.");
}
}
public bool IsReusable {
get {
return false;
}
}