84 lines
2.8 KiB
C#
84 lines
2.8 KiB
C#
using iTextSharp.text;
|
|||
|
|
using iTextSharp.text.pdf;
|
||
|
|
using System;
|
||
|
|
|
||
|
|
namespace Net.CrossCotting
|
||
|
|
{
|
||
|
|
public class PageEventHelper : PdfPageEventHelper
|
||
|
|
{
|
||
|
|
// This is the contentbyte object of the writer
|
||
|
|
PdfContentByte cb;
|
||
|
|
// we will put the final number of pages in a template
|
||
|
|
PdfTemplate template;
|
||
|
|
|
||
|
|
PdfTemplate templateHeader;
|
||
|
|
// this is the BaseFont we are going to use for the header / footer
|
||
|
|
BaseFont bf = null;
|
||
|
|
// This keeps track of the creation time
|
||
|
|
DateTime PrintTime = DateTime.Now;
|
||
|
|
|
||
|
|
#region Properties
|
||
|
|
private string _Title;
|
||
|
|
public string Title
|
||
|
|
{
|
||
|
|
get { return _Title; }
|
||
|
|
set { _Title = value; }
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
// we override the onOpenDocument method
|
||
|
|
public override void OnOpenDocument(PdfWriter writer, Document document)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
PrintTime = DateTime.Now;
|
||
|
|
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
|
||
|
|
cb = writer.DirectContent;
|
||
|
|
template = cb.CreateTemplate(document.PageSize.Width, 80);
|
||
|
|
templateHeader = cb.CreateTemplate(document.PageSize.Width, 80);
|
||
|
|
}
|
||
|
|
catch (DocumentException)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
catch (System.IO.IOException)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void OnStartPage(PdfWriter writer, Document document)
|
||
|
|
{
|
||
|
|
base.OnStartPage(writer, document);
|
||
|
|
Rectangle pageSize = document.PageSize;
|
||
|
|
if (Title != string.Empty)
|
||
|
|
{
|
||
|
|
cb.BeginText();
|
||
|
|
cb.SetFontAndSize(bf, 15);
|
||
|
|
cb.SetRgbColorFill(50, 50, 200);
|
||
|
|
cb.SetColorFill(new BaseColor(103, 93, 152));
|
||
|
|
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
|
||
|
|
cb.ShowText(Title);
|
||
|
|
cb.EndText();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void OnEndPage(PdfWriter writer, Document document)
|
||
|
|
{
|
||
|
|
base.OnEndPage(writer, document);
|
||
|
|
int pageN = writer.PageNumber;
|
||
|
|
Rectangle pageSize = document.PageSize;
|
||
|
|
cb.SetRgbColorFill(100, 100, 100);
|
||
|
|
cb.BeginText();
|
||
|
|
cb.SetFontAndSize(bf, 8);
|
||
|
|
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
|
||
|
|
cb.EndText();
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void OnCloseDocument(PdfWriter writer, Document document)
|
||
|
|
{
|
||
|
|
base.OnCloseDocument(writer, document);
|
||
|
|
template.BeginText();
|
||
|
|
template.SetFontAndSize(bf, 8);
|
||
|
|
template.SetTextMatrix(0, 0);
|
||
|
|
template.EndText();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|