获取 itextsharp 包
files 图片文件路径列表
pdfFileName 输出的 pdf文件
public bool MergeFileToPDFList<string> files,string pdfFileName)
{
iTextSharp.text.Document document = new iTextSharp.text.DocumentiTextSharp.text.PageSize.A4, 25, 25, 25, 25);
try
{
iTextSharp.text.pdf.PdfWriter.GetInstancedocument, new FileStreampdfFileName, FileMode.Create, FileAccess.ReadWrite));
document.Open);
iTextSharp.text.Image image;
foreach var item in files)
{
if String.IsNullOrEmptyitem)) break;
image = iTextSharp.text.Image.GetInstanceitem);
if image.Height > iTextSharp.text.PageSize.A4.Height – 25)
{
image.ScaleToFitiTextSharp.text.PageSize.A4.Width – 25, iTextSharp.text.PageSize.A4.Height – 25);
}
else if image.Width > iTextSharp.text.PageSize.A4.Width – 25)
{
image.ScaleToFitiTextSharp.text.PageSize.A4.Width – 25, iTextSharp.text.PageSize.A4.Height – 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.NewPage);
document.Addimage);
}
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 MergePDFstring Directorypath, string outpath)
{
List<string> filelist2 = new List<string>);
System.IO.DirectoryInfo di2 = new System.IO.DirectoryInfoDirectorypath);
FileInfo[] ff2 = di2.GetFiles”*.pdf”);
BubbleSortff2);
foreach FileInfo temp in ff2)
{ filelist2.AddDirectorypath + “\” + temp.Name); }
mergePDFFilesfilelist2, outpath);
//DeleteAllPdfDirectorypath);
}
/// <summary>
/// /// 冒泡排序
/// /// </summary>
/// /// <param name=”arr”>文件名数组</param>
public static void BubbleSortFileInfo[] 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 mergePDFFilesList<string> fileList, string outMergeFile)
{
PdfReader reader;
//Rectangle rec = new Rectangle1660, 1000);
//Document document = new Documentrec);
iTextSharp.text.Document document = new iTextSharp.text.DocumentiTextSharp.text.PageSize.A4, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.GetInstancedocument, new FileStreamoutMergeFile, FileMode.Create));
document.Open);
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
for int i = 0; i < fileList.Count; i++)
{
reader = new PdfReaderfileList[i]);
int iPageNum = reader.NumberOfPages;
for int j = 1; j <= iPageNum; j++)
{
document.NewPage);
newPage = writer.GetImportedPagereader, j);
cb.AddTemplatenewPage, 0, 0);
}
}
document.Close);
}
/// <summary>
/// /// 删除一个文件里所有的文件
/// /// </summary>
/// /// <param name=”Directorypath”>文件夹路径</param>
public static void DeleteAllPdfstring Directorypath)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfoDirectorypath);
if di.Exists)
{
FileInfo[] ff = di.GetFiles”*.pdf”);
foreach FileInfo temp in ff)
{
File.DeleteDirectorypath + “\” + temp.Name);
}
}
}
}