CSharp: QuestPDF create pdf file in donet core 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /// <summary> /// geovindu, Geovin Du,涂聚文 Edit /// </summary> public class DuModel { private string name; private int price, quantity; /// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="price"></param> /// <param name="quantity"></param> public DuModel( string name, int price, int quantity) { this .name = name; this .price = price; this .quantity = quantity; } /// <summary> /// /// </summary> public string Name { get { return name; } set { name = value; } } /// <summary> /// /// </summary> public int Price { get { return price; } set { price = value; } } /// <summary> /// /// </summary> public int Quantity { get { return quantity; } set { quantity = value; } } /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $ "{nameof(Name)}: {Name}, {nameof(Price)}: {Price}, {nameof(Quantity)}: {Quantity}" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | var titleStyle = TextStyle.Default.FontSize(36).SemiBold().FontColor(Colors.Blue.Medium).FontFamily( "simhei" ); var duPdf = Document .Create(container => { container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.PageColor(Colors.White); page.DefaultTextStyle(x => x.FontSize(20).SemiBold().FontColor(Colors.Blue.Medium).FontFamily( "simhei" )); page.Header() .Text( "geoVI studio捷为工作室" ) .FontFamily( "simhei" ) //中文,需定义中文字体否则出问题 .SemiBold().FontSize(36).FontColor(Colors.Blue.Darken2); page.Content() .PaddingVertical(1, Unit.Centimetre) .Column(x => { x.Spacing(20); x.Item().Table(t => { t.ColumnsDefinition(c => { c.RelativeColumn(); c.RelativeColumn(3); }); t.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text( "Visual Studio" ).FontFamily( "simhei" ); t.Cell().Border(1).Padding(5).Text( "Start in debug mode with 'Hot Reload on Save' enabled." ).FontFamily( "simhei" ); t.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text( "Command line" ).FontFamily( "simhei" ); t.Cell().Border(1).Padding(5).Text( "Run 'dotnet watch'." ).FontFamily( "simhei" ); }); x.Item().Text( "Modify this line and the preview should show your changes instantly." ).FontFamily( "simhei" ); //表格 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<DuModel>(); list.Add( new DuModel( "小锅" ,20,1)); list.Add( new DuModel( "小刀" , 2, 12)); list.Add( new DuModel( "小碗" , 5, 13)); list.Add( new DuModel( "小筷" , 20, 4)); list.Add( new DuModel( "小砧" , 1, 100)); //数据组合 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( "第 " ); x.CurrentPageNumber(); x.Span( " 页" ); x.Span( "/ 共 " ); x.TotalPages(); x.Span( "页" ); }); }); }); // duPdf.ShowInPreviewer(); string pathfile = @"geovindu" + DateTime.Now.ToString( "yyyyMMddHHmmss" ) + ".pdf" ; duPdf.GeneratePdf(pathfile); |
输出:
from:
https://github.com/QuestPDF/
https://github.com/QuestPDF/QuestPDF-ExampleInvoice
https://github.com/QuestPDF/QuestPDF-Documentation
CoreWCF
https://github.com/CoreWCF/CoreWCF
ImageSharp
https://github.com/SixLabors/ImageSharp
Mapper
https://github.com/MapsterMapper/Mapster
Pinyin4Net
https://github.com/bao-qian/Pinyin4Net
https://github.com/YangKuang/pinyin4net
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | var titleStyle = TextStyle.Default.FontSize(36).SemiBold().FontColor(Colors.Blue.Medium).FontFamily( "simhei" ); var duPdf = Document .Create(container => { //第一页 container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.PageColor(Colors.White); page.DefaultTextStyle(x => x.FontSize(20).SemiBold().FontColor(Colors.Blue.Medium).FontFamily( "simhei" )); page.Header() .Text( "geoVI studio捷为工作室" ) .FontFamily( "simhei" ) //中文,需定义中文字体否则出问题 .SemiBold().FontSize(36).FontColor(Colors.Blue.Darken2); page.Content() .PaddingVertical(1, Unit.Centimetre) .Column(x => { x.Spacing(20); x.Item().Table(t => { t.ColumnsDefinition(c => { c.RelativeColumn(); c.RelativeColumn(3); }); t.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text( "Visual Studio" ).FontFamily( "simhei" ); t.Cell().Border(1).Padding(5).Text( "Start in debug mode with 'Hot Reload on Save' enabled." ).FontFamily( "simhei" ); t.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text( "Command line" ).FontFamily( "simhei" ); t.Cell().Border(1).Padding(5).Text( "Run 'dotnet watch'." ).FontFamily( "simhei" ); }); x.Item().Text( "Modify this line and the preview should show your changes instantly." ).FontFamily( "simhei" ); //表格 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<DuModel>(); list.Add( new DuModel( "小锅" , 20, 1)); list.Add( new DuModel( "小刀" , 2, 12)); list.Add( new DuModel( "小碗" , 5, 13)); list.Add( new DuModel( "小筷" , 20, 4)); list.Add( new DuModel( "小砧" , 1, 100)); //数据组合 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( "第 " ); x.CurrentPageNumber(); x.Span( " 页" ); x.Span( "/共 " ); x.TotalPages(); x.Span( " 页" ); }); }); //第二页 container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.PageColor(Colors.White); page.DefaultTextStyle(x => x.FontSize(20).SemiBold().FontColor(Colors.Blue.Medium).FontFamily( "simhei" )); page.Header() .Text( "geoVI studio捷为工作室" ) .FontFamily( "simhei" ) //中文,需定义中文字体否则出问题 .SemiBold().FontSize(36).FontColor(Colors.Blue.Darken2); page.Content() .PaddingVertical(1, Unit.Centimetre) .Column(x => { x.Spacing(20); x.Item().Table(t => { t.ColumnsDefinition(c => { c.RelativeColumn(); c.RelativeColumn(3); }); t.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text( "Visual Studio" ).FontFamily( "simhei" ); t.Cell().Border(1).Padding(5).Text( "Start in debug mode with 'Hot Reload on Save' enabled." ).FontFamily( "simhei" ); t.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text( "Command line" ).FontFamily( "simhei" ); t.Cell().Border(1).Padding(5).Text( "Run 'dotnet watch'." ).FontFamily( "simhei" ); }); x.Item().Text( "Modify this line and the preview should show your changes instantly." ).FontFamily( "simhei" ); //表格 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<DuModel>(); list.Add( new DuModel( "小锅" , 20, 1)); list.Add( new DuModel( "小刀" , 2, 12)); list.Add( new DuModel( "小碗" , 5, 13)); list.Add( new DuModel( "小筷" , 20, 4)); list.Add( new DuModel( "小砧" , 1, 100)); //数据组合 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( "第 " ); x.CurrentPageNumber(); x.Span( " 页" ); x.Span( "/共 " ); x.TotalPages(); x.Span( " 页" ); }); }); }); string pathfile = @"geovindu" + DateTime.Now.ToString( "yyyyMMddHHmmss" ) + ".pdf" ; duPdf.GeneratePdf(pathfile); //web // duPdf.ShowInPreviewer(); //打开 //var process = new Process //{ // StartInfo = new ProcessStartInfo(pathfile) // { // UseShellExecute = true // } //}; //process.Start(); //duPdf.ShowInPreviewerAsync(2500); //web // byte[] bytepdf = duPdf.GeneratePdf(); |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
pdf
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!