267 lines
10 KiB
C#
267 lines
10 KiB
C#
using static System.Net.Mime.MediaTypeNames;
|
|
using iText.Kernel.Pdf;
|
|
using iText.Kernel.Geom;
|
|
using iText.IO.Image;
|
|
using iText.Layout.Element;
|
|
using Image = iText.Layout.Element.Image;
|
|
using iText.Layout.Properties;
|
|
using iText.Layout;
|
|
using iText.Kernel.Font;
|
|
using iText.IO.Font;
|
|
using iText.Kernel.Colors;
|
|
|
|
namespace PDFGenerateApi
|
|
{
|
|
public class PdfOptHelper
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="pagetitle"></param>
|
|
/// <param name="headers"></param>
|
|
/// <param name="majors"></param>
|
|
/// <param name="dataModels"></param>
|
|
/// <returns></returns>
|
|
public static MemoryStream CreatePdf(string pagetitle, List<string> headers, List<string> majors, List<dataModel> dataModels,DateTime dtnow)
|
|
{
|
|
// 创建内存流
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
// 创建 PDF Writer
|
|
PdfWriter writer = new PdfWriter(stream, new WriterProperties().SetCompressionLevel(CompressionConstants.BEST_COMPRESSION));
|
|
PdfDocument pdf = new PdfDocument(writer);
|
|
// 设置页面大小为横向 A4
|
|
PageSize pageSize = PageSize.A4.Rotate();
|
|
// 设置页面大小为 A4
|
|
pdf.SetDefaultPageSize(pageSize);
|
|
Document document = new Document(pdf);
|
|
// 加载中文字体
|
|
string fontsPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "fonts", "AlimamaDongFangDaKai-Regular.otf");
|
|
//if (!File.Exists(fontsPath))
|
|
//{
|
|
// bool aa = true;
|
|
// // _logger.LogError($"Font file not found at path: {fontsPath}");
|
|
// // return NotFound("Font file not found.");
|
|
//}
|
|
PdfFont font = PdfFontFactory.CreateFont(fontsPath, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
|
|
// 加载 logo 图像
|
|
string logoPath = "https://i5.youzy.cn/util4y/media/files/20231026/7840b6f1f30c413eade15babc70be26e_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230811154443.png"; // 确保 logo.png 放在 wwwroot 目录下
|
|
ImageData imageData = ImageDataFactory.Create(logoPath);
|
|
Image logo = new Image(imageData);
|
|
logo.SetWidth(124);
|
|
logo.SetHeight(40);
|
|
// 将 logo 添加到文档中
|
|
document.Add(logo);
|
|
Paragraph title = CreateTitle(font, pagetitle);
|
|
document.Add(title);
|
|
// 添加标题下方内容
|
|
Paragraph subtitle = CreateSubTitle(font, string.Format("最后修改时间:{0}", dtnow.ToString("yyyy-MM-dd HH:mm")));
|
|
document.Add(subtitle);
|
|
dataModels.ForEach(a =>
|
|
{
|
|
Table universitytable = HeaderTable(font, 9, headers, new List<dataModel>() { a });
|
|
universitytable.SetMarginTop(20);
|
|
document.Add(universitytable);
|
|
Table majortable = MajorTable(font, 9, majors, a.majorModels);
|
|
document.Add(majortable);
|
|
// 手动分页并添加新的表格,不带表头
|
|
// document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
|
|
});
|
|
|
|
//专业
|
|
|
|
// 关闭文档
|
|
document.Close();
|
|
// 返回 PDF 文件
|
|
return stream;
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标题
|
|
/// </summary>
|
|
/// <param name="font"></param>
|
|
/// <param name="titletext"></param>
|
|
/// <returns></returns>
|
|
private static Paragraph CreateTitle(PdfFont font, string titletext)
|
|
{
|
|
Paragraph title = new Paragraph(titletext)
|
|
.SetTextAlignment(TextAlignment.CENTER)
|
|
.SetFont(font)
|
|
.SetBold()
|
|
.SetFontSize(16)
|
|
.SetMarginTop(20)
|
|
.SetMarginBottom(5);
|
|
return title;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建副标
|
|
/// </summary>
|
|
/// <param name="font"></param>
|
|
/// <param name="titletext"></param>
|
|
/// <returns></returns>
|
|
private static Paragraph CreateSubTitle(PdfFont font, string titletext)
|
|
{
|
|
Paragraph title = new Paragraph(titletext)
|
|
.SetTextAlignment(TextAlignment.CENTER)
|
|
.SetFont(font)
|
|
.SetFontColor(ColorConstants.GRAY) // 设置字体颜色为灰色
|
|
.SetFontSize(12)
|
|
.SetMarginBottom(20);
|
|
return title;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// table表头
|
|
/// </summary>
|
|
/// <param name="tb"></param>
|
|
/// <param name="headers"></param>
|
|
/// <returns></returns>
|
|
private static Table HeaderTable(PdfFont font, int tb, List<string> headers, List<dataModel> dataList)
|
|
{
|
|
float[] columnWidths = { 1, 1, 2, 3, 2, 2, 2, 1, 1 };
|
|
Table table = new Table(columnWidths);
|
|
table.SetWidth(UnitValue.CreatePercentValue(100));
|
|
|
|
|
|
int h = 0;
|
|
foreach (var header in headers)
|
|
{
|
|
h++;
|
|
if (h == 4)
|
|
{
|
|
table.AddHeaderCell(new Cell().Add(new Paragraph(header).SetWidth(120).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
|
}
|
|
else
|
|
{
|
|
table.AddHeaderCell(new Cell().Add(new Paragraph(header).SetWidth(50).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
|
}
|
|
}
|
|
dataList.ForEach(a =>
|
|
{
|
|
table.AddCell(new Cell().Add(new Paragraph(a.id.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.probability).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.universityCode).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.universityName).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.planCount).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.history).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
a.years.ForEach(a =>
|
|
{
|
|
table.AddCell(new Cell().Add(new Paragraph(a).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
});
|
|
});
|
|
|
|
|
|
return table;
|
|
}
|
|
|
|
|
|
private static Table MajorTable(PdfFont font, int tb, List<string> headers, List<dataMajorModel> dataList)
|
|
{
|
|
float[] columnWidths = { 1, 1, 2, 3, 2, 2, 2, 1, 1 };
|
|
Table table = new Table(columnWidths);
|
|
table.SetWidth(UnitValue.CreatePercentValue(100));
|
|
int h = 0;
|
|
foreach (var header in headers)
|
|
{
|
|
h++;
|
|
if (h == 4)
|
|
{
|
|
table.AddCell(new Cell().Add(new Paragraph(header).SetWidth(85).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
|
}
|
|
else
|
|
{
|
|
table.AddCell(new Cell().Add(new Paragraph(header).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
|
}
|
|
}
|
|
dataList.ForEach(a =>
|
|
{
|
|
table.AddCell(new Cell().Add(new Paragraph(a.id.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.probability).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.majorCode).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.mjaorName).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.planCount).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
table.AddCell(new Cell().Add(new Paragraph(a.history).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
a.years.ForEach(a =>
|
|
{
|
|
table.AddCell(new Cell().Add(new Paragraph(a).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
|
});
|
|
});
|
|
return table;
|
|
}
|
|
|
|
|
|
|
|
public class dataModel
|
|
{
|
|
public int id { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string probability { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string universityCode { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string universityName { get; set; }
|
|
|
|
|
|
public string planCount { get; set; }
|
|
|
|
|
|
public string history { get; set; }
|
|
|
|
public List<string> years { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public List<dataMajorModel> majorModels { get; set; }
|
|
}
|
|
|
|
|
|
public class dataMajorModel
|
|
{
|
|
public int id { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string probability { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string majorCode { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string mjaorName { get; set; }
|
|
|
|
|
|
public string planCount { get; set; }
|
|
|
|
|
|
public string history { get; set; }
|
|
|
|
public List<string> years { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|