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
|
{
|
/// <summary>
|
/// ITextSharp辅助类
|
/// </summary>
|
public static class ITextSharpHelper
|
{
|
#pragma warning disable CA1416 // 验证平台兼容性
|
/// <summary>
|
/// 在指定位置添加图片
|
/// </summary>
|
/// <param name="ws">PdfWriter</param>
|
/// <param name="img"></param>
|
/// <param name="x">横坐标</param>
|
/// <param name="y">反向纵坐标</param>
|
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);
|
}
|
|
/// <summary>
|
/// 在指定位置添加图片
|
/// </summary>
|
/// <param name="ws">PdfWriter</param>
|
/// <param name="x">横坐标</param>
|
/// <param name="y">反向纵坐标</param>
|
/// <param name="filePath">路径</param>
|
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);
|
}
|
|
/// <summary>
|
/// 在指定位置添加文字
|
/// </summary>
|
/// <param name="ws">PdfWriter</param>
|
/// <param name="text">文本</param>
|
/// <param name="font">字体</param>
|
/// <param name="x">横坐标</param>
|
/// <param name="y">反向纵坐标</param>
|
/// <param name="rotation">旋转角度,逆时针</param>
|
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);
|
}
|
|
/// <summary>
|
/// 获取PdfImage
|
/// </summary>
|
/// <param name="img"></param>
|
/// <returns></returns>
|
public static iTextSharp.text.Image CreatePdfImage(this System.Drawing.Image img)
|
{
|
return iTextSharp.text.Image.GetInstance(img, ImageFormat.Png);
|
}
|
#pragma warning restore CA1416 // 验证平台兼容性
|
/// <summary>
|
/// 获取背景颜色为白色的PdfImage
|
/// </summary>
|
/// <param name="img"></param>
|
/// <returns></returns>
|
public static iTextSharp.text.Image CreatePdfImageInWhite(this System.Drawing.Image img)
|
{
|
return iTextSharp.text.Image.GetInstance(img, BaseColor.White);
|
}
|
|
/// <summary>
|
/// 获取Pdf基础字体
|
/// </summary>
|
/// <returns></returns>
|
public static BaseFont GetBaseFont()
|
{
|
return BaseFont.CreateFont(@"C:\WINDOWS\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="fontPath"></param>
|
/// <returns></returns>
|
public static BaseFont GetLocalBaseFont(string fontPath)
|
{
|
if (System.IO.File.Exists(fontPath))
|
{
|
return BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
|
}
|
else
|
{
|
return GetBaseFont();
|
}
|
}
|
|
/// <summary>
|
/// 字体设置
|
/// </summary>
|
/// <param name="baseFont"></param>
|
/// <param name="size"></param>
|
/// <returns></returns>
|
public static iTextSharp.text.Font CreateFontBySize(this BaseFont baseFont, float size)
|
{
|
return new iTextSharp.text.Font(baseFont, size);
|
}
|
|
/// <summary>
|
/// 获取特定颜色大小的Pdf字体
|
/// </summary>
|
/// <param name="baseFont"></param>
|
/// <param name="size"></param>
|
/// <param name="color"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 创建表格
|
/// </summary>
|
/// <param name="spanNum">表格的列数</param>
|
/// <param name="percentage">表格所占界面的百分比</param>
|
/// <returns></returns>
|
public static PdfPTable CreateTable(int spanNum, float percentage = 100)
|
{
|
var pdfTable = new PdfPTable(spanNum);
|
pdfTable.WidthPercentage = percentage;
|
return pdfTable;
|
}
|
|
/// <summary>
|
/// 创建表格
|
/// </summary>
|
/// <param name="relativeWidths"></param>
|
/// <returns></returns>
|
public static PdfPTable CreateTable(float[] relativeWidths)
|
{
|
return new PdfPTable(relativeWidths);
|
}
|
|
/// <summary>
|
/// 获取Pdf基础颜色
|
/// </summary>
|
/// <param name="color"></param>
|
/// <returns></returns>
|
public static BaseColor GetPdfBaseColor(this System.Drawing.Color color)
|
{
|
return new BaseColor(color);
|
}
|
|
/// <summary>
|
/// 添加表格单元格
|
/// </summary>
|
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;
|
}
|
|
/// <summary>
|
/// 创建单元格
|
/// </summary>
|
/// <param name="pdfTable">创建单元格的表格</param>
|
/// <param name="content">单元格内容</param>
|
/// <param name="font">单元格的字体</param>
|
/// <param name="colSpan">单元格所占列数</param>
|
/// <param name="rowSpan">单元格所占行数</param>
|
/// <param name="border">边框设置</param>
|
/// <param name="h_alignment">横向排列</param>
|
/// <param name="v_alignment">纵向排列</param>
|
/// <param name="minHeight">最低高度</param>
|
/// <param name="fixHeight">固定高度</param>
|
/// <param name="bc">单元格背景颜色</param>
|
/// <param name="borderWidthLeft">左边框宽度</param>
|
/// <param name="borderWidthRight">有边框宽度</param>
|
/// <param name="borderWidthTop">上边框宽度</param>
|
/// <param name="borderWidthBottom">下边框宽度</param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 创建图片单元格
|
/// </summary>
|
/// <param name="pdfTable">创建单元格的表格</param>
|
/// <param name="img">单元格内的图片</param>
|
/// <param name="colSpan">单元格所占列数</param>
|
/// <param name="rowSpan">单元格所占行数</param>
|
/// <param name="border">边框设置</param>
|
/// <param name="minHeight">最低高度</param>
|
/// <param name="fixHeight">固定高度</param>
|
/// <param name="bc">单元格背景颜色</param>
|
/// <param name="borderWidthLeft">左边框宽度</param>
|
/// <param name="borderWidthRight">有边框宽度</param>
|
/// <param name="borderWidthTop">上边框宽度</param>
|
/// <param name="borderWidthBottom">下边框宽度</param>
|
/// <param name="h_alignment"></param>
|
/// /// <param name="v_alignment"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
|
|
}
|
}
|