【发布时间】:2021-03-11 00:54:29
【问题描述】:
我想将gridview转换为pdf,代码运行良好,但我很困惑如何在其上制作headerpage(不是header column),我尝试使用stringbuilder但是当我尝试gridview不是在pdf中查看时,我也尝试了其他方法,但仍然不知道如何使它起作用, 也许你可以教我如何让它工作?
在这段代码中我尝试使用块,但块没有显示:(
protected void btnConvertPDF_Click(object sender, EventArgs e)
{
gridconvertPDF.AllowPaging = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm hf = new HtmlForm();
gridconvertPDF.Parent.Controls.Add(hf);
hf.Attributes["runat"] = "server";
hf.Controls.Add(gridconvertPDF);
hf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.Open();
Chunk c = new Chunk
("PEMINJAMAN INVENTARIS \n",
FontFactory.GetFont("Verdana", 25));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("Rizki Asriningtyas \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
pdfDoc.Add(p);
pdfDoc.Add(p1);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();}
【问题讨论】:
-
您仅在将所有标题内容添加到
pdfDoc之后 关联PdfWriter。因此,所有标题内容都丢失了。您应该先创建一个Document,然后关联一个PdfWriter,然后打开文档,然后将内容添加到文档中。 -
是的,它的作品,非常感谢:)