【发布时间】:2011-07-19 05:13:31
【问题描述】:
我有一个用户控件,它有一个面板,用于显示我在需要时打印的信息。 我有一个网格视图,我可以在其中获得搜索结果它有一个链接按钮,点击它我想在上面打开那个面板的 pdf。
【问题讨论】:
-
显示相关代码会有所帮助。你错过了最后一句话吗?
我有一个用户控件,它有一个面板,用于显示我在需要时打印的信息。 我有一个网格视图,我可以在其中获得搜索结果它有一个链接按钮,点击它我想在上面打开那个面板的 pdf。
【问题讨论】:
我已经实现了这一点,我希望其他人也可以这样做。我在下面列出了我的代码。
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
导入上面的DLL
string attachment = "attachment; filename=" + "File Name" + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
htextw.AddStyleAttribute("font-size", "7pt");
htextw.AddStyleAttribute("color", "Black");
Page pg = new Page();
HtmlForm frm = new HtmlForm();
pg.EnableEventValidation = false;
pg.RenderControl(htextw);
Document document = new Document();
document = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
//This is how you can add text in div in a pdf
Chunk c = new Chunk(TextHere + "\n", FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_LEFT;
p.Add(c); p.Add(c1);
// This is how you can generate table and can set proprties
PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 100;
//Bill No and Bill Date
PdfPCell pdfcell1 = new PdfPCell(new Phrase("Text in TAble" + "TExt From Database"));
pdfcell1.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER | iTextSharp.text.Rectangle.TOP_BORDER;
table.AddCell(pdfcell1);
PdfPCell pdfcell = new PdfPCell(new Phrase("Text in TAble" + "TExt From Database"));
pdfcell.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER | iTextSharp.text.Rectangle.TOP_BORDER;
pdfcell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.AddCell(pdfcell);
document.Add(p);
document.Add(table);
document.Add(tablegrid);
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
【讨论】:
我想你可能会在这个问题上遇到一些麻烦。你不能在客户端做这一切,因为 JavaScript 不能操纵文件(按设计)。 所以你的选择是:
您可以尝试对自定义处理程序 (.ashx) 文件进行 Ajax 调用,然后返回 PDF 文件的响应。我以前没有尝试过,但它可能会起作用。用target="_blank" 打开另一个页面怎么样,它会运行服务器端代码并生成 PDF,而原始页面保持在原来的位置?
【讨论】: