以下是demo 代码 环境是在 api中
public class TestModel
{
public string Name { get; set; } = "测试";
public decimal Price { get; set; } = 1;
public int Quantity { get; set; } = 2;
}
[Route("api/[controller]")]
[ApiController]
public class IndexController : ControllerBase
{
[HttpPost("TestPDF")]
public IActionResult TestPDF()
{
string path_file = @"F:\Users\Administrator\Desktop\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
//测试样式
var titleStyle = TextStyle.Default.FontSize(36).SemiBold().FontColor(Colors.Blue.Medium);
//整合对象
var pdf_obj = Document.Create(container =>
{
container.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.Background(Colors.White);
//字体默认大小20号字体
page.DefaultTextStyle(x => x.FontSize(20));
//页眉部分
page.Header()
.Background(Colors.Blue.Accent1)
.Row(row =>
{
row.RelativeItem().Column(column =>
{
column.Item().Text("测试标题1").FontFamily("simhei").Style(titleStyle);
column.Item().Text("测试标题2").FontFamily("simhei");
});
});
//.SemiBold().FontSize(36).FontColor(Colors.Blue.Medium);//此处为整体设置
//内容部分
page.Content()
.PaddingVertical(1, Unit.Centimetre)
.Column(x =>
{
x.Spacing(20);
x.Item().Text(Placeholders.LoremIpsum());
x.Item().Image(Placeholders.Image(200, 100));
x.Item().Image(@"F:\Users\Administrator\Desktop\QQ图片20220223154742.jpg");
//表格
x.Item().Table(table =>
{
//设置表头的列参数占比
table.ColumnsDefinition(columns =>
{
columns.ConstantColumn(30);
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
});
// 表头
table.Header(header =>
{
header.Cell().Text("#").FontFamily("simhei");
header.Cell().Text("商品").FontFamily("simhei");
header.Cell().AlignRight().Text("价格").FontFamily("simhei");
header.Cell().AlignRight().Text("数量").FontFamily("simhei");
header.Cell().AlignRight().Text("总价").FontFamily("simhei");
header.Cell().ColumnSpan(5)
.PaddingVertical(5).BorderBottom(1).BorderColor(Colors.Black);
});
var list = new List<TestModel>();
list.Add(new TestModel());
list.Add(new TestModel());
list.Add(new TestModel());
list.Add(new TestModel());
list.Add(new TestModel());
//数据组合
for (int i = 0; i < list.Count; i++)
{
table.Cell().Element(CellStyle).Text(i + 1).FontFamily("simhei");
table.Cell().Element(CellStyle).Text(list[i].Name).FontFamily("simhei");
table.Cell().Element(CellStyle).AlignRight().Text($"{list[i].Price}$").FontFamily("simhei").Style(titleStyle);
table.Cell().Element(CellStyle).AlignRight().Text(list[i].Quantity).FontFamily("simhei");
table.Cell().Element(CellStyle).AlignRight().Text($"{list[i].Price * list[i].Quantity}$").FontFamily("simhei");
static IContainer CellStyle(IContainer container)
{
return container.BorderBottom(1).BorderColor(Colors.Grey.Lighten2).PaddingVertical(5);
}
}
});
});
//页脚部分
page.Footer()
.AlignCenter()
.Text(x =>
{
x.Span("Page");
x.CurrentPageNumber();
});
});
});
byte[] byte_pdf = pdf_obj.GeneratePdf();
//.GeneratePdf(path_file);//这个是保存道对应目录地址
//这个是返回对应文件
return File(byte_pdf, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{DateTime.Now:G}.pdf");
}
标签:simhei,C#,Text,list,常规,Cell,FontFamily,QuestPDF,page From: https://www.cnblogs.com/flydmxy/p/16749133.html