当前位置: 首页 > news >正文

C#中PDF操作-QuestPDF介绍和使用教程

一、QuestPDF 简介

QuestPDF 是一个专为 .NET 平台打造的现代化 PDF 生成库,它采用声明式、代码驱动的设计理念,让你直接用 C# 代码描述文档结构,无需依赖 HTML 转换或外部渲染引擎-7。其强大的 Fluent API 支持链式调用,内置布局引擎能够自动处理分页与换行,非常适合用于生成发票、报表、合同、数据导出等场景-7。

二、环境搭建

2.1 安装

QuestPDF 通过 NuGet 安装,在 .NET 6+ 项目中直接使用即可

dotnet add package QuestPDF

2.2 许可证配置

根据项目类型,需要在应用启动时设置许可证。对于个人、非营利组织以及年营收低于 100 万美元的企业,可以免费使用社区版

// 社区版许可证(只需配置一次,通常放在 Program.cs 中)QuestPDF.Settings.License=LicenseType.Community;

如果不配置许可证就会报错

System.Exception:“ Thank you for choosing QuestPDF 👋 Before you continue, please take a moment to configure your license. This step helps ensure correct license compliance. QuestPDF requires a Commercial License for production use by organizations with more than $1M USD in annual gross revenue. Individuals, non-profits, open-source projects, and smaller companies qualify for the free Community license. If you are not the decision-maker for software purchases, please share the licensing and pricing details with your team lead or manager: https://www.questpdf.com/license Available license options: - Community: free, - Evaluation: intended solely for evaluation before choosing an appropriate license; not suitable for production use, - Professional: paid, for teams up to 10 developers with dedicated support, - Enterprise: paid, for unlimited developers with prioritized dedicated support. Set the license once at application startup. By doing so, you confirm that the selected tier matches your usage: > QuestPDF.Settings.License = LicenseType.Evaluation; // or Community / Professional / Enterprise No license key or activation is required — we trust you to select the correct option. By choosing the right license, you help ensure QuestPDF remains sustainable and continuously improving for everyone. We wish you a great experience! 🚀 ”

2.3 引入命名空间

usingQuestPDF.Fluent;usingQuestPDF.Helpers;usingQuestPDF.Infrastructure;

三、核心概念与文档结构

QuestPDF 的文档结构遵循文档 → 页面 → 容器 → 元素的层次模型-7:

  • Document:文档的入口,通过Document.Create()定义整体结构。
  • Page:代表单个页面,可设置尺寸、边距、页眉、页脚、背景等属性。
  • Container:布局容器,负责组织子元素(如 Column、Row 等)。
  • Element:具体内容元素,如文本(Text)、图片(Image)、表格(Table)等。

Document 是一个不可变的构建器对象——调用Create()后返回的只是一个声明式的描述,并不会立即生成文件;必须显式调用.GeneratePdf()(同步)或.GeneratePdfAsync()(异步),传入文件路径或 Stream 才能真正写出 PDF

四、基础使用

生成第一个PDF文档

添加一个文本

usingQuestPDF.Fluent;usingQuestPDF.Helpers;usingQuestPDF.Infrastructure;// 设置许可证QuestPDF.Settings.License=LicenseType.Community;Document.Create(container=>{container.Page(page=>{page.Size(PageSizes.A4);page.Margin(20);page.Content().Text("Hello World PDF");});}).GeneratePdf("test.pdf");

使用Column纵向排两行

Document.Create(container=>{container.Page(page=>{page.Size(PageSizes.A4);page.Margin(20);//page.Content().Text("Hello World PDF");page.Content().Column(col=>{col.Item().Text("QuestPDF!").FontSize(24).Bold();col.Item().Text("这是我的第一个 PDF 文档。").FontSize(14);});});}).GeneratePdf("test.pdf");

http://www.rkmt.cn/news/1409031.html

相关文章:

  • Windows桌面壁纸开发避坑指南:从DWM API到跨进程注入,这些‘坑’我帮你踩过了
  • 2026年 东莞遮光膜厂家推荐排行榜:mini遮光膜/PET遮光膜/点阵遮光膜/黑色遮光膜/LED遮光膜/防漏光遮光膜优质品牌深度解析 - 品牌企业推荐师(官方)
  • 企业级集成怎么选:n8n、Zapier还是RestCloud iPaaS?
  • Arm编译器v5到v6预定义宏迁移实战指南
  • 一站式搞定Invar 36现货:多规格棒材带材的优质供应网络汇总 - 品牌2025
  • 单细胞数据分析前传:我在华为云上为RStudio Server配置Shiny Server的踩坑实录
  • 告别穿戴束缚!黎阳之光无感定位赋能矿山矿洞精细化管控
  • 初创团队如何利用Taotoken Token Plan套餐优化AI开发成本
  • CSE-CIC-IDS2018数据集实战:如何用Python预处理CSV文件并快速开始你的入侵检测模型训练
  • [仅仅两步]的电信IPTV单线复用
  • 为什么你的ChatGPT头脑风暴总在平庸层打转?揭秘认知科学证实的4类思维阻断信号及实时矫正协议
  • XML Notepad:让复杂XML编辑变得像整理文件夹一样简单
  • 《企业级商城系统选型评价指标体系研究:交付能力、合规资质与服务韧性》
  • 互联网大厂Java面试实录:谢飞机的电商微服务面试之旅(含Spring Boot、MyBatis、Redis、Spring Cloud、Kafka等技术点)
  • 2026年当前本地花洒哪家强?长治科勒卫浴旗舰店深度测评与专业解析 - 2026年企业资讯
  • 2026年 宝钢镀锌HC420/780DHD+Z吉帕钢厂家推荐榜单:超高强度/轻量化/汽车用先进高强钢品牌深度解析 - 品牌企业推荐师(官方)
  • Teigha中evaluatePoint的作用解析
  • Clayton vs Gumbel vs Frank:三大Copula函数族怎么选?看完这篇实战对比就懂了
  • 时间调制阵列技术解析:硬件简化、并发多波束与ISAC应用
  • 知网AIGC检测算法升级AI率飙升?2026年4款降AI软件深度推荐
  • 别再为加密狗发愁!PolyWorks MS 2020 加密狗版保姆级安装激活全流程(附Win10/11系统避坑点)
  • 从账单明细看Taotoken按Token计费模式的透明性与可追溯性
  • 从零搭建AI合同审查工作流:ChatGPT条款提取→法律要点映射→风险等级自动标注(Python+LangChain实战代码包)
  • Spring Bean 作用域与生命周期
  • 从提示词工程、上下文工程到 Harness 工程:AI Agent 工程化演进路径
  • 智能驾驶的“眼睛”:一文读懂交通标志识别的技术与未来
  • 别再死记硬背了!用Python+SymPy实战拉格朗日乘子法,5分钟搞定SVM里的优化问题
  • 别再只盯着%util了!用iostat -xh 1 3 看懂Linux磁盘性能的5个关键指标
  • B2B产品陈旧感:识别、影响与系统性对抗策略
  • Keil C51评估版兼容性问题解析与NXP 87C752开发指南