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

一、QuestPDF 简介

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

二、环境搭建

2.1 安装

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

powershell 复制代码
dotnet add package QuestPDF

2.2 许可证配置

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

csharp 复制代码
// 社区版许可证(只需配置一次,通常放在 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 引入命名空间

csharp 复制代码
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;

三、核心概念与文档结构

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

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

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

四、基础使用

生成第一个PDF文档

添加一个文本

csharp 复制代码
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.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纵向排两行

csharp 复制代码
    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");
相关推荐
AI英德西牛仔20 小时前
豆包导出 pdf 颜色不一样怎么办,选用 AI 导出鸭优化文档导出,结合行业白皮书数据解析色彩失真成因
人工智能·ai·chatgpt·pdf·deepseek·ai导出鸭
zlinear数据采集卡1 天前
数据采集卡从入门到精通(9):分辨率与精度——16位卡不等于1/65536的精度
开发语言·数据库·fpga开发·开源·c#
薰珞婷紫小亭子1 天前
如何解决IEEE期刊LaTeX 模版编译pdf出现大空白的问题
经验分享·pdf
深澈Vincel1 天前
我用 C# + WebView2 做了一个针对“流氓软件”的“一键清理工具“:架构实践与踩坑记录
c#
Hello-FPGA1 天前
AI 如何自动通过 PDF 原理图生成管脚映射
人工智能·fpga开发·pdf
zlinear数据采集卡1 天前
数据采集卡从入门到精通(7):流水线型ADC——级级接力,高速与高精的平衡术
开发语言·arm开发·嵌入式硬件·fpga开发·c#
阿部多瑞 ABU1 天前
告别手工核图:基于 .NET + MuPDFCore 的 CAD 等轴测 PDF 材料表提取与新旧版本对比实战
后端·算法·ui·pdf·c#
条形码D1 天前
DRG综合管理分析系统-目前已经支持CHS-DRG3.0
c#·drg·3.0分组器·drg分组器·chs-drg3.0分组器
2601_963906782 天前
可自由切割与合并的 PDF 尺寸调整与打印工具
pdf
Alex Gram2 天前
数据库同步工具PanguSync图文教程
java·mysql·postgresql·sqlserver·c#·数据库同步软件·数据库同步工具