需求,将不同文件夹下所有的PDF合并。
下载itextsharp.dll 并引入
递归生成pdf文件List
private List<string> FindFile(string Path)
{
List<string> list = new List<string>();
director(Path, list);
return list;
}
public void director(string Path,List<string> flist)
{
DirectoryInfo di = new DirectoryInfo(Path);
FileSystemInfo[] fis = di.GetFileSystemInfos();//
foreach (FileSystemInfo temp in fis)
{
if (temp is DirectoryInfo)
{
//递归调用
director(temp.FullName,flist);
}
else
{
if (temp.Name.ToString().Contains(".pdf"))
{
flist.Add(Path + "\\" + temp.Name);
}
}
}
}
合并PDF 方法1:读取并写入(缺点无签名)
public static void MergePDFFile(List<string> fileList, string outMergeFile)
{
PdfReader reader;
iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle((float)609.45, (float)430.87);
Document document = new Document(rec);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
foreach (var itemFile in fileList)
{
if (!File.Exists(itemFile))
{
string fileName = Path.GetFileName(itemFile);
MessageBox.Show(string.Format("文件打印合并__{0} 文件不存在", fileName));
continue;
}
FileInfo fInfo = new FileInfo(itemFile);
if (fInfo.Length < 1)
{
string fileName = Path.GetFileName(itemFile);
MessageBox.Show(string.Format("文件打印合并__文件内容为空,无法打印,{0}", fileName));
return;
}
var ext = Path.GetExtension(itemFile).ToLower();
if (".pdf".Equals(ext))
{
reader = new PdfReader(itemFile);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
string fpath = AppDomain.CurrentDomain.BaseDirectory + "\\BQ.JPG";
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(fpath);
img.SetAbsolutePosition((float)409, (float)500);
writer.DirectContent.AddImage(img);
//float w = reader.GetPageSize(1).Width;
//float h = reader.GetPageSize(1).Height;
// float Scale = w > h ? h : w;
cb.AddTemplate(newPage, 0, 0);
//cb.AddTemplate(newPage, Scale, 0, 0, Scale, 0, 0);
}
}
}
document.Close();
MessageBox.Show("合并成功!");
}
合并PDF 方法2:读取并COPY(包含签名)
public static void MergePDFFile(List<string> fileList, string outMergeFile)
{
PdfReader reader;
iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle((float)609.45, (float)430.87);
Document document = new Document(rec);
FileStream baos = new FileStream(outMergeFile, FileMode.Create, FileAccess.Write);
PdfSmartCopy copy = new PdfSmartCopy(document, baos);
copy.ViewerPreferences = PdfWriter.HideToolbar | PdfWriter.HideMenubar;
document.Open();
foreach (var itemFile in fileList)
{
if (!File.Exists(itemFile))
{
string fileName = Path.GetFileName(itemFile);
MessageBox.Show(string.Format("文件打印合并__{0} 文件不存在", fileName));
continue;
}
FileInfo fInfo = new FileInfo(itemFile);
if (fInfo.Length < 1)
{
string fileName = Path.GetFileName(itemFile);
MessageBox.Show(string.Format("文件打印合并__文件内容为空,无法打印,{0}", fileName));
return;
}
var ext = Path.GetExtension(itemFile).ToLower();
if (".pdf".Equals(ext))
{
reader = new PdfReader(itemFile);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
PdfImportedPage page = copy.GetImportedPage(reader, j);
copy.AddPage(page);
}
}
}
document.Close();
MessageBox.Show("合并成功!");
}
}