用iTextSharp制作具有复杂页眉的pdf文件

 

红色框里面为要实现的页眉内容,用c#实现,代码如下:

  前面加
  using iTextSharp.text;
  using iTextSharp.text.pdf;

  用4个Chunk组成一个Phrase来实现,主要理解这部分即可。
 

private void GetPdfFile()
  {
  iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.A4, 30, 10, 36, 15);
  string sRptFile = "file\\Transducer Status Report" + "_" + Session.SessionID.ToString() + ".pdf";
  string sFile = Server.MapPath(Request.ApplicationPath) + "\\Report\\" + sRptFile;
  PdfWriter.getInstance(document, new FileStream(sFile, FileMode.Create));
  document.addTitle("Transducer Status Report");
  document.addSubject("Transducer Status Report");
  document.addKeywords("Transducer Status Report");
  document.addCreator("MT");
  document.addAuthor("stl");
  document.addHeader("Expires", "0");

  // 页眉页脚
  string space1 = " ".PadLeft(48, ' ');
  string suser = "User: " + Session["s_LogName"].ToString();
  string space2 = " ".PadLeft(170 - suser.Length, ' ');

  iTextSharp.text.Chunk chunk1 = new Chunk(space1 + "Transducer Status Report\r\n", FontFactory.getFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD));
  iTextSharp.text.Chunk chunk2 = new Chunk(suser + space2 + "Date: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "\r\n", FontFactory.getFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.NORMAL));
  iTextSharp.text.Chunk chunk3 = new Chunk("\r\n", FontFactory.getFont(FontFactory.HELVETICA, 5, iTextSharp.text.Font.NORMAL));
  iTextSharp.text.Chunk chunk4 = new Chunk(strQueryRange, FontFactory.getFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.BOLD));

  Phrase ph = new Phrase(9);
  ph.Add(chunk1);
  ph.Add(chunk2);
  ph.Add(chunk3);
  ph.Add(chunk4);

  iTextSharp.text.HeaderFooter header = new iTextSharp.text.HeaderFooter(ph, false);
  header.Border = iTextSharp.text.Rectangle.NO_BORDER;
  document.Header = header;

  iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new Phrase(4, "Page: ", FontFactory.getFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.NORMAL)), true);
  footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
  document.Footer = footer;

  document.Open();

  // 报表之表格部分
  iTextSharp.text.pdf.PdfPTable datatable = new PdfPTable(8);
  float[] headerwidths = {70, 100, 130, 320, 50, 50, 70, 60};
  datatable.setWidths(headerwidths);
  datatable.WidthPercentage = 100;
  datatable.HeaderRows = 1;

  for (int i = 0; i < arrHeader.Length; i ++)
  {
  ph = new Phrase(arrHeader[i], FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.BOLD));
  iTextSharp.text.pdf.PdfPCell cell1 = new PdfPCell(ph);
  cell1.HorizontalAlignment = Element.ALIGN_CENTER;
  cell1.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
  datatable.addCell(cell1);
  }

  PMWebCmd pwc = new PMWebCmd();
  string sValue;
  float fValue;

  for (int i = 0; i < DS.Tables["QueryData"].DefaultView.Count; i++)
  {
  sValue = DS.Tables["QueryData"].DefaultView[i]["ShowID"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));
  sValue = DS.Tables["QueryData"].DefaultView[i]["COName"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));
  sValue = DS.Tables["QueryData"].DefaultView[i]["CableName"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));
  sValue = DS.Tables["QueryData"].DefaultView[i]["ChnName"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));
  sValue = DS.Tables["QueryData"].DefaultView[i]["InputNo"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));
  sValue = DS.Tables["QueryData"].DefaultView[i]["AddressID"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));

  sValue = DS.Tables["QueryData"].DefaultView[i]["LastValue"].ToString();
  string sChnType = DS.Tables["QueryData"].DefaultView[i]["ChnType"].ToString();
  if (sChnType == "1")
  {
  fValue = Convert.ToSingle(sValue);
  sValue = (fValue >= pwc.Get_INVALIDVALUE()) ? "---" : pwc.GetDataFormat(false, fValue);
  }
  else if (sChnType == "2")
  {
  byte iVal = Convert.ToByte(sValue);
  sValue = pwc.GetBinInputReadStr(iVal);
  }
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));

  sValue = DS.Tables["QueryData"].DefaultView[i]["Status"].ToString();
  datatable.addCell(new Phrase(sValue, FontFactory.getFont(FontFactory.HELVETICA, (float)7.5, iTextSharp.text.Font.NORMAL)));
  }

  document.Add(datatable);
  document.Close();
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-01-18
  • 2022-12-23
  • 2021-12-02
  • 2021-04-15
  • 2022-12-23
猜你喜欢
  • 2021-09-22
  • 2021-12-21
  • 2022-12-23
  • 2021-10-31
  • 2021-12-04
  • 2022-02-09
相关资源
相似解决方案