| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace MvvmScaffoldFrame48.DLL.FileTools
- {
- public class PDFGenerate
- {
- #region 成员变量
- /// <summary>
- /// 是否保存标志量
- /// </summary>
- public bool IsSave = false;
- /// <summary>
- /// 字体位置
- /// </summary>
- string fontPath = @"C:\Windows\Fonts\MSYH.ttc,0"; // 微软雅黑
- #endregion
- #region 实例
- /// <summary>
- /// 文档对象
- /// </summary>
- private Document document = new Document(PageSize.A4);
- /// <summary>
- /// 字体实例
- /// </summary>
- BaseFont baseFont = null;
- #endregion
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="outputPath">PDF生成位置</param>
- public PDFGenerate(string outputPath)
- {
- PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
- document.Open();
- baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- }
- #endregion
- #region 公共方法
- /// <summary>
- /// 插入文本
- /// </summary>
- /// <param name="StrText">插入的文本</param>
- public void InsertText(string StrText)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, 12);
- Paragraph TextValue = new Paragraph(StrText, FontValue);
- TextValue.Alignment = Element.ALIGN_CENTER;
- document.Add(TextValue);
- }
- /// <summary>
- /// 插入文本
- /// </summary>
- /// <param name="StrText">插入的文本</param>
- /// <param name="FontSize">字体的字号</param>
- public void InsertText(string StrText, int FontSize)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, FontSize);
- Paragraph TextValue = new Paragraph(StrText, FontValue);
- TextValue.Alignment = Element.ALIGN_CENTER;
- document.Add(TextValue);
- }
- /// <summary>
- /// 插入文本
- /// </summary>
- /// <param name="StrText">插入的文本</param>
- /// <param name="FontSize">字体的字号</param>
- /// <param name="Alignment">对齐方式</param>
- public void InsertText(string StrText, int FontSize, int Alignment)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, FontSize);
- Paragraph TextValue = new Paragraph(StrText, FontValue);
- if (Alignment == 1)
- {
- TextValue.Alignment = Element.ALIGN_LEFT;
- }
- else if (Alignment == 2)
- {
- TextValue.Alignment = Element.ALIGN_RIGHT;
- }
- else if (Alignment == 3)
- {
- TextValue.Alignment = Element.ALIGN_CENTER;
- }
- else
- {
- Console.WriteLine("未定义对齐方式");
- }
- document.Add(TextValue);
- }
- /// <summary>
- /// 插入图片
- /// </summary>
- /// <param name="imagePath">图片路径</param>
- public void InsertImage(string imagePath)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, 12);
- // 插入图片
- try
- {
- Image img = Image.GetInstance(imagePath);
- img.ScaleToFit(500, 300);
- img.Alignment = Element.ALIGN_CENTER;
- document.Add(img);
- }
- catch (Exception ex)
- {
- document.Add(new Paragraph("图片加载失败: " + ex.Message));
- }
- }
- /// <summary>
- /// 插入图片
- /// </summary>
- /// <param name="imagePath">图片路径</param>
- public void InsertImage(byte[] imageBytes)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, 12);
- // 插入图片
- try
- {
- Image img = Image.GetInstance(imageBytes);
- img.ScaleToFit(500, 300);
- img.Alignment = Element.ALIGN_CENTER;
- document.Add(img);
- }
- catch (Exception ex)
- {
- document.Add(new Paragraph("图片加载失败: " + ex.Message));
- }
- }
- /// <summary>
- /// 插入图片
- /// </summary>
- /// <param name="imagePath">图片路径</param>
- /// <param name="width">图片宽度</param>
- /// <param name="height">图片高度</param>
- public void InsertImage(string imagePath, float width, float height)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, 12);
- // 插入图片
- try
- {
- Image img = Image.GetInstance(imagePath);
- img.ScaleToFit(width, height);
- img.Alignment = Element.ALIGN_CENTER;
- document.Add(img);
- }
- catch (Exception ex)
- {
- document.Add(new Paragraph("图片加载失败: " + ex.Message));
- }
- }
- /// <summary>
- /// 插入图片
- /// </summary>
- /// <param name="imagePath">图片路径</param>
- /// <param name="width">图片宽度</param>
- /// <param name="height">图片高度</param>
- public void InsertImage(byte[] imageBytes, float width, float height)
- {
- if (IsSave)
- {
- return;
- }
- Font FontValue = new Font(baseFont, 12);
- // 插入图片
- try
- {
- Image img = Image.GetInstance(imageBytes);
- img.ScaleToFit(width, height);
- img.Alignment = Element.ALIGN_CENTER;
- document.Add(img);
- }
- catch (Exception ex)
- {
- document.Add(new Paragraph("图片加载失败: " + ex.Message));
- }
- }
- /// <summary>
- /// 插入表格
- /// </summary>
- /// <param name="dataList">数据列表</param>
- /// <param name="tableName">表名</param>
- public void InsertTable<T>(List<T> dataList, string tableName = "表头") where T : class
- {
- if (IsSave)
- {
- return;
- }
- if (dataList == null || dataList.Count == 0)
- {
- return; // 如果数据为空则直接返回
- }
- Font FontValue = new Font(baseFont, 12);
- // 使用反射获取类的属性作为列
- var properties = typeof(T).GetProperties();
- PdfPTable table = new PdfPTable(properties.Length);
- table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
- // 添加表名作为表头(跨所有列)
- PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
- cell.Colspan = properties.Length;
- cell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(cell);
- // 添加属性名作为列标题
- foreach (var prop in properties)
- {
- PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
- headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell);
- }
- // 添加数据行
- foreach (var data in dataList)
- {
- foreach (var prop in properties)
- {
- var value = prop.GetValue(data)?.ToString() ?? "";
- table.AddCell(new Phrase(value, FontValue));
- }
- }
- // 将表格添加到文档
- document.Add(table);
- }
- /// <summary>
- /// 插入表格
- /// </summary>
- /// <param name="dataList">数据列表</param>
- /// <param name="tableName">表名</param>
- public void InsertTable<T>(List<T> dataList, List<string> Colspan, string tableName = "表头") where T : class
- {
- if (IsSave)
- {
- return;
- }
- if (dataList == null || dataList.Count == 0)
- {
- return; // 如果数据为空则直接返回
- }
- Font FontValue = new Font(baseFont, 12);
- // 使用反射获取类的属性作为列
- var properties = typeof(T).GetProperties();
- if (properties.Length != Colspan.Count) { return; }
- PdfPTable table = new PdfPTable(Colspan.Count);
- table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
- // 添加表名作为表头(跨所有列)
- PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
- cell.Colspan = Colspan.Count;
- cell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(cell);
- // 添加属性名作为列标题
- foreach (var prop in Colspan)
- {
- PdfPCell headerCell = new PdfPCell(new Phrase(prop, FontValue));
- headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell);
- }
- // 添加数据行
- foreach (var data in dataList)
- {
- foreach (var prop in properties)
- {
- var value = prop.GetValue(data)?.ToString() ?? "";
- table.AddCell(new Phrase(value, FontValue));
- }
- }
- // 将表格添加到文档
- document.Add(table);
- }
- /// <summary>
- /// 插入表格
- /// </summary>
- /// <param name="dataList">数据列表</param>
- /// <param name="tableName">表名</param>
- public void InsertTable<T>(T data, string tableName = "表头") where T : class
- {
- if (IsSave)
- {
- return;
- }
- if (data == null)
- {
- return; // 如果数据为空则直接返回
- }
- Font FontValue = new Font(baseFont, 12);
- // 使用反射获取类的属性作为列
- var properties = typeof(T).GetProperties();
- PdfPTable table = new PdfPTable(2);
- table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
- // 添加表名作为表头(跨所有列)
- PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
- cell.Colspan = 2;
- cell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(cell);
- PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
- headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell1);
- PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
- headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell2);
- //// 添加属性名作为列标题
- foreach (var prop in properties)
- {
- PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
- headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell);
- var value = prop.GetValue(data)?.ToString() ?? "";
- PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
- RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(RowValueCell);
- }
- // 将表格添加到文档
- document.Add(table);
- }
- /// <summary>
- /// 插入表格
- /// </summary>
- /// <param name="dataList">数据列表</param>
- /// <param name="tableName">表名</param>
- public bool InsertTable<T>(T data, List<string> Rowspan, string tableName = "表头") where T : class
- {
- bool Result = false;
- if (IsSave)
- {
- return false;
- }
- if (data == null)
- {
- return false; // 如果数据为空则直接返回
- }
- Font FontValue = new Font(baseFont, 12);
- // 使用反射获取类的属性作为列
- var properties = typeof(T).GetProperties();
- PdfPTable table = new PdfPTable(2);
- table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
- // 添加表名作为表头(跨所有列)
- PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
- cell.Colspan = 2;
- cell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(cell);
- PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
- headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell1);
- PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
- headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(headerCell2);
- if (Rowspan.Count != properties.Length) { return false; }
- for (int i = 0; i < properties.Length; i++)
- {
- PdfPCell RowNameCell = new PdfPCell(new Phrase(Rowspan[i], FontValue));
- RowNameCell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(RowNameCell);
- var value = properties[i].GetValue(data)?.ToString() ?? "";
- PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
- RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(RowValueCell);
- }
- // 将表格添加到文档
- document.Add(table);
- Result = true;
- return Result;
- }
- /// <summary>
- /// 插入空白行
- /// </summary>
- public void InsertNewLine()
- {
- if (IsSave)
- {
- return;
- }
- document.Add(Chunk.NEWLINE);
- }
- /// <summary>
- /// 保存PDF文件
- /// </summary>
- public void SavePDF()
- {
- document.Close();
- IsSave = true;
- }
- #endregion
- }
- }
|