From 5bb4616a5fcc743825d46b3c8c5ae298112465ce Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sun, 27 Aug 2023 20:09:14 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E5=8D=87=E7=BA=A7.net=20framework?= =?UTF-8?q?=E5=88=B0.net=207.0=20=E7=9B=B8=E5=85=B3=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E4=B9=9F=E8=BF=9B=E8=A1=8C=E4=BA=86=E5=8D=87=E7=BA=A7=20?= =?UTF-8?q?=E4=BD=86=E5=9B=A0=E4=B8=BAitext7=E5=A4=84=E7=90=86=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=8F=98=E6=9B=B4=EF=BC=8C=E7=8E=B0=E5=9C=A8=E4=BB=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E5=A4=84=E7=90=86=E5=90=8E=E7=9A=84pdf?= =?UTF-8?q?=E5=9B=BE=E5=83=8F=E8=BE=B9=E8=B7=9D=E8=BE=83=E5=A4=A7=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConsoleApp1/ConsoleApp1.sln | 4 +- ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj | 79 +++++-------------- ConsoleApp1/ConsoleApp1/Program.cs | 54 ++++++------- .../ConsoleApp1/Properties/AssemblyInfo.cs | 23 ------ ConsoleApp1/ConsoleApp1/packages.config | 5 -- 5 files changed, 43 insertions(+), 122 deletions(-) delete mode 100644 ConsoleApp1/ConsoleApp1/packages.config 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