如何在C#中使用iTextSharp在每页底部添加页面标题和页码? [英] How to add page title at top and page number at bottom of every page using iTextSharp in C#?
本文介绍了如何在C#中使用iTextSharp在每页底部添加页面标题和页码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将gridview转换为pdf格式,我已经完成了。
但是现在我想在每个页面的底部添加pagenumber。
i已经完成了这个
I have to convert gridview to pdf form , that i have done it.
But now i want to add pagenumber at bottom of every page.
i have done this
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
HtmlForm hForm = new HtmlForm();
grd.AllowPaging = false;
grd.Parent.Controls.Add(hForm);
hForm.Attributes["runat"] = "server";
hForm.Controls.Add(grdMessengerRequest);
hForm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
// at this place i have added table , which is displaying title
htmlparser.Parse(sr);
pdfDoc.Close();
}
}
现在,我必须溢出字符串阅读器/ htmlworker,所以我可以添加标题每个页面和文件的页码也是。
说瓷砖和页码的循环。
所以有任何解决方案,让我知道。
谢谢。
Now , i have to spilt string reader / htmlworker , so i can add title to each n every page and page number of document too.
say looping for tile and page number.
so is there any solution , lte me know .
thank you.
推荐答案
这里的完整代码很好..
第1步:将DATAGRIDVIEW表分配给DATATABLE OBJECT DT ..
之后用以下代码更改列名...
fine here full code..
1st step : Assign DATAGRIDVIEW TABLE TO DATATABLE OBJECT DT..
After that change column names in follwing code..
string filename="TEST.PDF";
Document document = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 11);
document.Open();
PdfPTable table = new PdfPTable(dt.Columns.Count-2);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f,4f,4f,4f,4f,4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
cell1.Colspan = dt.Columns.Count;
cell1.Padding = 5;
table.AddCell(cell1);
PdfPCell cell;
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
Chunk chunkCols = new Chunk("Candidate's Interview List", ColFont);
cell = new PdfPCell(new Paragraph(chunkCols));
cell.Colspan = dt.Columns.Count;
//cell.PaddingLeft = 10;
cell.Padding = 5;
table.AddCell(cell);
PdfPCell cellH;
Font ColFont3 = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
Chunk chunkCols1 = new Chunk("Report From : " + (startdate == "01/01/0001" ? "-" : startdate) + " To : " + (enddate == "12/31/9999" ? "-" : enddate), ColFont3);
cellH = new PdfPCell(new Paragraph(chunkCols1));
cellH.Colspan = dt.Columns.Count;
//cell.PaddingLeft = 10;
cellH.Padding = 5;
table.AddCell(cellH);
foreach (DataColumn c in dt.Columns)
{
if (c.ColumnName != "Recruiter Name" && c.ColumnName != "Type")
{
cell = new PdfPCell(new Paragraph(new Chunk(c.Caption, FontFactory.GetFont(FontFactory.HELVETICA, 11, Font.BOLD))));
table.AddCell(cell);
}
}
table.HeaderRows = 4;
table.DefaultCell.BorderWidth = 1;
DataTable uniqueCols = dt.DefaultView.ToTable(true, "Recruiter Name");
Font ColFont1 = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
if (uniqueCols.Rows.Count > 0)
{
for (int distRN = 0; distRN < uniqueCols.Rows.Count; distRN++)
{
string RecruiterName = uniqueCols.Rows[distRN]["Recruiter Name"].ToString();
if (RecruiterName != "Not Available")
{
DataRow[] dtDetails = dt.Select("[Recruiter Name] = '" + RecruiterName.Replace("'", "''") + "'");
Chunk chunkCols2 = new Chunk("Account Manager : " + RecruiterName, ColFont1);
cell = new PdfPCell(new Paragraph(chunkCols2));
cell.Colspan = dt.Columns.Count - 2;
//cell.PaddingLeft = 10;
cell.Padding = 5;
cell.MinimumHeight = 25f;
cell.Border = 0;
table.AddCell(cell);
foreach (DataRow r in dtDetails)
{
PdfPCell DateInterview = new PdfPCell(new Paragraph(r["Date of Interview"].ToString(), font5));
DateInterview.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(DateInterview);
PdfPCell CCandName = new PdfPCell(new Paragraph(r["Candidate Name"].ToString(), font5));
CCandName.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(CCandName);
PdfPCell CCompnyName = new PdfPCell(new Paragraph(r["Company Name"].ToString(), font5));
CCompnyName.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(CCompnyName);
PdfPCell CCompnyClintName = new PdfPCell(new Paragraph(r["Company Contact Name"].ToString(), font5));
CCompnyClintName.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(CCompnyClintName);
PdfPCell CTitle = new PdfPCell(new Paragraph(r["Title"].ToString(), font5));
CTitle.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(CTitle);
PdfPCell CSubject = new PdfPCell(new Paragraph(r["Subject"].ToString(), font5));
CSubject.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(CSubject);
PdfPCell DateAdded = new PdfPCell(new Paragraph(r["Date Added"].ToString(), font5));
DateAdded.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(DateAdded);
}
}
}
}
document.Add(table);
Paragraph footer = new Paragraph("COMANY NAME", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth=1000;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell2 = new PdfPCell(footer);
cell2.Border = 0;
cell2.PaddingLeft = 10;
footerTbl.AddCell(cell2);
footerTbl.WriteSelectedRows(0, -1, 550, 30, writer.DirectContent);
document.Close();
此代码对您有帮助...
This code helpfull for you...
PdfPCell cell;
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
Chunk chunkCols = new Chunk("Candidate's Interview List", ColFont);
cell = new PdfPCell(new Paragraph(chunkCols));
cell.Colspan = dt.Columns.Count;
//cell.PaddingLeft = 10;
cell.Padding = 5;
table.AddCell(cell);
table.HeaderRows = 4;
谢谢......
Thank you...
Hai ..
请找到以下代码....所以你可以为每个页面的每个n添加标题
Hai..
Please find the following code.... SO you can add title to each n every page
Document document = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 11);
document.Open();
PdfPTable table = new PdfPTable(dt.Columns.Count-2);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f,4f,4f,4f,4f,4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("LOGO PATH"));
PdfPCell cell1 = new PdfPCell(logo);
cell1.Colspan = dt.Columns.Count;
cell1.Padding = 5;
table.AddCell(cell1);
PdfPCell cell;
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
Chunk chunkCols = new Chunk("NAME OF THE HEADING", ColFont);
cell = new PdfPCell(new Paragraph(chunkCols));
cell.Colspan = dt.Columns.Count;
//cell.PaddingLeft = 10;
cell.Padding = 5;
table.AddCell(cell);
table.HeaderRows = 4;
table.DefaultCell.BorderWidth = 1;
这篇关于如何在C#中使用iTextSharp在每页底部添加页面标题和页码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文