diff --git a/ConsoleApp1/ConsoleApp1.sln b/ConsoleApp1/ConsoleApp1.sln new file mode 100644 index 0000000..86e27de --- /dev/null +++ b/ConsoleApp1/ConsoleApp1.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FD761E29-0A72-4930-AB5C-0222A52142CA} + EndGlobalSection +EndGlobal diff --git a/ConsoleApp1/ConsoleApp1/App.config b/ConsoleApp1/ConsoleApp1/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/ConsoleApp1/ConsoleApp1/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj new file mode 100644 index 0000000..9f761ba --- /dev/null +++ b/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj @@ -0,0 +1,74 @@ + + + + + Debug + AnyCPU + {A4F374FF-7AA7-4360-9597-0E66D6D4D1EC} + Exe + ConsoleApp1 + ConsoleApp1 + v4.8.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x64 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + false + + + 6A11F0253E135DE52237FD68763D28DC2A296075 + + + ConsoleApp1_TemporaryKey.pfx + + + false + + + + ..\packages\BouncyCastle.1.8.9\lib\BouncyCastle.Crypto.dll + + + ..\packages\iTextSharp.5.5.13.3\lib\itextsharp.dll + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ConsoleApp1/ConsoleApp1/Program.cs b/ConsoleApp1/ConsoleApp1/Program.cs new file mode 100644 index 0000000..30363a8 --- /dev/null +++ b/ConsoleApp1/ConsoleApp1/Program.cs @@ -0,0 +1,116 @@ +using System; +using System.IO; +using iTextSharp.text; +using iTextSharp.text.pdf; +using System.Linq; + +namespace ConsoleApp1 +{ + class Program + { + static void Main(string[] args) + { + // 获取上级文件夹路径 + Console.Write("请输入漫画所在的根路径:"); + string parentFolder = Console.ReadLine(); + + // 确保上级文件夹存在 + if (!Directory.Exists(parentFolder)) + { + Console.WriteLine("根路径不存在。"); + PauseBeforeExit(); + return; + } + + // 获取保存 PDF 的根文件夹路径 + Console.Write("请输入保存PDF位置:"); + string outputFolder = Console.ReadLine(); + + // 确保PDF文件夹存在 + if (!Directory.Exists(outputFolder)) + { + Console.WriteLine("PDF文件夹不存在。"); + PauseBeforeExit(); + return; + } + + // 获取所有漫画文件夹 + string[] comicFolders = Directory.GetDirectories(parentFolder); + + // 循环处理每个漫画文件夹 + foreach (string comicFolder in comicFolders) + { + // 确保漫画文件夹中包含图像文件 + string[] imageFiles = Directory.GetFiles(comicFolder, "*.*", SearchOption.AllDirectories) + .Where(file => file.ToLower().EndsWith(".jpg") || file.ToLower().EndsWith(".png")) + .ToArray(); + + if (imageFiles.Length == 0) + { + Console.WriteLine($"在漫画文件夹 {comicFolder} 中未找到任何图像文件 (.jpg or .png)。"); + continue; // 继续处理下一个漫画文件夹 + } + + // 生成 PDF 文件名为漫画文件夹的名称 + string pdfFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.pdf"); + + // 检查PDF文件是否已经存在,若存在则跳过当前漫画文件夹的处理 + if (File.Exists(pdfFileName)) + { + Console.WriteLine($"PDF文件 {pdfFileName} 已存在,跳过。"); + continue; + } + + // 创建一个新的PDF文档 + Document pdfDocument = new Document(); + FileStream fileStream = File.Create(pdfFileName); + PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, fileStream); + + // 打开文档 + pdfDocument.Open(); + + // 逐个添加图像到PDF文档 + foreach (string imageFile in imageFiles) + { + // 读取图像文件 + iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFile); + + // 计算图像缩放比例,使其适合于PDF页面 + float widthRatio = pdfDocument.PageSize.Width / image.Width; + float heightRatio = pdfDocument.PageSize.Height / image.Height; + float ratio = Math.Min(widthRatio, heightRatio); + + // 设置图像缩放比例 + image.ScalePercent(ratio * 100); + + // 新建一个页面 + pdfDocument.NewPage(); + + // 将图像添加到页面中间 + float x = (pdfDocument.PageSize.Width - image.ScaledWidth) / 2; + float y = (pdfDocument.PageSize.Height - image.ScaledHeight) / 2; + image.SetAbsolutePosition(x, y); + + // 将图像添加到页面 + pdfDocument.Add(image); + } + + // 关闭文档 + pdfDocument.Close(); + fileStream.Close(); + pdfWriter.Close(); + + Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。"); + } + + PauseBeforeExit(); + } + + // 暂停程序,等待用户输入任意键后退出 + private static void PauseBeforeExit() + { + Console.WriteLine("按任意键退出..."); + Console.ReadKey(); + } + } +} diff --git a/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs b/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4a09932 --- /dev/null +++ b/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ConsoleApp1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ConsoleApp1")] +[assembly: AssemblyCopyright("Copyright © 2023")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("a4f374ff-7aa7-4360-9597-0e66d6d4d1ec")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ConsoleApp1/ConsoleApp1/packages.config b/ConsoleApp1/ConsoleApp1/packages.config new file mode 100644 index 0000000..e28eb0b --- /dev/null +++ b/ConsoleApp1/ConsoleApp1/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file