初学C#还望高手指点为谢
只有本站会员才能查看附件,请 登录
!已经学着搞了一串代码,但是运行不成功,请知道的老师指点一下!

using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using namespace wenjianjia
{
public partial class PDFMerge : Form
{
public PDFMerge()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//获取文件夹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)
{
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile(Files1[name]);
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile(Files2[name]);
if (Files2.ContainsKey(name))
{
//获取文件夹2中pdf的第一页插入到对应pdf的第三页
PdfPageBase page = pdf1.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();
pdf2.Pages.Insert(2, size, new PdfMargins(0, 0));
pdf2.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
pdf2.Pages.Add(size, new PdfMargins(0, 0));
pdf2.SaveToFile(Files1[name]);
System.Diagnostics.Process.Start(Files1[name]);
}
}
}
}
}
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using namespace wenjianjia
{
public partial class PDFMerge : Form
{
public PDFMerge()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//获取文件夹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)
{
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile(Files1[name]);
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile(Files2[name]);
if (Files2.ContainsKey(name))
{
//获取文件夹2中pdf的第一页插入到对应pdf的第三页
PdfPageBase page = pdf1.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();
pdf2.Pages.Insert(2, size, new PdfMargins(0, 0));
pdf2.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
pdf2.Pages.Add(size, new PdfMargins(0, 0));
pdf2.SaveToFile(Files1[name]);
System.Diagnostics.Process.Start(Files1[name]);
}
}
}
}
}