110,536
社区成员
发帖
与我相关
我的任务
分享
//获取文件夹1中的所有pdf
FolderBrowserDialog FBDialog = new FolderBrowserDialog();
string folder1Path = "";
if (FBDialog.ShowDialog() == DialogResult.OK)
{
string strPath = FBDialog.SelectedPath;
if (strPath.EndsWith("\\"))
folder1Path = strPath;
else
folder1Path = strPath + "\\";
}
//遍历文件夹1获取具体文件
//files集合存储文件名和对应的具体位置
Dictionary<string, string> Files1 = new Dictionary<string, string>();
DirectoryInfo theFolder = new DirectoryInfo(folder1Path);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
foreach (DirectoryInfo NextFolder in dirInfo)
{
FileInfo[] fileInfo = NextFolder.GetFiles("*.pdf");
foreach (FileInfo NextFile in fileInfo)
{
Files1.Add(NextFile.Name, NextFile.FullName);
}
}
//遍历文件夹2,同样的操作,其实这里可以提取一个方法出来要简便些
FBDialog = new FolderBrowserDialog();
string folder2Path = "";
if (FBDialog.ShowDialog() == DialogResult.OK)
{
string strPath = FBDialog.SelectedPath;
if (strPath.EndsWith("\\"))
folder2Path = strPath;
else
folder2Path = strPath + "\\";
}
Dictionary<string, string> Files2 = new Dictionary<string, string>();
theFolder = new DirectoryInfo(folder2Path);
FileInfo[] fileInfo1 = theFolder.GetFiles("*.pdf");
foreach (FileInfo NextFile in fileInfo1)
{
Files2.Add(NextFile.Name, NextFile.FullName);
}
//遍历Files2,如果Files1中有相同的key,也就是文件name相同,将文件夹2中的pdf的第一页插入到文件夹1中对应pdf的第三页
foreach (string name in Files1.Keys)
{
if (Files2.ContainsKey(name))
{
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile(Files1[name]);
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile(Files2[name]);
//获取文件夹2中pdf的第一页插入到对应pdf的第三页
PdfPageBase page = pdf2.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();
pdf1.Pages.Insert(1, size, new PdfMargins(0, 0));
pdf1.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
pdf1.SaveToFile(Files1[name]);
}
}
[/quote谢谢老师指导,对不起,我点错了一下,//获取文件夹1中的所有pdf
FolderBrowserDialog FBDialog = new FolderBrowserDialog();
string folder1Path = "";
if (FBDialog.ShowDialog() == DialogResult.OK)
{
string strPath = FBDialog.SelectedPath;
if (strPath.EndsWith("\\"))
folder1Path = strPath;
else
folder1Path = strPath + "\\";
}
//遍历文件夹1获取具体文件
//files集合存储文件名和对应的具体位置
Dictionary<string, string> Files1 = new Dictionary<string, string>();
DirectoryInfo theFolder = new DirectoryInfo(folder1Path);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
foreach (DirectoryInfo NextFolder in dirInfo)
{
FileInfo[] fileInfo = NextFolder.GetFiles("*.pdf");
foreach (FileInfo NextFile in fileInfo)
{
Files1.Add(NextFile.Name, NextFile.FullName);
}
}
//遍历文件夹2,同样的操作,其实这里可以提取一个方法出来要简便些
FBDialog = new FolderBrowserDialog();
string folder2Path = "";
if (FBDialog.ShowDialog() == DialogResult.OK)
{
string strPath = FBDialog.SelectedPath;
if (strPath.EndsWith("\\"))
folder2Path = strPath;
else
folder2Path = strPath + "\\";
}
Dictionary<string, string> Files2 = new Dictionary<string, string>();
theFolder = new DirectoryInfo(folder2Path);
FileInfo[] fileInfo1 = theFolder.GetFiles("*.pdf");
foreach (FileInfo NextFile in fileInfo1)
{
Files2.Add(NextFile.Name, NextFile.FullName);
}
//遍历Files2,如果Files1中有相同的key,也就是文件name相同,将文件夹2中的pdf的第一页插入到文件夹1中对应pdf的第三页
foreach (string name in Files1.Keys)
{
if (Files2.ContainsKey(name))
{
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile(Files1[name]);
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile(Files2[name]);
//获取文件夹2中pdf的第一页插入到对应pdf的第三页
PdfPageBase page = pdf2.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();
pdf1.Pages.Insert(1, size, new PdfMargins(0, 0));
pdf1.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
pdf1.SaveToFile(Files1[name]);
}
}