using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; namespace IStation.DataFile { /// /// ITextSharp辅助类 /// public static class ITextSharpHelper { #pragma warning disable CA1416 // 验证平台兼容性 /// /// 在指定位置添加图片 /// /// PdfWriter /// /// 横坐标 /// 反向纵坐标 public static void AddImageAtPosition(this PdfWriter ws, System.Drawing.Image img, int x, int y) { var iTextImg = iTextSharp.text.Image.GetInstance(img, ImageFormat.Png); //若不缩放四分之三,图片显示过大。暂时未探索出图片为什么图片会变大,待解决。 iTextImg.ScalePercent(75); iTextImg.SetAbsolutePosition(x, y); ws.DirectContent.AddImage(iTextImg); } /// /// 在指定位置添加图片 /// /// PdfWriter /// 横坐标 /// 反向纵坐标 /// 路径 public static void AddImageAtPosition(this PdfWriter ws, string filePath, int x, int y) { var iTextImg = iTextSharp.text.Image.GetInstance(filePath); //若不缩放四分之三,图片显示过大。暂时未探索出图片为什么图片会变大,待解决。 iTextImg.ScalePercent(75); iTextImg.SetAbsolutePosition(x, y); ws.DirectContent.AddImage(iTextImg); } /// /// 在指定位置添加文字 /// /// PdfWriter /// 文本 /// 字体 /// 横坐标 /// 反向纵坐标 /// 旋转角度,逆时针 public static void AddTextAtPosition(this PdfWriter ws, string text, iTextSharp.text.Font font, float x, float y, float rotation = 0) { var cb = ws.DirectContent; var txt = new Phrase(text, font); ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, txt, x, y, 45); } /// /// 获取PdfImage /// /// /// public static iTextSharp.text.Image CreatePdfImage(this System.Drawing.Image img) { return iTextSharp.text.Image.GetInstance(img, ImageFormat.Png); } #pragma warning restore CA1416 // 验证平台兼容性 /// /// 获取背景颜色为白色的PdfImage /// /// /// public static iTextSharp.text.Image CreatePdfImageInWhite(this System.Drawing.Image img) { return iTextSharp.text.Image.GetInstance(img, BaseColor.White); } /// /// 获取Pdf基础字体 /// /// public static BaseFont GetBaseFont() { return BaseFont.CreateFont(@"C:\WINDOWS\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } /// /// /// /// /// public static BaseFont GetLocalBaseFont(string fontPath) { if (System.IO.File.Exists(fontPath)) { return BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } else { return GetBaseFont(); } } /// /// 字体设置 /// /// /// /// public static iTextSharp.text.Font CreateFontBySize(this BaseFont baseFont, float size) { return new iTextSharp.text.Font(baseFont, size); } /// /// 获取特定颜色大小的Pdf字体 /// /// /// /// /// public static iTextSharp.text.Font CreateFontBySize(this BaseFont baseFont, float size, System.Drawing.Color color) { var font = new iTextSharp.text.Font(baseFont, size); font.Color = new BaseColor(color); return font; } /// /// 创建表格 /// /// 表格的列数 /// 表格所占界面的百分比 /// public static PdfPTable CreateTable(int spanNum, float percentage = 100) { var pdfTable = new PdfPTable(spanNum); pdfTable.WidthPercentage = percentage; return pdfTable; } /// /// 创建表格 /// /// /// public static PdfPTable CreateTable(float[] relativeWidths) { return new PdfPTable(relativeWidths); } /// /// 获取Pdf基础颜色 /// /// /// public static BaseColor GetPdfBaseColor(this System.Drawing.Color color) { return new BaseColor(color); } /// /// 添加表格单元格 /// public static PdfPCell CreateTableCell ( this PdfPTable table, PdfPTable cellTable, int colSpan, int rowSpan, int border, float borderWidthLeft = -1, float borderWidthRight = -1, float borderWidthTop = -1, float borderWidthBottom = -1, float paddingLeft = -1, float paddingRight = -1, float paddingTop = -1, float paddingBottom = -1 ) { var cell = new PdfPCell(cellTable); cell.Padding = 0; cell.Colspan = colSpan; cell.Rowspan = rowSpan; cell.UseAscender = true; cell.Border = border; if (borderWidthLeft > 0) cell.BorderWidthLeft = borderWidthLeft; if (borderWidthRight > 0) cell.BorderWidthRight = borderWidthRight; if (borderWidthTop > 0) cell.BorderWidthTop = borderWidthTop; if (borderWidthBottom > 0) cell.BorderWidthBottom = borderWidthBottom; if (paddingLeft > 0) cell.PaddingLeft = paddingLeft; if (paddingRight > 0) cell.PaddingRight = paddingRight; if (paddingTop > 0) cell.PaddingTop = paddingTop; if (paddingBottom > 0) cell.PaddingBottom = paddingBottom; table.AddCell(cell); return cell; } /// /// 创建单元格 /// /// 创建单元格的表格 /// 单元格内容 /// 单元格的字体 /// 单元格所占列数 /// 单元格所占行数 /// 边框设置 /// 横向排列 /// 纵向排列 /// 最低高度 /// 固定高度 /// 单元格背景颜色 /// 左边框宽度 /// 有边框宽度 /// 上边框宽度 /// 下边框宽度 /// public static PdfPCell CreateCell ( this PdfPTable pdfTable, string content, iTextSharp.text.Font font, int colSpan, int rowSpan, int border, float minHeight = -1, float fixHeight = -1, int h_alignment = Element.ALIGN_CENTER, int v_alignment = Element.ALIGN_MIDDLE, BaseColor bc = null, float borderWidthLeft = -1, float borderWidthRight = -1, float borderWidthTop = -1, float borderWidthBottom = -1 ) { var cell = new PdfPCell(new Paragraph(content, font)); cell.Colspan = colSpan; cell.Rowspan = rowSpan; cell.UseAscender = true; cell.Border = border; cell.HorizontalAlignment = h_alignment; cell.VerticalAlignment = v_alignment; if (minHeight > 0) cell.MinimumHeight = minHeight; if (fixHeight > 0) cell.FixedHeight = fixHeight; if (bc != null) cell.BackgroundColor = bc; if (borderWidthLeft > 0) cell.BorderWidthLeft = borderWidthLeft; if (borderWidthRight > 0) cell.BorderWidthRight = borderWidthRight; if (borderWidthTop > 0) cell.BorderWidthTop = borderWidthTop; if (borderWidthBottom > 0) cell.BorderWidthBottom = borderWidthBottom; pdfTable.AddCell(cell); return cell; } /// /// 创建图片单元格 /// /// 创建单元格的表格 /// 单元格内的图片 /// 单元格所占列数 /// 单元格所占行数 /// 边框设置 /// 最低高度 /// 固定高度 /// 单元格背景颜色 /// 左边框宽度 /// 有边框宽度 /// 上边框宽度 /// 下边框宽度 /// /// /// /// public static PdfPCell CreateImageCell ( this PdfPTable pdfTable, System.Drawing.Image img, int colSpan, int rowSpan, int border, float minHeight = -1, float fixHeight = -1, int h_alignment = Element.ALIGN_CENTER, int v_alignment = Element.ALIGN_MIDDLE, BaseColor bc = null, float borderWidthLeft = -1, float borderWidthRight = -1, float borderWidthTop = -1, float borderWidthBottom = -1 ) { var cell = new PdfPCell(); cell.HorizontalAlignment = h_alignment; cell.VerticalAlignment = v_alignment; var pdfImg = img.CreatePdfImage(); pdfImg.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; cell.Image = pdfImg; cell.Colspan = colSpan; cell.Rowspan = rowSpan; cell.Border = border; if (minHeight > 0) cell.MinimumHeight = minHeight; if (fixHeight > 0) cell.FixedHeight = fixHeight; if (bc != null) cell.BackgroundColor = bc; if (borderWidthLeft > 0) cell.BorderWidthLeft = borderWidthLeft; if (borderWidthRight > 0) cell.BorderWidthRight = borderWidthRight; if (borderWidthTop > 0) cell.BorderWidthTop = borderWidthTop; if (borderWidthBottom > 0) cell.BorderWidthBottom = borderWidthBottom; pdfTable.AddCell(cell); return cell; } } }