itextsharp简介
itextsharp 是一个开源的 .net 库,主要用于创建和操作 pdf 文档。它是 itext 的 .net 版本,itext 是一个广泛使用的 java 库。itextsharp 继承了 itext 的核心功能并进行了适应 .net 平台的调整。
itextsharp 的主要功能包括:
创建 pdf 文档:可以生成带有文本、图像、表格、矢量图形等内容的 pdf 文档。
修改 pdf 文档:支持从现有 pdf 文件中提取文本、添加或删除页面、修改内容等。
数字签名:支持为 pdf 文件添加数字签名,以验证文件的完整性和真实性。
加密与解密:提供对 pdf 文件进行加密保护的功能,支持设置权限(如打印、复制等),并支持解密操作。
表单功能:支持创建 pdf 表单,并允许数据填充、表单提交等操作。
文本抽取:可以从 pdf 文档中提取文本,支持文字提取和 ocr(光学字符识别)功能(需要外部支持)。
支持多种字体和国际化:支持嵌入字体,能够处理不同语言字符集(包括中文、日文等)。
高效性能:能够高效地处理大量 pdf 文件,适用于商业和大型应用程序。
效果图如下:

目标
本文演示如何使用 c# 和 itextsharp 将图片转换为 pdf 的功能。
使用步骤
首先,我们需要在项目中引用以下两个 dll 文件:
bouncycastle.crypto.dll:提供 itextsharp 在处理数字签名、加密、解密等功能时所需的加密支持。
itextsharp.dll:用于操作 pdf 文件。
代码实现
using system;
using system.io;
using system.windows.forms;
using itextsharp.text;
using itextsharp.text.pdf;
namespace pdfzhuan
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
// 选择文件按钮,打开文件对话框并选择图片文件
private void button1_click(object sender, eventargs e)
{
openfiledialog ofd = new openfiledialog
{
initialdirectory = application.startuppath, // 设置打开对话框的初始目录
title = "请选择要打开的文件", // 设置对话框标题
multiselect = true, // 允许多选文件
filter = "图片文件|*.jpg|所有文件|*.*", // 设置文件过滤器
filterindex = 2, // 设置默认过滤器索引
restoredirectory = false // 不记忆最后打开的目录
};
if (ofd.showdialog() == dialogresult.ok)
{
string filepath = ofd.filename; // 获取文件路径
string filename = ofd.safefilename; // 获取文件名(不包括路径)
string pdffile = filepath.replace(".jpg", ".pdf"); // 设置转换后的 pdf 文件路径
textbox1.text = filepath; // 显示图片路径
textbox2.text = pdffile; // 显示 pdf 输出路径
}
}
/// <summary>
/// 将图片转换为 pdf
/// </summary>
/// <param name="jpgfile">图片文件路径</param>
/// <param name="pdf">生成的 pdf 文件路径</param>
/// <returns></returns>
public static bool convertjpg2pdf(string jpgfile, string pdf)
{
try
{
// 创建一个 a4 尺寸的 pdf 文档
var document = new document(pagesize.a4, 25, 25, 25, 25);
using (var stream = new filestream(pdf, filemode.create, fileaccess.write, fileshare.none))
{
// 获取 pdf 写入实例
pdfwriter.getinstance(document, stream);
document.open(); // 打开文档
// 加载图片
using (var imagestream = new filestream(jpgfile, filemode.open, fileaccess.read, fileshare.readwrite))
{
var image = image.getinstance(imagestream);
// 图片如果过大,则缩放以适应页面
if (image.height > pagesize.a4.height - 25 || image.width > pagesize.a4.width - 25)
{
image.scaletofit(pagesize.a4.width - 25, pagesize.a4.height - 25);
}
image.alignment = image.align_middle; // 设置图片居中
document.add(image); // 将图片添加到 pdf 文档中
}
document.close(); // 关闭文档
}
messagebox.show("pdf 转换成功!");
return true;
}
catch (exception ex)
{
messagebox.show($"转换失败: {ex.message}");
return false;
}
}
// 点击转换按钮,执行转换操作
private void button3_click(object sender, eventargs e)
{
if (string.isnullorempty(textbox1.text))
{
messagebox.show("请选择要转换的图片!");
return;
}
string jpgfile = textbox1.text;
string pdffile = textbox2.text;
// 执行图片转换为 pdf
convertjpg2pdf(jpgfile, pdffile);
}
// 点击选择文件夹按钮,设置输出 pdf 文件的路径
private void button2_click(object sender, eventargs e)
{
if (string.isnullorempty(textbox1.text))
{
messagebox.show("请选择要转换的图片!");
return;
}
folderbrowserdialog folderdialog = new folderbrowserdialog
{
description = "请选择图片所在文件夹"
};
if (folderdialog.showdialog() == dialogresult.ok)
{
string filename = datetime.now.tostring("yyyymmddhhmm"); // 生成文件名
textbox2.text = path.combine(folderdialog.selectedpath, $"{filename}.pdf"); // 设置输出路径
}
}
}
}代码解析
选择图片文件:通过 openfiledialog 控件打开文件对话框,用户可以选择 .jpg 格式的图片。选择后,图片路径和对应的 pdf 输出路径会显示在文本框中。
图片转 pdf:通过 itextsharp 库,创建一个 a4 尺寸的 pdf 文档。通过 itextsharp.text.image.getinstance() 方法加载图片,如果图片尺寸超过 a4 页面,则自动缩放以适应页面大小。最后,图片被添加到 pdf 中并保存。
设置输出 pdf 文件路径:用户可以通过 folderbrowserdialog 选择输出文件夹,并设置 pdf 文件名。
总结
通过简单的用户界面和文件操作,用户可以方便地将 jpg 图片转化为 pdf 格式。这一功能对于批量生成文档、报告或其他图像处理场景非常有用。
到此这篇关于c#使用itextsharp库将图片转换为pdf的文章就介绍到这了,更多相关c# itextsharp图片转pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论