1.创建页眉页脚
SizeF pageSize = pdf.Pages[0].Size == null ? PdfPageSize.A4 : pdf.Pages[0].Size; //画笔 PdfPen pen = new PdfPen(pdf.StyleConfig.PenColor, pdf.StyleConfig.PenWidth); //文字样式和位置 字体和对齐方式 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(pdf.StyleConfig.HeaderFont, pdf.StyleConfig.HeaderEmSize)); for (int i = 0; i < pdf.Pages.Count; i++) { //偏移变量 float x = pdf.PdfMargins.Left; float y = 0; //图片size float width = 0; float height = 0; //绘制图片 if (!string.IsNullOrEmpty(imageUrl)) { PdfImage headerImage = PdfImage.FromFile(imageUrl); width = headerImage.Width / 4; height = headerImage.Height / 4; y = pdf.PdfMargins.Top; pdf.Pages[i].Canvas.DrawImage(headerImage, x, y, width, height); } //绘制线段 float x1 = pdf.PdfMargins.Left + width + 30; float x2 = pageSize.Width - pdf.PdfMargins.Right; float y1 = pdf.PdfMargins.Top + height; pdf.Pages[i].Canvas.DrawLine(pen, x1, y1, x2, y1); if (!string.IsNullOrEmpty(text)) { // 绘入文字 SizeF size = font.MeasureString(text); x = pageSize.Width - pdf.PdfMargins.Right - size.Width; y = height - size.Height + (pdf.PdfMargins.Top / 3 * 2); pdf.Pages[i].Canvas.DrawString(text, font, PdfBrushes.Black, x, y); } } }
2.创建页脚
if (!string.IsNullOrEmpty(footerText)) { //获取页面大小 SizeF pageSize = pdf.Pages[0].Size == null ? PdfPageSize.A4 : pdf.Pages[0].Size; //偏移量 float x = 0; float y = 0; //画笔 PdfPen pen = new PdfPen(pdf.StyleConfig.PenColor, pdf.StyleConfig.PenWidth); //文字样式和位置 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(pdf.StyleConfig.FooterFont, pdf.StyleConfig.FooterEmSize), true); for (int i = 0; i < pdf.Pages.Count; i++) { //绘制文字 SizeF size = font.MeasureString(footerText); x = (pageSize.Width - size.Width) / 2; y = pageSize.Height - pdf.PdfMargins.Bottom - size.Height - 2; pdf.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.Black, x, y); } }
3.创建水印
if (!string.IsNullOrEmpty(text)) { //创建True Type字体 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(Pdf.StyleConfig.WaterMarkFont, Pdf.StyleConfig.WaterMarkEmSize), true); //测量文字所占的位置大小 SizeF size = font.MeasureString(text); //计算偏移量 float x = (float)(size.Width * System.Math.Sqrt(2) / 4); float y = (float)(size.Height * System.Math.Sqrt(2) / 4); //遍历文档页 foreach (PdfPageBase page in Pdf.Pages) { //设置透明度 page.Canvas.SetTransparency(Pdf.StyleConfig.Transparency); //将坐标系向右,向下平移 page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - x - y, page.Canvas.Size.Height / 2 + x - y); //将坐标系逆时针旋转45度 page.Canvas.RotateTransform(Pdf.StyleConfig.Angle); //绘制文本 page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0); } }
4.二维码
if (!string.IsNullOrEmpty(data)) { SizeF pageSize = pdf.Pages[0].Size == null ? PdfPageSize.A4 : pdf.Pages[0].Size; //创建BarcodeSettings对象 BarcodeSettings.ApplyKey("keyNumber"); BarcodeSettings settings = new BarcodeSettings(); //设置条码类型为二维码 settings.Type = BarCodeType.QRCode; //设置二维码数据 settings.Data = data; settings.ShowText = false; //设置数据类型 settings.QRCodeDataMode = QRCodeDataMode.Auto; //设置宽度 settings.X = 0.7f; BarCodeGenerator generator = new BarCodeGenerator(settings); Image image = generator.GenerateImage(); //绘制二维码图形到PDF PdfImage pdfImage = PdfImage.FromImage(image); float x = pageSize.Width - pdf.PdfMargins.Right - pdfImage.PhysicalDimension.Width; float y = 5; if (!string.IsNullOrEmpty(imageUrl)) { PdfImage headerImage = PdfImage.FromFile(imageUrl); y += headerImage.Height / 4; } y += pdf.PdfMargins.Top; pdf.Pages[0].Canvas.DrawImage(pdfImage, x, y); }