diff --git a/ConsoleApp1/ConsoleApp1.sln b/ConsoleApp1/ConsoleApp1.sln index 86e27de..fa68d88 100644 --- a/ConsoleApp1/ConsoleApp1.sln +++ b/ConsoleApp1/ConsoleApp1.sln @@ -1,9 +1,9 @@ - + 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj index ad1d43d..ff17da3 100644 --- a/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj +++ b/ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj @@ -1,17 +1,7 @@ - - - + - Debug - AnyCPU - {A4F374FF-7AA7-4360-9597-0E66D6D4D1EC} + net7.0-windows10.0.19041.0 Exe - ConsoleApp1 - ConsoleApp1 - v4.8.1 - 512 - true - true publish\ true Disk @@ -27,25 +17,7 @@ false false true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + True false @@ -55,35 +27,15 @@ ConsoleApp1_TemporaryKey.pfx + Comic PDF Helper + 批量转换图像为PDF的小工具 + ConsoleApp1 + Copyright © Chenx221 2023 + 1.0.0.2 + 1.0.0.2 + 7.0 + ConsoleApp1.Program - - false - - - - ..\packages\BouncyCastle.1.8.9\lib\BouncyCastle.Crypto.dll - - - ..\packages\iTextSharp.5.5.13.3\lib\itextsharp.dll - - - - - - - - - - - - - - - - - - - False @@ -96,5 +48,12 @@ false - + + + + + + + + \ No newline at end of file diff --git a/ConsoleApp1/ConsoleApp1/Program.cs b/ConsoleApp1/ConsoleApp1/Program.cs index 61b9580..61c0851 100644 --- a/ConsoleApp1/ConsoleApp1/Program.cs +++ b/ConsoleApp1/ConsoleApp1/Program.cs @@ -1,11 +1,15 @@ using System; using System.IO; -using iTextSharp.text; -using iTextSharp.text.pdf; +using iText.Kernel.Pdf; +using iText.Layout; +using iText.Layout.Element; +using iText.IO.Image; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Text; +using iText.Kernel.Geom; +using iText.Layout.Properties; namespace ConsoleApp1 { @@ -15,6 +19,7 @@ namespace ConsoleApp1 { // 设置控制台的输出编码为UTF-8 //Console.OutputEncoding = Encoding.UTF8; + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); Console.OutputEncoding = Encoding.GetEncoding(936); // 获取上级文件夹路径 @@ -65,7 +70,7 @@ namespace ConsoleApp1 Array.Sort(imageFiles, new NaturalSortComparer()); // 生成 PDF 文件名为漫画文件夹的名称 - string pdfFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.pdf"); + string pdfFileName = System.IO.Path.Combine(outputFolder, $"{System.IO.Path.GetFileName(comicFolder)}.pdf"); // 检查PDF文件是否已经存在,若存在则跳过当前漫画文件夹的处理 if (File.Exists(pdfFileName)) @@ -74,13 +79,10 @@ namespace ConsoleApp1 continue; } - // 创建一个新的PDF文档 - Document pdfDocument = new Document(); - FileStream fileStream = File.Create(pdfFileName); - PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, fileStream); + PdfWriter pdfWriter = new PdfWriter(pdfFileName); + PdfDocument pdfDocument = new PdfDocument(pdfWriter); + Document doc = new Document(pdfDocument); - // 打开文档 - pdfDocument.Open(); // 进度条相关变量 int totalImageCount = imageFiles.Length; @@ -92,34 +94,22 @@ namespace ConsoleApp1 // 更新进度条 UpdateProgressBar(currentImageIndex, totalImageCount); - // 读取图像文件 - iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFile); + Image image = new(ImageDataFactory.Create(imageFile)); + image.SetAutoScale(true); + image.SetHorizontalAlignment(HorizontalAlignment.CENTER); + doc.Add(image); - // 计算图像缩放比例,使其适合于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); + // 在除最后一张图像外的图像后添加空白页面 + if (currentImageIndex < totalImageCount - 1) + { + doc.Add(new AreaBreak()); + } currentImageIndex++; } // 关闭文档 pdfDocument.Close(); - fileStream.Close(); pdfWriter.Close(); Console.WriteLine(); // 换行,确保进度条后面不会被覆盖 @@ -142,8 +132,8 @@ namespace ConsoleApp1 public int Compare(string x, string y) { // Get the file names from the full file paths - string fileNameX = Path.GetFileName(x); - string fileNameY = Path.GetFileName(y); + string fileNameX = System.IO.Path.GetFileName(x); + string fileNameY = System.IO.Path.GetFileName(y); // Define the regex pattern to match numbers in the file names string pattern = @"(\d+)"; diff --git a/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs b/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs index a13d5c4..891e6fb 100644 --- a/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs +++ b/ConsoleApp1/ConsoleApp1/Properties/AssemblyInfo.cs @@ -1,16 +1,6 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("Comic PDF Helper")] -[assembly: AssemblyDescription("批量转换图像为PDF的小工具")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("ConsoleApp1")] -[assembly: AssemblyCopyright("Copyright © Chenx221 2023")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -21,16 +11,3 @@ using System.Runtime.InteropServices; // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID [assembly: Guid("a4f374ff-7aa7-4360-9597-0e66d6d4d1ec")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.1")] -[assembly: AssemblyFileVersion("1.0.0.1")] diff --git a/ConsoleApp1/ConsoleApp1/packages.config b/ConsoleApp1/ConsoleApp1/packages.config deleted file mode 100644 index e28eb0b..0000000 --- a/ConsoleApp1/ConsoleApp1/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file