1 static void SetAllHeaderFooter(string inputPath) 2 { 3 4 PdfReader reader=new PdfReader(inputPath); 5 PdfStamper stamp=new PdfStamper(reader , new FileStream(Guid.NewGuid().ToString("N")+".pdf" , FileMode.Create)); 6 7 8 for(int i=1 ; i<=reader.NumberOfPages ; i++) 9 { 10 AddHeaderFooter(stamp , i , reader.NumberOfPages); 11 } 12 13 stamp.Close(); 14 reader.Close(); 15 16 }
1 static void AddHeaderFooter(PdfStamper stamp,int pageNum,int pageCount) 2 { 3 4 PdfContentByte cb =stamp.GetOverContent(pageNum); 5 6 Color green=new Color(0 , 131 , 34); 7 BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA , BaseFont.CP1252 , BaseFont.NOT_EMBEDDED); 8 9 var header=new Rectangle(50 , 795 , 570 , 815); 10 header.BackgroundColor=green; 11 header.BorderColor=green; 12 cb.Rectangle(header); 13 cb.Stroke(); 14 15 var footer=new Rectangle(50 , 35 , 570 , 55); 16 footer.BackgroundColor=green; 17 footer.BorderColor=green; 18 cb.Rectangle(footer); 19 cb.Stroke(); 20 21 cb.SetRGBColorFill(255 , 255 , 255); 22 23 cb.BeginText(); 24 cb.SetFontAndSize(bf , 12); 25 cb.SetTextMatrix(70 , 800); 26 27 cb.ShowText("U als ondernemer"); 28 cb.SetTextMatrix(550-bf.GetWidthPoint("Resultaten" , 12) , 800); 29 cb.ShowText("Resultaten"); 30 cb.EndText(); 31 32 String pageText =string.Format("Page {0} of {1}",pageNum,pageCount); 33 float pageLen = bf.GetWidthPoint(pageText , 12); 34 cb.BeginText(); 35 cb.SetFontAndSize(bf , 12); 36 cb.SetTextMatrix(280 , 40); 37 cb.ShowText(pageText); 38 cb.EndText(); 39 cb.ResetRGBColorFill(); 40 41 42 43 }
最終結果是長這樣的:ui