添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
// we will put the final number of pages in a template PdfTemplate headerTemplate, footerTemplate; // 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 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); // Create PdfTable object PdfPTable pdfTab = new PdfPTable( 3 ); // We will have to create separate cells to include image logo and 2 separate strings // Row 1 PdfPCell pdfCell1 = new PdfPCell(); PdfPCell pdfCell2 = new PdfPCell(p1Header); PdfPCell pdfCell3 = new PdfPCell(); String text = " Page " + writer.PageNumber + " of " ; // Add paging to header 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 ); // Adds "12" in Page 1 of 12 cb.AddTemplate(headerTemplate, PdfDoc.PageSize.GetRight( 200 ) + len, PdfDoc.PageSize.GetTop( 45 )); // Add paging to footer 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 )); // Row 2 PdfPCell pdfCell4 = new PdfPCell( new Phrase( " Sub Header Description" , baseFontNormal)); // Row 3 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)); // set the alignment of all three cells and set border to 0 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 ; // add all three cells into PdfTable 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.HorizontalAlignment = Element.ALIGN_CENTER; // call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable // first param is start row. -1 indicates there is no end row and all the rows to be included to write // Third and fourth param is x and y position to start writing pdfTab.WriteSelectedRows( 0 , -1, 40 , PdfDoc.PageSize.Height - 30 , writer.DirectContent); // set pdfContent value // Move the pointer and draw line to separate header section from rest of page cb.MoveTo( 40 , PdfDoc.PageSize.Height - 100 ); cb.LineTo(PdfDoc.PageSize.Width - 40 , PdfDoc.PageSize.Height - 100 ); cb.Stroke(); // Move the pointer and draw line to separate footer section from rest of page 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!
I can see one assignment to cb , in the OnOpenDocument 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. :)
  • 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.
  •