SlideCombine/Form1.cs
yuuko 505715c05e 实现PDF书签合并功能
- 添加BookmarkExtractor类用于从FreePic2Pdf_bkmk文件提取书签内容
- 添加ContentFormatter类实现内容格式化处理
- 添加FileMerger类实现文件智能合并功能
- 更新主界面支持路径选择和处理进度显示
- 支持按文件名前缀自动合并(如CH-875 1-3和CH-875 4-6合并为CH-875.txt)
- 输出格式符合需求:tableOfContents与subject之间插入格式化内容
- 支持UTF-8和GBK编码自动检测
- 添加详细的使用说明文档

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-24 15:44:37 +08:00

162 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace SlideCombine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 设置窗体图标 - 使用嵌入资源
try
{
var stream = GetType().Assembly.GetManifestResourceStream("SlideCombine.app.ico");
if (stream != null)
{
this.Icon = new Icon(stream);
}
}
catch
{
// 如果图标加载失败,使用默认图标
// this.Icon = SystemIcons.Application;
}
}
private void btnBrowseSource_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.Description = "选择文件夹";
if (dialog.ShowDialog() == DialogResult.OK)
{
txtSourcePath.Text = dialog.SelectedPath;
Log($"选择的文件夹: {dialog.SelectedPath}");
}
}
}
private void btnBrowseOutput_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.Description = "选择输出文件夹";
if (dialog.ShowDialog() == DialogResult.OK)
{
txtOutputPath.Text = dialog.SelectedPath;
Log($"输出文件夹: {dialog.SelectedPath}");
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtSourcePath.Clear();
txtOutputPath.Clear();
txtLog.Clear();
progressBar.Value = 0;
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnMerge_Click(object sender, EventArgs e)
{
try
{
// 验证输入
if (string.IsNullOrWhiteSpace(txtSourcePath.Text) || string.IsNullOrWhiteSpace(txtOutputPath.Text))
{
MessageBox.Show("请选择PDF文件夹路径和TXT输出路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!Directory.Exists(txtSourcePath.Text))
{
MessageBox.Show("指定的PDF文件夹路径不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 禁用按钮,防止重复点击
btnMerge.Enabled = false;
btnClear.Enabled = false;
// 重置进度条和日志
progressBar.Value = 0;
txtLog.Clear();
Log("开始处理PDF书签文件...");
// 处理文件
var results = FileMerger.ProcessAllFolders(txtSourcePath.Text, txtOutputPath.Text);
// 显示进度
progressBar.Value = 50;
Log($"找到 {results.Count} 个文件组需要处理");
// 保存结果
FileMerger.SaveResults(results, txtOutputPath.Text);
progressBar.Value = 100;
// 统计成功和失败的数量
int successCount = 0;
int failCount = 0;
var sb = new StringBuilder();
foreach (var result in results)
{
if (result.Success)
{
successCount++;
Log($"✓ 成功处理: {result.BaseFileName} (合并了 {result.SourceFiles.Count} 个文件)");
}
else
{
failCount++;
Log($"✗ 处理失败: {result.ErrorMessage}");
}
}
Log($"处理完成! 成功: {successCount}, 失败: {failCount}");
if (successCount > 0)
{
MessageBox.Show($"书签合并完成!\n成功处理 {successCount} 个文件\n输出路径: {txtOutputPath.Text}",
"处理完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("没有成功处理任何文件,请检查输入路径和文件格式。",
"处理失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
Log($"错误: {ex.Message}");
MessageBox.Show($"处理过程中发生错误:\n{ex.Message}",
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// 重新启用按钮
btnMerge.Enabled = true;
btnClear.Enabled = true;
}
}
private void Log(string msg)
{
txtLog.AppendText($"{msg}\r\n");
txtLog.ScrollToCaret();
Application.DoEvents();
}
}
}