EDN Admin
Well-known member
I want to display header and footer on every page of pdf, i am using itextsharp in C#.
I created class namely PdfPage which inherits PdfPageEventHelper
the code contains OnstartPage(for Header) OnEndPage(for Footer) namespace WebApplication2
{
public class PdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document document)
{
string image2 = HttpContext.Current.Server.MapPath(".") + "/Images/developers_img.jpg";
string image3 = HttpContext.Current.Server.MapPath(".") + "/Images/contact_logo.png";
string imageFilePath = HttpContext.Current.Server.MapPath(".") + "/Images/contact_logo.png";
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(image2);
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(image3);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(280f, 400f);
jpg.Alignment = Element.ALIGN_TOP;
PdfPTable table1 = new PdfPTable(3);
table1.TotalWidth = document.PageSize.Width;
table1.WidthPercentage = 90;
//table1.HorizontalAlignment = Element.ALIGN_BOTTOM;
PdfPCell cell11 = new PdfPCell();
// cell11.PaddingLeft = 10;
cell11.AddElement(jpg);
cell11.Border = 0;
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell12 = new PdfPCell();
cell12.Border = 0;
PdfTemplate pdftm = writer.DirectContent.CreateTemplate(0,0);
PdfPCell cell13 = new PdfPCell();
cell13.AddElement(new Paragraph(""));
cell13.AddElement(new Paragraph(cellHeight+"Cell Height"));
cell13.AddElement(new Paragraph(" Last Updated"));
cell13.AddElement(new Paragraph(" " + DateTime.Now.ToString()));
cell13.Border = 0;
table1.AddCell(cell11);
table1.AddCell(cell12);
table1.AddCell(cell13);
document.Add(table1);
}
//Add the footer at the page end
public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfPTable footerTbl = new PdfPTable(3);
footerTbl.TotalWidth = doc.PageSize.Width;
footerTbl.WidthPercentage = 90;
float cellHeight = doc.BottomMargin;
PdfPCell cell31 = new PdfPCell();
cell31.FixedHeight = cellHeight;
cell31.AddElement(new Paragraph(" "));
cell31.AddElement(new Paragraph(" "));
cell31.AddElement(new Paragraph(" "));
cell31.AddElement(new Paragraph(cellHeight+"Bottom margin "));
cell31.AddElement(new Paragraph("CONFIDENTIAL"));
cell31.AddElement(new Paragraph(" "));
cell31.Border = 0;
cell31.PaddingLeft = 10;
cell31.MinimumHeight = 100f;
footerTbl.AddCell(cell31);
PdfPCell cell32 = new PdfPCell();
cell32.Border = 0;
footerTbl.AddCell(cell32);
PdfPCell cell33 = new PdfPCell();
cell33.Border = 0;
//cell33.VerticalAlignment = Element.ALIGN_TOP;
//cell33.MinimumHeight = 100f;
cell33.PaddingBottom = 50;
cell33.AddElement(new Paragraph("Computer Solu, Inc."));
cell33.AddElement(new Paragraph("481 Main Street- 10801"));
cell33.AddElement(new Paragraph("Phone: 914 - 355 - 5800"));
cell33.AddElement(new Paragraph("www.computersolution.com"));
footerTbl.AddCell(cell33);
footerTbl.WriteSelectedRows(0, -1,0,footerTbl.TotalHeight, writer.DirectContent);
}
in the webpage button click i am creating the document object and creating object for the Pdfpage class,
**but page contents are overlapping on footer content but the requirement to display header and footer on all pages of pdf document,
if the content beyond the one page it should added into next page after adding header at the top of second page then the remaining content need to added in second page
at the bottom of second page again footer to be added
the following code i used , image(Images/developers_img.jpg") is overlapping on footer in first page
in third page ,image(Images/developers_img.jpg") is coming at position of header
after that my header is displaying**namespace WebApplication2
{
public partial class CSE_Web : System.Web.UI.Page
{
protected Font ParaFont
{
get
{
// create a basecolor to use for the footer font, if needed.
BaseColor grey = new BaseColor(128, 128, 128);
Font font = FontFactory.GetFont("Arial", 9, Font.NORMAL,grey);
return font;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var doc1 = new Document(PageSize.A4, 50, 50, 25, 25);
string path = Server.MapPath("PDF");
string path1 = @"D:ASP.NETCse website pdfWebApplication2WebApplication2PDFDoc1.pdf";
string image2 = Server.MapPath(".") + "/Images/developers_img.jpg";
string image3 = Server.MapPath(".") + "/Images/contact_logo.png";
string imageFilePath = Server.MapPath(".") + "/Images/contact_logo.png";
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(image2);
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(image3);
PdfPage page = new PdfPage();
PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(280f,400f);
jpg.Alignment = Element.ALIGN_TOP;
doc1.Open();
writer.PageEvent = new PdfPage();
PdfPTable table1 = new PdfPTable(3);
table1.TotalWidth = doc1.PageSize.Width;
PdfPCell cell11 = new PdfPCell();
cell11.AddElement(jpg);
cell11.Border = 0;
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell12 = new PdfPCell();
cell12.Border = 0;
PdfPCell cell13 = new PdfPCell();
cell13.AddElement(new Paragraph(""));
cell13.AddElement(new Paragraph(""));
cell13.PaddingRight = -1;
cell13.HorizontalAlignment = Element.PHRASE;
cell13.AddElement(new Paragraph(" Last Updated"));
cell13.AddElement(new Paragraph(" " + DateTime.Now.ToString()));
cell13.Border = 0;
table1.AddCell(cell11);
table1.AddCell(cell12);
table1.AddCell(cell13);
doc1.Add(table1);
Paragraph p1 = new Paragraph("n Computer Solutions East services the Tri-State area of New York, Connecticut, and New Jersey, as the leading information technology (IT) company that offers innovative computer services and products to enable small and medium size enterprises to succeed in todays digital-age. Our clients rely on our computer premium services, knowledgeable/certified consultants to lead them across the Information Technology highway so they can compete head to head against their competitors and larger organizations. Give us a call to learn more about how we can service your business or home.", ParaFont);
p1.Alignment = Element.ALIGN_JUSTIFIED_ALL;
doc1.Add(p1);
Paragraph p2 = new Paragraph();
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(new Paragraph("n"));
doc1.Add(new Paragraph("n"));
System.Diagnostics.Process.Start("IExplore.exe", path1);
doc1.Close();
}
}
}
any help to solve this issue
sathishtech.IT
View the full article
I created class namely PdfPage which inherits PdfPageEventHelper
the code contains OnstartPage(for Header) OnEndPage(for Footer) namespace WebApplication2
{
public class PdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document document)
{
string image2 = HttpContext.Current.Server.MapPath(".") + "/Images/developers_img.jpg";
string image3 = HttpContext.Current.Server.MapPath(".") + "/Images/contact_logo.png";
string imageFilePath = HttpContext.Current.Server.MapPath(".") + "/Images/contact_logo.png";
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(image2);
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(image3);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(280f, 400f);
jpg.Alignment = Element.ALIGN_TOP;
PdfPTable table1 = new PdfPTable(3);
table1.TotalWidth = document.PageSize.Width;
table1.WidthPercentage = 90;
//table1.HorizontalAlignment = Element.ALIGN_BOTTOM;
PdfPCell cell11 = new PdfPCell();
// cell11.PaddingLeft = 10;
cell11.AddElement(jpg);
cell11.Border = 0;
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell12 = new PdfPCell();
cell12.Border = 0;
PdfTemplate pdftm = writer.DirectContent.CreateTemplate(0,0);
PdfPCell cell13 = new PdfPCell();
cell13.AddElement(new Paragraph(""));
cell13.AddElement(new Paragraph(cellHeight+"Cell Height"));
cell13.AddElement(new Paragraph(" Last Updated"));
cell13.AddElement(new Paragraph(" " + DateTime.Now.ToString()));
cell13.Border = 0;
table1.AddCell(cell11);
table1.AddCell(cell12);
table1.AddCell(cell13);
document.Add(table1);
}
//Add the footer at the page end
public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfPTable footerTbl = new PdfPTable(3);
footerTbl.TotalWidth = doc.PageSize.Width;
footerTbl.WidthPercentage = 90;
float cellHeight = doc.BottomMargin;
PdfPCell cell31 = new PdfPCell();
cell31.FixedHeight = cellHeight;
cell31.AddElement(new Paragraph(" "));
cell31.AddElement(new Paragraph(" "));
cell31.AddElement(new Paragraph(" "));
cell31.AddElement(new Paragraph(cellHeight+"Bottom margin "));
cell31.AddElement(new Paragraph("CONFIDENTIAL"));
cell31.AddElement(new Paragraph(" "));
cell31.Border = 0;
cell31.PaddingLeft = 10;
cell31.MinimumHeight = 100f;
footerTbl.AddCell(cell31);
PdfPCell cell32 = new PdfPCell();
cell32.Border = 0;
footerTbl.AddCell(cell32);
PdfPCell cell33 = new PdfPCell();
cell33.Border = 0;
//cell33.VerticalAlignment = Element.ALIGN_TOP;
//cell33.MinimumHeight = 100f;
cell33.PaddingBottom = 50;
cell33.AddElement(new Paragraph("Computer Solu, Inc."));
cell33.AddElement(new Paragraph("481 Main Street- 10801"));
cell33.AddElement(new Paragraph("Phone: 914 - 355 - 5800"));
cell33.AddElement(new Paragraph("www.computersolution.com"));
footerTbl.AddCell(cell33);
footerTbl.WriteSelectedRows(0, -1,0,footerTbl.TotalHeight, writer.DirectContent);
}
in the webpage button click i am creating the document object and creating object for the Pdfpage class,
**but page contents are overlapping on footer content but the requirement to display header and footer on all pages of pdf document,
if the content beyond the one page it should added into next page after adding header at the top of second page then the remaining content need to added in second page
at the bottom of second page again footer to be added
the following code i used , image(Images/developers_img.jpg") is overlapping on footer in first page
in third page ,image(Images/developers_img.jpg") is coming at position of header
after that my header is displaying**namespace WebApplication2
{
public partial class CSE_Web : System.Web.UI.Page
{
protected Font ParaFont
{
get
{
// create a basecolor to use for the footer font, if needed.
BaseColor grey = new BaseColor(128, 128, 128);
Font font = FontFactory.GetFont("Arial", 9, Font.NORMAL,grey);
return font;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var doc1 = new Document(PageSize.A4, 50, 50, 25, 25);
string path = Server.MapPath("PDF");
string path1 = @"D:ASP.NETCse website pdfWebApplication2WebApplication2PDFDoc1.pdf";
string image2 = Server.MapPath(".") + "/Images/developers_img.jpg";
string image3 = Server.MapPath(".") + "/Images/contact_logo.png";
string imageFilePath = Server.MapPath(".") + "/Images/contact_logo.png";
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(image2);
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(image3);
PdfPage page = new PdfPage();
PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(280f,400f);
jpg.Alignment = Element.ALIGN_TOP;
doc1.Open();
writer.PageEvent = new PdfPage();
PdfPTable table1 = new PdfPTable(3);
table1.TotalWidth = doc1.PageSize.Width;
PdfPCell cell11 = new PdfPCell();
cell11.AddElement(jpg);
cell11.Border = 0;
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell12 = new PdfPCell();
cell12.Border = 0;
PdfPCell cell13 = new PdfPCell();
cell13.AddElement(new Paragraph(""));
cell13.AddElement(new Paragraph(""));
cell13.PaddingRight = -1;
cell13.HorizontalAlignment = Element.PHRASE;
cell13.AddElement(new Paragraph(" Last Updated"));
cell13.AddElement(new Paragraph(" " + DateTime.Now.ToString()));
cell13.Border = 0;
table1.AddCell(cell11);
table1.AddCell(cell12);
table1.AddCell(cell13);
doc1.Add(table1);
Paragraph p1 = new Paragraph("n Computer Solutions East services the Tri-State area of New York, Connecticut, and New Jersey, as the leading information technology (IT) company that offers innovative computer services and products to enable small and medium size enterprises to succeed in todays digital-age. Our clients rely on our computer premium services, knowledgeable/certified consultants to lead them across the Information Technology highway so they can compete head to head against their competitors and larger organizations. Give us a call to learn more about how we can service your business or home.", ParaFont);
p1.Alignment = Element.ALIGN_JUSTIFIED_ALL;
doc1.Add(p1);
Paragraph p2 = new Paragraph();
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(p1);
p2.Add(Environment.NewLine);
doc1.Add(p2);
doc1.Add(jpg2);
doc1.Add(new Paragraph("n"));
doc1.Add(new Paragraph("n"));
System.Diagnostics.Process.Start("IExplore.exe", path1);
doc1.Close();
}
}
}
any help to solve this issue
sathishtech.IT
View the full article