获取 itextsharp 包
files 图片文件路径列表
pdfFileName 输出的 pdf文件
public bool MergeFileToPDF(List<string> files,string pdfFileName)
{
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
try
{
iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(pdfFileName, FileMode.Create, FileAccess.ReadWrite));
document.Open();
iTextSharp.text.Image image;
foreach (var item in files)
{
if (String.IsNullOrEmpty(item)) break;
image = iTextSharp.text.Image.GetInstance(item);
if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.NewPage();
document.Add(image);
}
document.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine("转换失败,原因:" + ex.Message);
}
return false;
}
合并两个PDF文件类
public class PdfHelper
{
/// <summary>
/// /// 读取合并的pdf文件名称
/// /// </summary>
/// /// <param name="Directorypath">目录</param>
/// /// <param name="outpath">导出的路径</param>
public static void MergePDF(string Directorypath, string outpath)
{
List<string> filelist2 = new List<string>();
System.IO.DirectoryInfo di2 = new System.IO.DirectoryInfo(Directorypath);
FileInfo[] ff2 = di2.GetFiles("*.pdf");
BubbleSort(ff2);
foreach (FileInfo temp in ff2)
{ filelist2.Add(Directorypath + "\\" + temp.Name); }
mergePDFFiles(filelist2, outpath);
//DeleteAllPdf(Directorypath);
}
/// <summary>
/// /// 冒泡排序
/// /// </summary>
/// /// <param name="arr">文件名数组</param>
public static void BubbleSort(FileInfo[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
for (int j = i; j < arr.Length; j++)
{
if (arr[i].LastWriteTime > arr[j].LastWriteTime)//按创建时间(升序)
{
FileInfo temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/// <summary>
/// /// 合成pdf文件
/// /// </summary>
/// /// <param name="fileList">文件名list</param>
/// /// <param name="outMergeFile">输出路径</param>
public static void mergePDFFiles(List<string> fileList, string outMergeFile)
{
PdfReader reader;
//Rectangle rec = new Rectangle(1660, 1000);
//Document document = new Document(rec);
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
for (int i = 0; i < fileList.Count; i++)
{
reader = new PdfReader(fileList[i]);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, 0, 0);
}
}
document.Close();
}
/// <summary>
/// /// 删除一个文件里所有的文件
/// /// </summary>
/// /// <param name="Directorypath">文件夹路径</param>
public static void DeleteAllPdf(string Directorypath)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Directorypath);
if (di.Exists)
{
FileInfo[] ff = di.GetFiles("*.pdf");
foreach (FileInfo temp in ff)
{
File.Delete(Directorypath + "\\" + temp.Name);
}
}
}
}