2017-08-21 125 views
0

我想設置我的頁腳的背景顏色。我似乎無法找到任何有助於做到這一點的代碼。請參閱我的OnEndPage事件代碼。iTextSharp頁腳背景顏色

我已經試過cb.SetColorFill(BaseColor.LIGHT_GRAY);,但不工作:/

哪個屬性或方法來添加背景顏色到頁腳?

public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document) 
     { 
      base.OnEndPage(writer, document); 
      iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK); 
      iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK); 

      var headerImagePath = System.Web.HttpContext.Current.Server.MapPath("~/Content/misc/proactive-reg-form-header.jpg"); 
      var headerImage = Image.GetInstance(headerImagePath); 
      if (headerImage.Height > headerImage.Width) 
      { 
       //Maximum height is 800 pixels. 
       float percentage = 0.0f; 
       percentage = 700/headerImage.Height; 
       headerImage.ScalePercent(percentage * 100); 
      } 
      else 
      { 
       //Maximum width is 600 pixels. 
       float percentage = 0.0f; 
       percentage = 572/headerImage.Width; 
       headerImage.ScalePercent(percentage * 100); 
      } 

      //Create PdfTable object 
      PdfPTable pdfTab = new PdfPTable(1); 

      //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(headerImage); 
      //PdfPCell pdfCell3 = new PdfPCell(); 
      String text = "Page " + writer.PageNumber + " of "; 

      //Add paging to footer 
      { 
       cb.BeginText(); 
       cb.SetFontAndSize(bf, 10); 
       cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(30)); 
       cb.ShowText(text); 
       cb.EndText(); 
       float len = bf.GetWidthPoint(text, 10); 
       cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(30)); 

       //add image to footer 
       writer.DirectContent.AddImage(footerImage); 
      } 

      pdfCell2.HorizontalAlignment = Element.ALIGN_CENTER; 
      pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM; 

      pdfCell2.Border = 0; 

      pdfTab.AddCell(pdfCell2); 

      pdfTab.TotalWidth = 100f; 
      pdfTab.WidthPercentage = 100f; 

      //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, 10, document.PageSize.Height - 10, writer.DirectContent); 
      //set pdfContent value 

      if (document.PageNumber != 1) 
      { 
       //Move the pointer and draw line to separate header section from rest of page 
       cb.MoveTo(10, document.PageSize.Height - 60); 
       cb.LineTo((document.PageSize.Width - 10), document.PageSize.Height - 60); 
       cb.Stroke(); 
      } 

     } 

回答

0

Got it!

所以我修改了下面的代碼:

//Add paging to footer 
     { 
      cb.BeginText(); 
      cb.SetFontAndSize(bf, 10); 
      cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(30)); 
      cb.ShowText(text); 
      cb.EndText(); 
      float len = bf.GetWidthPoint(text, 10); 
      cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(30)); 

      //add image to footer 
      writer.DirectContent.AddImage(footerImage); 
     } 

//Add paging to footer 
     { 
      cb.BeginText(); 
      cb.SetFontAndSize(bf, 10); 
      cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(30)); 
      cb.ShowText(text); 
      cb.EndText(); 
      float len = bf.GetWidthPoint(text, 10); 
      cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(30)); 

      //this part adds the background color 
      cb.SetColorFill(BaseColor.LIGHT_GRAY); 
      cb.Rectangle(0, 0, document.PageSize.Width, 50); 
      cb.FillStroke(); 

      //add image to footer 
      writer.DirectContent.AddImage(footerImage); 
     }