Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
Chenx221 | b78416fbdb | |
Chenx221 | db520a3833 | |
Chenx221 | 2a064be75f | |
Chenx221 | 2b81bd6f24 | |
Chenx221 | 8c1a818260 | |
Chenx221 | 5bb4616a5f | |
Chenx221 | 41273b7a4f | |
Chenx221 | 4296abea79 | |
Chenx221 | 958b1bf0c3 | |
Chenx221 | fb6a477774 | |
Chenx221 | 3d34359578 |
|
@ -1,9 +1,8 @@
|
|||
|
||||
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}") = "Comic2PDF", "ConsoleApp1\Comic2PDF.csproj", "{A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
|
@ -0,0 +1,59 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignManifests>false</SignManifests>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ManifestCertificateThumbprint>6A11F0253E135DE52237FD68763D28DC2A296075</ManifestCertificateThumbprint>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ManifestKeyFile>ConsoleApp1_TemporaryKey.pfx</ManifestKeyFile>
|
||||
<AssemblyTitle>Comic PDF Helper</AssemblyTitle>
|
||||
<Description>批量转换图像为PDF的小工具</Description>
|
||||
<Product>Comic2PDF</Product>
|
||||
<Copyright>Copyright © Chenx221 2023</Copyright>
|
||||
<AssemblyVersion>1.0.0.3</AssemblyVersion>
|
||||
<FileVersion>1.0.0.3</FileVersion>
|
||||
<StartupObject>ConsoleApp1.Program</StartupObject>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include=".NETFramework,Version=v4.8.1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Microsoft .NET Framework 4.8.1 %28x86 和 x64%29</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
|
||||
<PackageReference Include="itext7" Version="8.0.3" />
|
||||
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.3" />
|
||||
<PackageReference Include="itext7.bouncy-castle-fips-adapter" Version="8.0.3" />
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,100 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>ConsoleApp1</RootNamespace>
|
||||
<AssemblyName>ConsoleApp1</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignManifests>false</SignManifests>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ManifestCertificateThumbprint>6A11F0253E135DE52237FD68763D28DC2A296075</ManifestCertificateThumbprint>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ManifestKeyFile>ConsoleApp1_TemporaryKey.pfx</ManifestKeyFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignAssembly>false</SignAssembly>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="BouncyCastle.Crypto, Version=1.8.9.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
|
||||
<HintPath>..\packages\BouncyCastle.1.8.9\lib\BouncyCastle.Crypto.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="itextsharp, Version=5.5.13.3, Culture=neutral, PublicKeyToken=8354ae6d2174ddca, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\iTextSharp.5.5.13.3\lib\itextsharp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include=".NETFramework,Version=v4.8.1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Microsoft .NET Framework 4.8.1 %28x86 和 x64%29</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
|
@ -1,8 +1,14 @@
|
|||
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 System.Numerics;
|
||||
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
|
@ -10,6 +16,11 @@ namespace ConsoleApp1
|
|||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// 设置控制台的输出编码为UTF-8
|
||||
//Console.OutputEncoding = Encoding.UTF8;
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
Console.OutputEncoding = Encoding.GetEncoding(936);
|
||||
|
||||
// 获取上级文件夹路径
|
||||
Console.Write("请输入漫画所在的根路径:");
|
||||
string parentFolder = Console.ReadLine();
|
||||
|
@ -40,6 +51,9 @@ namespace ConsoleApp1
|
|||
// 循环处理每个漫画文件夹
|
||||
foreach (string comicFolder in comicFolders)
|
||||
{
|
||||
// 输出开始处理提示信息
|
||||
Console.WriteLine($"开始处理漫画文件夹 {comicFolder}...");
|
||||
|
||||
// 确保漫画文件夹中包含图像文件
|
||||
string[] imageFiles = Directory.GetFiles(comicFolder, "*.*", SearchOption.AllDirectories)
|
||||
.Where(file => file.ToLower().EndsWith(".jpg") || file.ToLower().EndsWith(".png"))
|
||||
|
@ -51,8 +65,11 @@ namespace ConsoleApp1
|
|||
continue; // 继续处理下一个漫画文件夹
|
||||
}
|
||||
|
||||
// Sort the imageFiles using the NaturalSortComparer
|
||||
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))
|
||||
|
@ -61,45 +78,53 @@ 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;
|
||||
int currentImageIndex = 0;
|
||||
|
||||
// 逐个添加图像到PDF文档
|
||||
foreach (string imageFile in imageFiles)
|
||||
{
|
||||
// 读取图像文件
|
||||
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFile);
|
||||
// 更新进度条
|
||||
UpdateProgressBar(currentImageIndex, totalImageCount);
|
||||
|
||||
// 计算图像缩放比例,使其适合于PDF页面
|
||||
float widthRatio = pdfDocument.PageSize.Width / image.Width;
|
||||
float heightRatio = pdfDocument.PageSize.Height / image.Height;
|
||||
Image image = new(ImageDataFactory.Create(imageFile));
|
||||
|
||||
// 合适的缩放
|
||||
float widthRatio = pdfDocument.GetDefaultPageSize().GetWidth() / image.GetImageWidth();
|
||||
float heightRatio = pdfDocument.GetDefaultPageSize().GetHeight() / image.GetImageHeight();
|
||||
float ratio = Math.Min(widthRatio, heightRatio);
|
||||
|
||||
// 设置图像缩放比例
|
||||
image.ScalePercent(ratio * 100);
|
||||
|
||||
// 新建一个页面
|
||||
pdfDocument.NewPage();
|
||||
image.Scale(ratio,ratio);
|
||||
|
||||
// 将图像添加到页面中间
|
||||
float x = (pdfDocument.PageSize.Width - image.ScaledWidth) / 2;
|
||||
float y = (pdfDocument.PageSize.Height - image.ScaledHeight) / 2;
|
||||
image.SetAbsolutePosition(x, y);
|
||||
float x = (pdfDocument.GetDefaultPageSize().GetWidth() - image.GetImageScaledWidth()) / 2;
|
||||
float y = (pdfDocument.GetDefaultPageSize().GetHeight() - image.GetImageScaledHeight()) / 2;
|
||||
image.SetFixedPosition(x, y);
|
||||
doc.Add(image);
|
||||
|
||||
// 将图像添加到页面
|
||||
pdfDocument.Add(image);
|
||||
GC.Collect();
|
||||
//image = null;
|
||||
|
||||
// 在除最后一张图像外的图像后添加空白页面
|
||||
if (currentImageIndex < totalImageCount - 1)
|
||||
{
|
||||
doc.Add(new AreaBreak());
|
||||
}
|
||||
|
||||
currentImageIndex++;
|
||||
}
|
||||
|
||||
// 关闭文档
|
||||
//doc.Close(); //这里会导致内存无法自动回收
|
||||
pdfDocument.Close();
|
||||
fileStream.Close();
|
||||
pdfWriter.Close();
|
||||
|
||||
Console.WriteLine(); // 换行,确保进度条后面不会被覆盖
|
||||
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
|
||||
}
|
||||
|
||||
|
@ -112,5 +137,59 @@ namespace ConsoleApp1
|
|||
Console.WriteLine("按任意键退出...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
// Custom Natural Sort Comparer
|
||||
public class NaturalSortComparer : IComparer<string>
|
||||
{
|
||||
public int Compare(string x, string y)
|
||||
{
|
||||
// Get the file names from the full file paths
|
||||
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+)";
|
||||
|
||||
// Get all the matches of numbers in the file names
|
||||
MatchCollection matchesX = Regex.Matches(fileNameX, pattern);
|
||||
MatchCollection matchesY = Regex.Matches(fileNameY, pattern);
|
||||
|
||||
// Compare the matches one by one
|
||||
int matchCount = Math.Min(matchesX.Count, matchesY.Count);
|
||||
for (int i = 0; i < matchCount; i++)
|
||||
{
|
||||
BigInteger numX = BigInteger.Parse(matchesX[i].Value);
|
||||
BigInteger numY = BigInteger.Parse(matchesY[i].Value);
|
||||
|
||||
int numComparison = numX.CompareTo(numY);
|
||||
if (numComparison != 0)
|
||||
return numComparison;
|
||||
|
||||
// Compare the non-numeric parts between the matched numbers
|
||||
int nonNumericComparison = fileNameX.IndexOf(matchesX[i].Value) - fileNameY.IndexOf(matchesY[i].Value);
|
||||
if (nonNumericComparison != 0)
|
||||
return nonNumericComparison;
|
||||
}
|
||||
|
||||
// If the numbers are the same up to this point, compare the remaining non-numeric parts
|
||||
return fileNameX.CompareTo(fileNameY);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新进度条
|
||||
private static void UpdateProgressBar(int current, int total)
|
||||
{
|
||||
int progressBarWidth = 50;
|
||||
int progress = (int)Math.Round((double)current / total * progressBarWidth);
|
||||
|
||||
// 检查是否是最后一次更新
|
||||
if (current == total - 1)
|
||||
progress = progressBarWidth; // 将进度设置为进度条的最大宽度
|
||||
|
||||
string progressBar = "[" + new string('=', progress) + new string(' ', progressBarWidth - progress) + "]";
|
||||
Console.Write($"\r{progressBar} {current + 1}/{total}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="BouncyCastle" version="1.8.9" targetFramework="net481" />
|
||||
<package id="iTextSharp" version="5.5.13.3" targetFramework="net481" />
|
||||
</packages>
|
Loading…
Reference in New Issue