.net core 使用 textSharp生成pdf

引入Nuget包

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
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
 
using System.IO;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
             
            //生成pdf
            Document document = new Document();
 
            var fileStream = File.Create("aa.pdf");
            PdfWriter pw = PdfWriter.GetInstance(document, fileStream);
 
 
            document.Open();
            //写入数据
            //注册字体
            string fontpath = "a.ttf";
            BaseFont customfont = BaseFont.CreateFont(fontpath, BaseFont.CP1252, BaseFont.EMBEDDED);
 
            var baseFont = new Font(customfont);
            baseFont.Color = new BaseColor(System.Drawing.Color.Red);
            baseFont.Size = 30;
 
 
            #region 头部
            PdfPTable tableLogo = new PdfPTable(3);
            tableLogo.DefaultCell.Border = Rectangle.NO_BORDER;
            tableLogo.DefaultCell.MinimumHeight = 40f;
            float[] headWidths = new float[] { 60f, 60f, 150f };
            tableLogo.SetWidths(headWidths);
            //logo
            var logo = iTextSharp.text.Image.GetInstance(File.ReadAllBytes("aa.png"));
            // logo.ScaleToFit(130 * 0.667f, 43 * 0.667f);
            var logoCell = new PdfPCell();
            logoCell.PaddingLeft = -1f;
            logoCell.MinimumHeight = 45f;
            logoCell.HorizontalAlignment = Element.ALIGN_LEFT;
            logoCell.Border = 0;
            logoCell.AddElement(logo);
            tableLogo.AddCell(logoCell);
 
 
            var sCell = new PdfPCell(new Paragraph("SmartFind", baseFont));
            sCell.Border = 0;
            sCell.PaddingLeft = 3f;
            sCell.PaddingBottom = 17f;
            sCell.MinimumHeight = 45f;
            sCell.HorizontalAlignment = Element.ALIGN_LEFT;
            sCell.VerticalAlignment = Element.ALIGN_BOTTOM;
            tableLogo.AddCell(sCell);
 
            var aCell = new PdfPCell();
            aCell.Border = 0;
            aCell.MinimumHeight = 45f;
            aCell.PaddingBottom = 17f;
            aCell.HorizontalAlignment = Element.ALIGN_LEFT;
            aCell.VerticalAlignment = Element.ALIGN_BOTTOM;
 
            var acc = new Paragraph("|  Accessories", baseFont);
            aCell.AddElement(acc);
            tableLogo.AddCell(aCell);
 
            document.Add(tableLogo);
            #endregion
 
            document.Add(new Paragraph(""));
 
 
 
            //页脚
            PDFFooter footer = new PDFFooter();
            footer.OnEndPage(pw, document);
            document.Close();
            fileStream.Dispose();
        }
        //This implementation is obviously not very good --> Though it should be enough for everyone to implement their own.
       
    }
 
    public class PDFFooter : PdfPageEventHelper
    {
        // write on top of document
        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            base.OnOpenDocument(writer, document);
            PdfPTable tabFot = new PdfPTable(new float[] { 1F });
            tabFot.SpacingAfter = 10F;
            PdfPCell cell;
            tabFot.TotalWidth = 300F;
            cell = new PdfPCell(new Phrase("Header"));
            tabFot.AddCell(cell);
            tabFot.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
        }
 
        // write on start of each page
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);
        }
 
        // write on end of each page
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);
            //PdfPTable tabFot = new PdfPTable(new float[] { 1F });
            //tabFot.TotalWidth = 700f;
            //tabFot.DefaultCell.Border = 0;
            ////  var footFont = FontFactory.GetFont("Lato", 12 * 0.667f, new Color(60, 60, 60));
            //string fontpath = HttpContext.Current.Server.MapPath("~/App_Data");
            //BaseFont customfont = BaseFont.CreateFont(fontpath + "\\Lato-Regular.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
            //var footFont = new Font(customfont, 12 * 0.667f, Font.NORMAL, new Color(170, 170, 170));
 
            //PdfPCell cell;
            //cell = new PdfPCell(new Phrase("@ 2016 . All Rights Reserved", footFont));
            //cell.VerticalAlignment = Element.ALIGN_CENTER;
            //cell.Border = 0;
            //cell.PaddingLeft = 100f;
            //tabFot.AddCell(cell);
            //tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent);
        }
 
        //write on close of document
        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);
        }
    }
}

  

  

posted @   IWing  阅读(1837)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示