This commit is contained in:
Chenx221 2023-07-24 10:36:22 +08:00
parent c8e0d476f7
commit 6b85c88439
6 changed files with 262 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
</configuration>

View File

@ -0,0 +1,74 @@
<?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>
</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>x64</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="ConsoleApp1_TemporaryKey.pfx" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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();
}
}
}

View File

@ -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")]

View File

@ -0,0 +1,5 @@
<?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>