diff --git a/FileMerger.cs b/FileMerger.cs index cde65c3..a011fbe 100644 --- a/FileMerger.cs +++ b/FileMerger.cs @@ -18,7 +18,7 @@ namespace SlideCombine public class FileMerger { - public static List ProcessAllFolders(string pdfRootPath, string txtOutputPath) + public static List ProcessAllFolders(string pdfRootPath, string txtSourcePath, string txtOutputPath) { var results = new List(); @@ -34,6 +34,12 @@ namespace SlideCombine throw new Exception($"在路径 {pdfRootPath} 下未找到任何 FreePic2Pdf_bkmk 或 FreePic2Pdf_bkmk.txt 文件"); } + // 检查TXT源路径是否存在 + if (!Directory.Exists(txtSourcePath)) + { + throw new Exception($"TXT源文件路径不存在: {txtSourcePath}"); + } + // 按基础文件名分组(取文件夹名称的空格前缀) var fileGroups = new Dictionary>(); @@ -56,7 +62,7 @@ namespace SlideCombine // 处理每个分组 foreach (var group in fileGroups) { - var result = ProcessFileGroup(group.Key, group.Value.OrderBy(f => f).ToList()); + var result = ProcessFileGroup(group.Key, group.Value.OrderBy(f => f).ToList(), txtSourcePath); results.Add(result); } } @@ -80,7 +86,7 @@ namespace SlideCombine return spaceIndex > 0 ? folderName.Substring(0, spaceIndex) : folderName; } - private static ProcessResult ProcessFileGroup(string baseName, List bkmkFiles) + private static ProcessResult ProcessFileGroup(string baseName, List bkmkFiles, string txtSourcePath) { var result = new ProcessResult { @@ -95,7 +101,7 @@ namespace SlideCombine foreach (var bkmkFile in bkmkFiles) { // 获取对应的TXT文件路径 - var txtFile = GetCorrespondingTxtFile(bkmkFile); + var txtFile = GetCorrespondingTxtFile(bkmkFile, txtSourcePath); // 创建元数据文档 var metadata = CreateMetadataFromFiles(txtFile, bkmkFile); @@ -119,14 +125,13 @@ namespace SlideCombine return result; } - private static string GetCorrespondingTxtFile(string bkmkFile) + private static string GetCorrespondingTxtFile(string bkmkFile, string txtSourcePath) { var directory = Path.GetDirectoryName(bkmkFile); var folderName = new DirectoryInfo(directory).Name; - // 在TXT文件夹中查找对应的文件 - var txtDirectory = Path.Combine(Directory.GetParent(directory).Parent.FullName, "TXT"); - var txtFile = Path.Combine(txtDirectory, $"{folderName}.txt"); + // 在指定的TXT源路径中查找对应的文件 + var txtFile = Path.Combine(txtSourcePath, $"{folderName}.txt"); return File.Exists(txtFile) ? txtFile : null; } diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 2cf3ada..1a8d2b7 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -36,7 +36,7 @@ namespace SlideCombine // 主窗体设置 AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(700, 550); + ClientSize = new Size(700, 650); Text = "PDF书签合并工具 v1.0"; StartPosition = FormStartPosition.CenterScreen; BackColor = Color.FromArgb(240, 240, 240); @@ -47,6 +47,11 @@ namespace SlideCombine txtSourcePath = new TextBox(); btnBrowseSource = new Button(); + grpTextFolder = new GroupBox(); + lblTextPath = new Label(); + txtTextPath = new TextBox(); + btnBrowseText = new Button(); + grpOutputFolder = new GroupBox(); lblOutputPath = new Label(); txtOutputPath = new TextBox(); @@ -61,27 +66,27 @@ namespace SlideCombine progressBar = new ProgressBar(); txtLog = new RichTextBox(); - // 设置源文件夹组 + // 设置PDF文件夹组 grpSourceFolder.Text = "📁 PDF文件夹路径"; grpSourceFolder.Location = new Point(15, 15); - grpSourceFolder.Size = new Size(670, 80); + grpSourceFolder.Size = new Size(670, 70); grpSourceFolder.TabStop = false; grpSourceFolder.BackColor = Color.White; grpSourceFolder.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); - lblSourcePath.Text = "选择包含PDF文件夹的路径:"; + lblSourcePath.Text = "选择包含PDF文件夹的路径(含FreePic2Pdf_bkmk.txt文件):"; lblSourcePath.Location = new Point(15, 20); - lblSourcePath.Size = new Size(200, 23); + lblSourcePath.Size = new Size(350, 20); lblSourcePath.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); - txtSourcePath.Location = new Point(15, 45); - txtSourcePath.Size = new Size(540, 25); + txtSourcePath.Location = new Point(15, 40); + txtSourcePath.Size = new Size(540, 22); txtSourcePath.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); txtSourcePath.BorderStyle = BorderStyle.FixedSingle; btnBrowseSource.Text = "浏览"; - btnBrowseSource.Location = new Point(560, 44); - btnBrowseSource.Size = new Size(80, 27); + btnBrowseSource.Location = new Point(560, 39); + btnBrowseSource.Size = new Size(80, 24); btnBrowseSource.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); btnBrowseSource.BackColor = Color.FromArgb(66, 139, 202); btnBrowseSource.ForeColor = Color.White; @@ -93,27 +98,59 @@ namespace SlideCombine grpSourceFolder.Controls.Add(txtSourcePath); grpSourceFolder.Controls.Add(btnBrowseSource); + // 设置TXT文件夹组 + grpTextFolder.Text = "📄 TXT源文件路径"; + grpTextFolder.Location = new Point(15, 90); + grpTextFolder.Size = new Size(670, 70); + grpTextFolder.TabStop = false; + grpTextFolder.BackColor = Color.White; + grpTextFolder.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); + + lblTextPath.Text = "选择包含元数据TXT文件的路径:"; + lblTextPath.Location = new Point(15, 20); + lblTextPath.Size = new Size(350, 20); + lblTextPath.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); + + txtTextPath.Location = new Point(15, 40); + txtTextPath.Size = new Size(540, 22); + txtTextPath.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); + txtTextPath.BorderStyle = BorderStyle.FixedSingle; + + btnBrowseText.Text = "浏览"; + btnBrowseText.Location = new Point(560, 39); + btnBrowseText.Size = new Size(80, 24); + btnBrowseText.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); + btnBrowseText.BackColor = Color.FromArgb(66, 139, 202); + btnBrowseText.ForeColor = Color.White; + btnBrowseText.FlatStyle = FlatStyle.Flat; + btnBrowseText.FlatAppearance.BorderSize = 0; + btnBrowseText.Click += new EventHandler(btnBrowseText_Click); + + grpTextFolder.Controls.Add(lblTextPath); + grpTextFolder.Controls.Add(txtTextPath); + grpTextFolder.Controls.Add(btnBrowseText); + // 设置输出文件夹组 - grpOutputFolder.Text = "📄 TXT输出路径"; - grpOutputFolder.Location = new Point(15, 105); - grpOutputFolder.Size = new Size(670, 80); + grpOutputFolder.Text = "💾 最终输出路径"; + grpOutputFolder.Location = new Point(15, 165); + grpOutputFolder.Size = new Size(670, 70); grpOutputFolder.TabStop = false; grpOutputFolder.BackColor = Color.White; grpOutputFolder.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); - lblOutputPath.Text = "选择TXT文件输出路径:"; + lblOutputPath.Text = "选择合并后TXT文件的输出路径:"; lblOutputPath.Location = new Point(15, 20); - lblOutputPath.Size = new Size(200, 23); + lblOutputPath.Size = new Size(350, 20); lblOutputPath.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); - txtOutputPath.Location = new Point(15, 45); - txtOutputPath.Size = new Size(540, 25); + txtOutputPath.Location = new Point(15, 40); + txtOutputPath.Size = new Size(540, 22); txtOutputPath.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); txtOutputPath.BorderStyle = BorderStyle.FixedSingle; btnBrowseOutput.Text = "浏览"; - btnBrowseOutput.Location = new Point(560, 44); - btnBrowseOutput.Size = new Size(80, 27); + btnBrowseOutput.Location = new Point(560, 39); + btnBrowseOutput.Size = new Size(80, 24); btnBrowseOutput.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular); btnBrowseOutput.BackColor = Color.FromArgb(66, 139, 202); btnBrowseOutput.ForeColor = Color.White; @@ -126,7 +163,7 @@ namespace SlideCombine grpOutputFolder.Controls.Add(btnBrowseOutput); // 设置按钮面板 - pnlButtons.Location = new Point(15, 195); + pnlButtons.Location = new Point(15, 240); pnlButtons.Size = new Size(670, 50); pnlButtons.BackColor = Color.Transparent; @@ -166,7 +203,7 @@ namespace SlideCombine // 设置进度组 grpProgress.Text = "📊 处理进度"; - grpProgress.Location = new Point(15, 255); + grpProgress.Location = new Point(15, 295); grpProgress.Size = new Size(670, 280); grpProgress.TabStop = false; grpProgress.BackColor = Color.White; @@ -191,6 +228,7 @@ namespace SlideCombine // 添加所有控件到窗体 Controls.Add(grpSourceFolder); + Controls.Add(grpTextFolder); Controls.Add(grpOutputFolder); Controls.Add(pnlButtons); Controls.Add(grpProgress); @@ -202,6 +240,10 @@ namespace SlideCombine private Label lblSourcePath; private TextBox txtSourcePath; private Button btnBrowseSource; + private GroupBox grpTextFolder; + private Label lblTextPath; + private TextBox txtTextPath; + private Button btnBrowseText; private GroupBox grpOutputFolder; private Label lblOutputPath; private TextBox txtOutputPath; diff --git a/Form1.cs b/Form1.cs index 7586040..9a80e4d 100644 --- a/Form1.cs +++ b/Form1.cs @@ -42,15 +42,28 @@ namespace SlideCombine } } + private void btnBrowseText_Click(object sender, EventArgs e) + { + using (FolderBrowserDialog dialog = new FolderBrowserDialog()) + { + dialog.Description = "请选择包含元数据TXT文件的路径"; + if (dialog.ShowDialog() == DialogResult.OK) + { + txtTextPath.Text = dialog.SelectedPath; + LogInfo($"已选择TXT源路径: {dialog.SelectedPath}"); + } + } + } + private void btnBrowseOutput_Click(object sender, EventArgs e) { using (FolderBrowserDialog dialog = new FolderBrowserDialog()) { - dialog.Description = "请选择TXT文件的输出路径"; + dialog.Description = "请选择合并后TXT文件的输出路径"; if (dialog.ShowDialog() == DialogResult.OK) { txtOutputPath.Text = dialog.SelectedPath; - LogInfo($"已选择输出路径: {dialog.SelectedPath}"); + LogInfo($"已选择最终输出路径: {dialog.SelectedPath}"); } } } @@ -58,6 +71,7 @@ namespace SlideCombine private void btnClear_Click(object sender, EventArgs e) { txtSourcePath.Clear(); + txtTextPath.Clear(); txtOutputPath.Clear(); txtLog.Clear(); progressBar.Value = 0; @@ -75,9 +89,11 @@ namespace SlideCombine try { // 验证输入 - if (string.IsNullOrWhiteSpace(txtSourcePath.Text) || string.IsNullOrWhiteSpace(txtOutputPath.Text)) + if (string.IsNullOrWhiteSpace(txtSourcePath.Text) || + string.IsNullOrWhiteSpace(txtTextPath.Text) || + string.IsNullOrWhiteSpace(txtOutputPath.Text)) { - MessageBox.Show("请选择PDF文件夹路径和TXT输出路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); + MessageBox.Show("请选择所有三个路径:PDF路径、TXT源路径和输出路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } @@ -87,6 +103,12 @@ namespace SlideCombine return; } + if (!Directory.Exists(txtTextPath.Text)) + { + MessageBox.Show("指定的TXT源文件路径不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + // 禁用按钮,防止重复点击 btnMerge.Enabled = false; btnClear.Enabled = false; @@ -97,7 +119,7 @@ namespace SlideCombine Log("开始处理PDF书签文件..."); // 处理文件 - var results = FileMerger.ProcessAllFolders(txtSourcePath.Text, txtOutputPath.Text); + var results = FileMerger.ProcessAllFolders(txtSourcePath.Text, txtTextPath.Text, txtOutputPath.Text); // 显示进度 progressBar.Value = 50;