bug fixed
parent
fdf8520961
commit
b72a76af2d
|
|
@ -16,6 +16,7 @@ using StackExchange.Profiling.Internal;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using MiniExcelLibs;
|
using MiniExcelLibs;
|
||||||
|
using PDFGenerateApi;
|
||||||
namespace New_College.Api.Controllers.Front
|
namespace New_College.Api.Controllers.Front
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -213,26 +214,26 @@ namespace New_College.Api.Controllers.Front
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
//[HttpGet("generate")]
|
[HttpGet("generate")]
|
||||||
//public IActionResult Generate([FromQuery] MinExcelAttrDto dto)
|
public IActionResult Generate([FromQuery] MinExcelAttrDto dto)
|
||||||
//{
|
{
|
||||||
//string[] unversityheaders = { "志愿", "概率", "院校代码", "院校名称", "24招生计划", "历年", "2023", "2022", "2021" };
|
string[] unversityheaders = { "志愿", "概率", "院校代码", "院校名称", "24招生计划", "历年", "2023", "2022", "2021" };
|
||||||
//var stream = PdfOptHelper.CreatePdf("test", unversityheaders.ToList(), new List<PdfOptHelper.dataModel>() {
|
var stream = PdfOptHelper.CreatePdf("test", unversityheaders.ToList(), new List<PdfOptHelper.dataModel>() {
|
||||||
// new PdfOptHelper.dataModel()
|
new PdfOptHelper.dataModel()
|
||||||
// { id=1,
|
{ id=1,
|
||||||
// history="录取人数\n计划人数\n最低分\n最低位次\n效位差",
|
history="录取人数\n计划人数\n最低分\n最低位次\n效位差",
|
||||||
// planName="A222",
|
planName="A222",
|
||||||
// probability="90%",
|
probability="90%",
|
||||||
// universityCode="A99",
|
universityCode="A99",
|
||||||
// universityName="中科大大家四大石窟记得哈就开始大肆扩大数据库",
|
universityName="中科大大家四大石窟记得哈就开始大肆扩大数据库",
|
||||||
// years=new List<string>(){"2023", "2022", "2021" }
|
years=new List<string>(){"2023", "2022", "2021" }
|
||||||
// }
|
}
|
||||||
//});
|
});
|
||||||
|
|
||||||
//// 返回 PDF 文件
|
// 返回 PDF 文件
|
||||||
//return File(stream.ToArray(), "application/pdf", "table_example_with_logo_and_title.pdf");
|
return File(stream.ToArray(), "application/pdf", "table_example_with_logo_and_title.pdf");
|
||||||
|
|
||||||
//}
|
}
|
||||||
[HttpGet("Export")]
|
[HttpGet("Export")]
|
||||||
public async Task<IActionResult> Export([FromQuery] MinExcelAttrDto dto)
|
public async Task<IActionResult> Export([FromQuery] MinExcelAttrDto dto)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\New_College.Extensions\New_College.Extensions.csproj" />
|
<ProjectReference Include="..\New_College.Extensions\New_College.Extensions.csproj" />
|
||||||
|
<ProjectReference Include="..\PdfOpt\PdfOpt.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "New_College.AdminMvc", "New
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "New_Spider", "New_Spider\New_Spider.csproj", "{E23857BF-DFBB-49DD-A86A-1B2932F6D33B}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "New_Spider", "New_Spider\New_Spider.csproj", "{E23857BF-DFBB-49DD-A86A-1B2932F6D33B}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PDFGenerateApi", "PDFGenerateApi\PDFGenerateApi.csproj", "{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PDFGenerateApi", "PDFGenerateApi\PDFGenerateApi.csproj", "{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfOpt", "PdfOpt\PdfOpt.csproj", "{AD66F033-5A1F-4FE3-AB99-8E06AB987554}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|
@ -93,6 +95,10 @@ Global
|
||||||
{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{AD66F033-5A1F-4FE3-AB99-8E06AB987554}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{AD66F033-5A1F-4FE3-AB99-8E06AB987554}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{AD66F033-5A1F-4FE3-AB99-8E06AB987554}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{AD66F033-5A1F-4FE3-AB99-8E06AB987554}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="itext7" Version="8.0.4" />
|
||||||
|
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.4" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
|
@ -0,0 +1,244 @@
|
||||||
|
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="dataModels"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static MemoryStream CreatePdf(string pagetitle, List<string> headers, List<dataModel> dataModels)
|
||||||
|
{
|
||||||
|
// 创建内存流
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
// 创建 PDF Writer
|
||||||
|
PdfWriter writer = new PdfWriter(stream);
|
||||||
|
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", "NotoSansCJKsc-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}", DateTime.Now.ToString("yyyy-MM-dd HH:mm")));
|
||||||
|
document.Add(subtitle);
|
||||||
|
// 添加表头
|
||||||
|
// string[] unversityheaders = { "志愿", "概率", "院校代码", "院校名称", "24招生计划", "历年", "2023", "2022", "2021" };
|
||||||
|
Table universitytable = HeaderTable(font, 9, headers, new List<dataModel>() { });
|
||||||
|
document.Add(universitytable);
|
||||||
|
#region ***********写入表格
|
||||||
|
// 创建一个 8列的表格,设置表格宽度为 100% 以适应页面宽度
|
||||||
|
// Table universitytable = new Table(9);
|
||||||
|
// universitytable.SetWidth(UnitValue.CreatePercentValue(100));
|
||||||
|
|
||||||
|
|
||||||
|
//int h = 0;
|
||||||
|
//foreach (var header in unversityheaders)
|
||||||
|
//{
|
||||||
|
// h++;
|
||||||
|
// if (h == 4)
|
||||||
|
// {
|
||||||
|
// universitytable.AddHeaderCell(new Cell().Add(new Paragraph(header).SetWidth(85).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// universitytable.AddHeaderCell(new Cell().Add(new Paragraph(header).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//// 可选:添加表格数据,这里仅作为示例
|
||||||
|
//for (int i = 1; i <= 1; i++)
|
||||||
|
//{
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph(i.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("0.9").SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("A999").SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("山东师范大学(本科)(山东公办)省重点 保研 研究生院 省部共建山东 济南市/综合/公办/排名190" + i).SetWidth(85).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("100").SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("历史数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("2023数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("2022数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// universitytable.AddCell(new Cell().Add(new Paragraph("2021数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
|
||||||
|
//}
|
||||||
|
//document.Add(universitytable);
|
||||||
|
//// float[] columnWidths = { 0.5f, 0.5f, 1, 2, 1, 1, 1, 1, 1 };
|
||||||
|
//Table majortable = new Table(9);
|
||||||
|
//majortable.SetWidth(UnitValue.CreatePercentValue(100));
|
||||||
|
//// 添加表头
|
||||||
|
//string[] majorlistheaders = { "序号", "概率", "专业代码", "专业名称", "24招生计划", "历年", "2023", "2022", "2021" };
|
||||||
|
//foreach (var header in majorlistheaders)
|
||||||
|
//{
|
||||||
|
// majortable.AddHeaderCell(new Cell().Add(new Paragraph(header).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//for (int i = 1; i <= 5; i++)
|
||||||
|
//{
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph(i.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("0.9").SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("QL").SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("垫子信息技术" + i).SetWidth(85).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("20").SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("录取人数\n计划人数\n最低分\n最低位次\n效位差" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("2023数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("2022数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
// majortable.AddCell(new Cell().Add(new Paragraph("2021数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
|
||||||
|
//}
|
||||||
|
//document.Add(majortable);
|
||||||
|
#endregion
|
||||||
|
// 将表格添加到文档中
|
||||||
|
// 关闭文档
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Table table = new Table(tb);
|
||||||
|
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(85).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
table.AddHeaderCell(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.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.planName).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 planName { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public string history { get; set; }
|
||||||
|
|
||||||
|
public List<string> years { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue