PdfTemplate headerTemplate, footerTemplate;
BaseFont bf =
null
;
DateTime PrintTime = DateTime.Now;
#region Fields
private
string
_header;
#endregion
#region Properties
public
string
Header
get
{
return
_header; }
set
{ _header =
value
; }
#endregion
public
override
void
OnOpenDocument(PdfWriter writer, Document PdfDoc)
PrintTime = DateTime.Now;
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb = writer.DirectContent;
headerTemplate = cb.CreateTemplate(
100
,
100
);
footerTemplate = cb.CreateTemplate(
50
,
50
);
catch
(DocumentException de)
catch
(System.IO.IOException ioe)
public
override
void
OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document PdfDoc)
base
.OnEndPage(writer, PdfDoc);
iTextSharp.text.Font baseFontNormal =
new
iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA,
12f
, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
iTextSharp.text.Font baseFontBig =
new
iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA,
12f
, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK);
Phrase p1Header =
new
Phrase(
"
Sample Header Here"
, baseFontNormal);
PdfPTable pdfTab =
new
PdfPTable(
3
);
PdfPCell pdfCell1 =
new
PdfPCell();
PdfPCell pdfCell2 =
new
PdfPCell(p1Header);
PdfPCell pdfCell3 =
new
PdfPCell();
String
text =
"
Page "
+ writer.PageNumber +
"
of "
;
cb.BeginText();
cb.SetFontAndSize(bf,
12
);
cb.SetTextMatrix(PdfDoc.PageSize.GetRight(
200
), PdfDoc.PageSize.GetTop(
45
));
cb.ShowText(text);
cb.EndText();
float
len = bf.GetWidthPoint(text,
12
);
cb.AddTemplate(headerTemplate, PdfDoc.PageSize.GetRight(
200
) + len, PdfDoc.PageSize.GetTop(
45
));
cb.BeginText();
cb.SetFontAndSize(bf,
12
);
cb.SetTextMatrix(PdfDoc.PageSize.GetRight(
180
), PdfDoc.PageSize.GetBottom(
30
));
cb.ShowText(text);
cb.EndText();
float
len = bf.GetWidthPoint(text,
12
);
cb.AddTemplate(footerTemplate, PdfDoc.PageSize.GetRight(
180
) + len, PdfDoc.PageSize.GetBottom(
30
));
PdfPCell pdfCell4 =
new
PdfPCell(
new
Phrase(
"
Sub Header Description"
, baseFontNormal));
PdfPCell pdfCell5 =
new
PdfPCell(
new
Phrase(
"
Date:"
+ PrintTime.ToShortDateString(), baseFontBig));
PdfPCell pdfCell6 =
new
PdfPCell();
PdfPCell pdfCell7 =
new
PdfPCell(
new
Phrase(
"
TIME:"
+
string
.Format(
"
{0:t}"
, DateTime.Now), baseFontBig));
pdfCell1.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell2.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell3.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell4.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell5.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell6.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell7.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;
pdfCell3.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.VerticalAlignment = Element.ALIGN_TOP;
pdfCell5.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell6.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell7.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.Colspan =
3
;
pdfCell1.Border =
0
;
pdfCell2.Border =
0
;
pdfCell3.Border =
0
;
pdfCell4.Border =
0
;
pdfCell5.Border =
0
;
pdfCell6.Border =
0
;
pdfCell7.Border =
0
;
pdfTab.AddCell(pdfCell1);
pdfTab.AddCell(pdfCell2);
pdfTab.AddCell(pdfCell3);
pdfTab.AddCell(pdfCell4);
pdfTab.AddCell(pdfCell5);
pdfTab.AddCell(pdfCell6);
pdfTab.AddCell(pdfCell7);
pdfTab.TotalWidth = PdfDoc.PageSize.Width -
80f
;
pdfTab.WidthPercentage =
70
;
pdfTab.WriteSelectedRows(
0
, -1,
40
, PdfDoc.PageSize.Height -
30
, writer.DirectContent);
cb.MoveTo(
40
, PdfDoc.PageSize.Height -
100
);
cb.LineTo(PdfDoc.PageSize.Width -
40
, PdfDoc.PageSize.Height -
100
);
cb.Stroke();
cb.MoveTo(
40
, PdfDoc.PageSize.GetBottom(
50
));
cb.LineTo(PdfDoc.PageSize.Width -
40
, PdfDoc.PageSize.GetBottom(
50
));
cb.Stroke();
public
override
void
OnCloseDocument(PdfWriter writer, Document PdfDoc)
base
.OnCloseDocument(writer, PdfDoc);
headerTemplate.BeginText();
headerTemplate.SetFontAndSize(bf,
12
);
headerTemplate.SetTextMatrix(
0
,
0
);
headerTemplate.ShowText((writer.PageNumber -
1
).ToString());
headerTemplate.EndText();
footerTemplate.BeginText();
footerTemplate.SetFontAndSize(bf,
12
);
footerTemplate.SetTextMatrix(
0
,
0
);
footerTemplate.ShowText((writer.PageNumber -
1
).ToString());
footerTemplate.EndText();
You use cb in 23 places in that code, and declare it in one. At no time do you ever assign a value to it, so it will remain null, and any usage of it will throw a "null reference exception".
At some point to need to create or locate a PdfContentByte instance, and set the cb variable to it - but I can't help you find that: it's your class after all!
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
cb
, in theOnOpenDocument
method:cb = writer.DirectContent;
Of course, there's no way of knowing whether that method ever gets called, whether it's called in the correct order, whether the assignment is ever reached, or whether the property actually returns an object. :)