首页 > 其他分享 >CSharp: QuestPDF create pdf file in donet core 6

CSharp: QuestPDF create pdf file in donet core 6

时间:2022-10-04 13:55:41浏览次数:54  
标签:donet simhei Text create list Cell FontFamily core page

 

 /// <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}";
        }

    }

  

    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

 

标签:donet,simhei,Text,create,list,Cell,FontFamily,core,page
From: https://www.cnblogs.com/geovindu/p/16753660.html

相关文章