Compare commits
2 Commits
3d34359578
...
958b1bf0c3
Author | SHA1 | Date |
---|---|---|
Chenx221 | 958b1bf0c3 | |
Chenx221 | fb6a477774 |
|
@ -3,6 +3,9 @@ using System.IO;
|
|||
using iTextSharp.text;
|
||||
using iTextSharp.text.pdf;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
|
@ -10,6 +13,9 @@ namespace ConsoleApp1
|
|||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// 设置控制台的输出编码为UTF-8
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
|
||||
// 获取上级文件夹路径
|
||||
Console.Write("请输入漫画所在的根路径:");
|
||||
string parentFolder = Console.ReadLine();
|
||||
|
@ -51,6 +57,9 @@ namespace ConsoleApp1
|
|||
continue; // 继续处理下一个漫画文件夹
|
||||
}
|
||||
|
||||
// Sort the imageFiles using the NaturalSortComparer
|
||||
Array.Sort(imageFiles, new NaturalSortComparer());
|
||||
|
||||
// 生成 PDF 文件名为漫画文件夹的名称
|
||||
string pdfFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.pdf");
|
||||
|
||||
|
@ -69,9 +78,16 @@ namespace ConsoleApp1
|
|||
// 打开文档
|
||||
pdfDocument.Open();
|
||||
|
||||
// 进度条相关变量
|
||||
int totalImageCount = imageFiles.Length;
|
||||
int currentImageIndex = 0;
|
||||
|
||||
// 逐个添加图像到PDF文档
|
||||
foreach (string imageFile in imageFiles)
|
||||
{
|
||||
// 更新进度条
|
||||
UpdateProgressBar(currentImageIndex, totalImageCount);
|
||||
|
||||
// 读取图像文件
|
||||
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFile);
|
||||
|
||||
|
@ -93,6 +109,8 @@ namespace ConsoleApp1
|
|||
|
||||
// 将图像添加到页面
|
||||
pdfDocument.Add(image);
|
||||
|
||||
currentImageIndex++;
|
||||
}
|
||||
|
||||
// 关闭文档
|
||||
|
@ -100,6 +118,7 @@ namespace ConsoleApp1
|
|||
fileStream.Close();
|
||||
pdfWriter.Close();
|
||||
|
||||
Console.WriteLine(); // 换行,确保进度条后面不会被覆盖
|
||||
Console.WriteLine($"漫画文件夹 {comicFolder} 转换完成。");
|
||||
}
|
||||
|
||||
|
@ -112,5 +131,55 @@ namespace ConsoleApp1
|
|||
Console.WriteLine("按任意键退出...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
// Custom Natural Sort Comparer
|
||||
public class NaturalSortComparer : IComparer<string>
|
||||
{
|
||||
public int Compare(string x, string y)
|
||||
{
|
||||
// Define the regex pattern to match numbers in the strings
|
||||
string pattern = @"(\d+)";
|
||||
|
||||
// Get all the matches of numbers in the strings
|
||||
MatchCollection matchesX = Regex.Matches(x, pattern);
|
||||
MatchCollection matchesY = Regex.Matches(y, pattern);
|
||||
|
||||
// Compare the matches one by one
|
||||
int matchCount = Math.Min(matchesX.Count, matchesY.Count);
|
||||
for (int i = 0; i < matchCount; i++)
|
||||
{
|
||||
int numX = int.Parse(matchesX[i].Value);
|
||||
int numY = int.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 = x.IndexOf(matchesX[i].Value) - y.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 x.CompareTo(y);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新进度条
|
||||
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}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue