升级.net framework到.net 7.0
相关依赖也进行了升级
但因为itext7处理方法变更,现在仍存在处理后的pdf图像边距较大的问题
This commit is contained in:
Chenx221 2023-08-27 20:09:14 +08:00
parent 41273b7a4f
commit 5bb4616a5f
5 changed files with 43 additions and 122 deletions

View File

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

View File

@ -1,17 +1,7 @@
<?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')" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}</ProjectGuid>
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
<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>
@ -27,25 +17,7 @@
<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>
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup>
<SignManifests>false</SignManifests>
@ -55,35 +27,15 @@
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>ConsoleApp1_TemporaryKey.pfx</ManifestKeyFile>
<AssemblyTitle>Comic PDF Helper</AssemblyTitle>
<Description>批量转换图像为PDF的小工具</Description>
<Product>ConsoleApp1</Product>
<Copyright>Copyright © Chenx221 2023</Copyright>
<AssemblyVersion>1.0.0.2</AssemblyVersion>
<FileVersion>1.0.0.2</FileVersion>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<StartupObject>ConsoleApp1.Program</StartupObject>
</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>
@ -96,5 +48,12 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
<PackageReference Include="itext7" Version="8.0.1" />
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.1" />
<PackageReference Include="itext7.bouncy-castle-fips-adapter" Version="8.0.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</ItemGroup>
</Project>

View File

@ -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+)";

View File

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

View File

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