在C#项目中处理PDF文件时,iTextSharp是一款非常实用的第三方库,它提供了丰富的API支持PDF的创建、编辑、内容提取等多种操作,能够满足大部分日常开发中的PDF处理需求。下面将从环境准备开始,逐步讲解具体的使用方法,并附上完整的项目实例代码。

iTextSharp环境配置
首先需要在项目中引入iTextSharp依赖,目前常用的稳定版本是5.5.13.3,可以通过NuGet包管理器直接安装,也可以手动下载对应版本的DLL文件添加到项目引用中。如果使用NuGet安装,在包管理器控制台执行以下命令即可:
Install-Package iTextSharp -Version 5.5.13.3
基础PDF创建与文字添加
创建PDF是最基础的操作,首先需要初始化文档对象,指定保存路径,然后添加内容后关闭文档。以下是创建一个简单PDF并添加段落文字的示例代码:
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextSharpDemo
{
class Program
{
static void Main(string[] args)
{
// 定义PDF保存路径
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "demo.pdf");
// 初始化文档对象,设置页面大小为A4
Document document = new Document(PageSize.A4);
try
{
// 创建PDF写入器,关联文档和文件流
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
// 打开文档
document.Open();
// 添加标题段落
Paragraph title = new Paragraph("iTextSharp操作PDF示例");
title.Alignment = Element.ALIGN_CENTER;
document.Add(title);
// 添加普通文字段落
Paragraph content = new Paragraph("这是使用C#和iTextSharp生成的第一个PDF文件,后续会逐步添加更多内容。");
document.Add(content);
Console.WriteLine("PDF创建成功,路径:" + pdfPath);
}
catch (Exception ex)
{
Console.WriteLine("创建PDF失败:" + ex.Message);
}
finally
{
// 关闭文档,释放资源
document.Close();
}
}
}
}
PDF中插入表格
实际项目中经常需要在PDF中展示表格数据,iTextSharp提供了PdfPTable类来实现表格功能,支持设置列数、单元格内容、边框样式等属性。以下是插入一个3列表格的示例代码:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.IO;
namespace iTextSharpDemo
{
class TableDemo
{
static void Main(string[] args)
{
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "table_demo.pdf");
Document document = new Document(PageSize.A4);
try
{
PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
document.Open();
// 创建3列的表格
PdfPTable table = new PdfPTable(3);
// 设置表格宽度为页面宽度的80%
table.WidthPercentage = 80;
// 添加表头
table.AddCell(new PdfPCell(new Phrase("姓名")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("年龄")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("职业")) { HorizontalAlignment = Element.ALIGN_CENTER });
// 添加表格数据行
table.AddCell(new PdfPCell(new Phrase("张三")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("25")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("程序员")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("李四")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("30")) { HorizontalAlignment = Element.ALIGN_CENTER });
table.AddCell(new PdfPCell(new Phrase("设计师")) { HorizontalAlignment = Element.ALIGN_CENTER });
document.Add(table);
Console.WriteLine("带表格的PDF生成成功");
}
catch (Exception ex)
{
Console.WriteLine("生成失败:" + ex.Message);
}
finally
{
document.Close();
}
}
}
}
PDF中嵌入图片
除了文字和表格,iTextSharp也支持在PDF中插入图片,支持JPG、PNG等常见图片格式,还可以设置图片的大小、位置、旋转角度等属性。以下是插入本地图片的示例代码:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.IO;
namespace iTextSharpDemo
{
class ImageDemo
{
static void Main(string[] args)
{
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image_demo.pdf");
// 本地图片路径,替换为实际存在的图片路径即可
string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.jpg");
Document document = new Document(PageSize.A4);
try
{
PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
document.Open();
// 添加文字说明
document.Add(new Paragraph("以下是嵌入的本地图片:"));
// 加载图片
Image img = Image.GetInstance(imagePath);
// 设置图片宽度为页面宽度的50%
img.ScalePercent(50);
// 设置图片居中对齐
img.Alignment = Element.ALIGN_CENTER;
document.Add(img);
Console.WriteLine("带图片的PDF生成成功");
}
catch (Exception ex)
{
Console.WriteLine("生成失败:" + ex.Message);
}
finally
{
document.Close();
}
}
}
}
读取现有PDF内容
iTextSharp不仅可以生成PDF,还可以读取现有PDF的文本内容,通过PdfReader和ITextExtractionStrategy即可实现。以下是读取PDF文字内容的示例代码:
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.IO;
namespace iTextSharpDemo
{
class ReadPdfDemo
{
static void Main(string[] args)
{
// 待读取的PDF路径,替换为实际路径
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "demo.pdf");
try
{
// 初始化PDF读取器
PdfReader reader = new PdfReader(pdfPath);
string pdfContent = "";
// 遍历所有页面,提取文字内容
for (int i = 1; i <= reader.NumberOfPages; i++)
{
pdfContent += PdfTextExtractor.GetTextFromPage(reader, i);
}
reader.Close();
Console.WriteLine("PDF内容如下:");
Console.WriteLine(pdfContent);
}
catch (Exception ex)
{
Console.WriteLine("读取PDF失败:" + ex.Message);
}
}
}
}
完整项目实例说明
以上示例都是独立的功能模块,实际项目中可以将这些功能封装成工具类,方便统一调用。完整的项目源码包含上述所有功能,同时添加了异常处理和路径配置逻辑,开发者可以直接下载后修改对应的路径和参数,快速适配自己的业务需求。需要注意的是,iTextSharp 5.x版本是AGPL协议,如果是商业项目使用,需要注意协议合规性,也可以选择升级到iText 7的社区版本或者购买商业授权。
iTextSharpC#_PDF操作PDF生成PDF编辑修改时间:2026-06-25 22:06:49