我有一大堆图片,老板让把它们转成PDF文件,写了一小个程序,分享给大家:

首先添加 itextsharp.dll

这个DLL可以去网上下载,也可以私信我发给你,有些版本是不能用的。

具体使用代码如下:

using System; using System.IO; using System.Windows.Forms; namespace Pic2PDF { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string path = Application.StartupPath; string picfilename = path + "\c#.jpg"; string pdffilename = path + "\c#.pdf"; //执行操作 string r = Pic2PDF(picfilename, pdffilename); MessageBox.Show(r); } public static string Pic2PDF(String PicFilePath, string PdfPath) { if (File.Exists(PdfPath)) { File.Delete(PdfPath); } iTextSharp.text.Document document = new iTextSharp.text.Document(); try { iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(PdfPath, FileMode.CreateNew)); document.Open(); iTextSharp.text.pdf.BaseFont bfChinese = iTextSharp.text.pdf.BaseFont.CreateFont("C:\WINDOWS\Fonts\simsun.ttc,1", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED); iTextSharp.text.Font Titlefont = new iTextSharp.text.Font(bfChinese, 16, iTextSharp.text.Font.BOLD, new iTextSharp.text.Color(0, 0, 0)); iTextSharp.text.Image je = iTextSharp.text.Image.GetInstance(PicFilePath); document.Add(je); } catch (Exception x) { return x.ToString(); } document.Close(); return "OK"; } } }

做一个循环,处理整个图片文件夹下的图片即可

看下效果: