using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using iTextSharp.text; using iTextSharp.text.pdf; namespace MEAS.Common { public class PdfHelper { //定义一个用于保存静态变量的实例 private static PdfHelper instance = null; //定义一个保证线程同步的标识 private static readonly object locker = new object(); //构造函数为私有,使外界不能创建该类的实例 private PdfHelper() { } public static PdfHelper Instance { get { if (instance == null) { lock (locker) { if (instance == null) instance = new PdfHelper(); } } return instance; } } /// <summary> /// 合并PDF文件 /// </summary> /// <param name="fileList">需要合并的PDF集合</param> /// <param name="outMergeFile">合并后文件路径</param> public string MergePDFFiles(string[] fileList, string mergeFile) { try { //PdfDocument Document document = new Document(); PdfCopy pdfCopy = new PdfCopy(document, new FileStream(mergeFile, FileMode.Create)); //PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(mergeFile, FileMode.Create)); document.Open(); //PdfContentByte cb = writer.DirectContent; //PdfImportedPage newPage; for (int i = 0; i < fileList.Length; i++) { PdfReader reader = new PdfReader(fileList[i]); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); //newPage.Width = //reader. PdfImportedPage newPage = pdfCopy.GetImportedPage(reader, j); pdfCopy.AddPage(newPage); //cb.AddTemplate(newPage, 0, 0); } reader.Close(); } document.Close(); } catch (Exception) { throw; } return mergeFile; } /// <summary> /// 合并PDF文件(生成A4大小的页面) /// </summary> /// <param name="fileList">需要合并的PDF集合</param> /// <param name="outMergeFile">合并后文件路径</param> public string MergePDFFilesA4(string[] fileList, string mergeFile) { PdfReader reader; Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(mergeFile, FileMode.Create)); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; for (int i = 0; i < fileList.Length; 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(); return mergeFile; } ///直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary> /// <param name="strPath">文件夹路径</param> /// <returns>执行结果</returns> public void DeleteDir(string file) { try { //去除文件夹和子文件的只读属性 //去除文件夹的只读属性 System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file); fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory; //去除文件的只读属性 System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal); //判断文件夹是否还存在 if (Directory.Exists(file)) { foreach (string f in Directory.GetFileSystemEntries(file)) { if (File.Exists(f)) { //如果有子文件删除文件 File.Delete(f); Console.WriteLine(f); } else DeleteDir(f); //循环递归删除子文件夹 } //删除空文件夹 Directory.Delete(file); } } catch (Exception) // 异常处理 { //throw; return;//异常不删除, //Console.WriteLine(ex.Message.ToString());// 异常信息 } } } }
标签:文件,string,using,PdfHelper,System,new,PDF,document From: https://www.cnblogs.com/YYkun/p/16836624.html