update
升级.net framework到.net 7.0 相关依赖也进行了升级 但因为itext7处理方法变更,现在仍存在处理后的pdf图像边距较大的问题
This commit is contained in:
parent
41273b7a4f
commit
5bb4616a5f
@ -1,9 +1,9 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.6.33829.357
|
VisualStudioVersion = 17.6.33829.357
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
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
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -1,17 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<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>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
|
||||||
<ProjectGuid>{A4F374FF-7AA7-4360-9597-0E66D6D4D1EC}</ProjectGuid>
|
|
||||||
<OutputType>Exe</OutputType>
|
<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>
|
<PublishUrl>publish\</PublishUrl>
|
||||||
<Install>true</Install>
|
<Install>true</Install>
|
||||||
<InstallFrom>Disk</InstallFrom>
|
<InstallFrom>Disk</InstallFrom>
|
||||||
@ -27,25 +17,7 @@
|
|||||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||||
<UseApplicationTrust>false</UseApplicationTrust>
|
<UseApplicationTrust>false</UseApplicationTrust>
|
||||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||||
</PropertyGroup>
|
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
|
||||||
<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>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<SignManifests>false</SignManifests>
|
<SignManifests>false</SignManifests>
|
||||||
@ -55,35 +27,15 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<ManifestKeyFile>ConsoleApp1_TemporaryKey.pfx</ManifestKeyFile>
|
<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>
|
||||||
<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>
|
<ItemGroup>
|
||||||
<BootstrapperPackage Include=".NETFramework,Version=v4.8.1">
|
<BootstrapperPackage Include=".NETFramework,Version=v4.8.1">
|
||||||
<Visible>False</Visible>
|
<Visible>False</Visible>
|
||||||
@ -96,5 +48,12 @@
|
|||||||
<Install>false</Install>
|
<Install>false</Install>
|
||||||
</BootstrapperPackage>
|
</BootstrapperPackage>
|
||||||
</ItemGroup>
|
</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>
|
</Project>
|
@ -1,11 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using iTextSharp.text;
|
using iText.Kernel.Pdf;
|
||||||
using iTextSharp.text.pdf;
|
using iText.Layout;
|
||||||
|
using iText.Layout.Element;
|
||||||
|
using iText.IO.Image;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using iText.Kernel.Geom;
|
||||||
|
using iText.Layout.Properties;
|
||||||
|
|
||||||
namespace ConsoleApp1
|
namespace ConsoleApp1
|
||||||
{
|
{
|
||||||
@ -15,6 +19,7 @@ namespace ConsoleApp1
|
|||||||
{
|
{
|
||||||
// 设置控制台的输出编码为UTF-8
|
// 设置控制台的输出编码为UTF-8
|
||||||
//Console.OutputEncoding = Encoding.UTF8;
|
//Console.OutputEncoding = Encoding.UTF8;
|
||||||
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||||
Console.OutputEncoding = Encoding.GetEncoding(936);
|
Console.OutputEncoding = Encoding.GetEncoding(936);
|
||||||
|
|
||||||
// 获取上级文件夹路径
|
// 获取上级文件夹路径
|
||||||
@ -65,7 +70,7 @@ namespace ConsoleApp1
|
|||||||
Array.Sort(imageFiles, new NaturalSortComparer());
|
Array.Sort(imageFiles, new NaturalSortComparer());
|
||||||
|
|
||||||
// 生成 PDF 文件名为漫画文件夹的名称
|
// 生成 PDF 文件名为漫画文件夹的名称
|
||||||
string pdfFileName = Path.Combine(outputFolder, $"{Path.GetFileName(comicFolder)}.pdf");
|
string pdfFileName = System.IO.Path.Combine(outputFolder, $"{System.IO.Path.GetFileName(comicFolder)}.pdf");
|
||||||
|
|
||||||
// 检查PDF文件是否已经存在,若存在则跳过当前漫画文件夹的处理
|
// 检查PDF文件是否已经存在,若存在则跳过当前漫画文件夹的处理
|
||||||
if (File.Exists(pdfFileName))
|
if (File.Exists(pdfFileName))
|
||||||
@ -74,13 +79,10 @@ namespace ConsoleApp1
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建一个新的PDF文档
|
PdfWriter pdfWriter = new PdfWriter(pdfFileName);
|
||||||
Document pdfDocument = new Document();
|
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
|
||||||
FileStream fileStream = File.Create(pdfFileName);
|
Document doc = new Document(pdfDocument);
|
||||||
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, fileStream);
|
|
||||||
|
|
||||||
// 打开文档
|
|
||||||
pdfDocument.Open();
|
|
||||||
|
|
||||||
// 进度条相关变量
|
// 进度条相关变量
|
||||||
int totalImageCount = imageFiles.Length;
|
int totalImageCount = imageFiles.Length;
|
||||||
@ -92,34 +94,22 @@ namespace ConsoleApp1
|
|||||||
// 更新进度条
|
// 更新进度条
|
||||||
UpdateProgressBar(currentImageIndex, totalImageCount);
|
UpdateProgressBar(currentImageIndex, totalImageCount);
|
||||||
|
|
||||||
// 读取图像文件
|
Image image = new(ImageDataFactory.Create(imageFile));
|
||||||
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFile);
|
image.SetAutoScale(true);
|
||||||
|
image.SetHorizontalAlignment(HorizontalAlignment.CENTER);
|
||||||
|
doc.Add(image);
|
||||||
|
|
||||||
// 计算图像缩放比例,使其适合于PDF页面
|
// 在除最后一张图像外的图像后添加空白页面
|
||||||
float widthRatio = pdfDocument.PageSize.Width / image.Width;
|
if (currentImageIndex < totalImageCount - 1)
|
||||||
float heightRatio = pdfDocument.PageSize.Height / image.Height;
|
{
|
||||||
float ratio = Math.Min(widthRatio, heightRatio);
|
doc.Add(new AreaBreak());
|
||||||
|
}
|
||||||
// 设置图像缩放比例
|
|
||||||
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);
|
|
||||||
|
|
||||||
currentImageIndex++;
|
currentImageIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关闭文档
|
// 关闭文档
|
||||||
pdfDocument.Close();
|
pdfDocument.Close();
|
||||||
fileStream.Close();
|
|
||||||
pdfWriter.Close();
|
pdfWriter.Close();
|
||||||
|
|
||||||
Console.WriteLine(); // 换行,确保进度条后面不会被覆盖
|
Console.WriteLine(); // 换行,确保进度条后面不会被覆盖
|
||||||
@ -142,8 +132,8 @@ namespace ConsoleApp1
|
|||||||
public int Compare(string x, string y)
|
public int Compare(string x, string y)
|
||||||
{
|
{
|
||||||
// Get the file names from the full file paths
|
// Get the file names from the full file paths
|
||||||
string fileNameX = Path.GetFileName(x);
|
string fileNameX = System.IO.Path.GetFileName(x);
|
||||||
string fileNameY = Path.GetFileName(y);
|
string fileNameY = System.IO.Path.GetFileName(y);
|
||||||
|
|
||||||
// Define the regex pattern to match numbers in the file names
|
// Define the regex pattern to match numbers in the file names
|
||||||
string pattern = @"(\d+)";
|
string pattern = @"(\d+)";
|
||||||
|
@ -1,16 +1,6 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
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: AssemblyTrademark("")]
|
||||||
[assembly: AssemblyCulture("")]
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
@ -21,16 +11,3 @@ using System.Runtime.InteropServices;
|
|||||||
|
|
||||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||||
[assembly: Guid("a4f374ff-7aa7-4360-9597-0e66d6d4d1ec")]
|
[assembly: Guid("a4f374ff-7aa7-4360-9597-0e66d6d4d1ec")]
|
||||||
|
|
||||||
// 程序集的版本信息由下列四个值组成:
|
|
||||||
//
|
|
||||||
// 主版本
|
|
||||||
// 次版本
|
|
||||||
// 生成号
|
|
||||||
// 修订号
|
|
||||||
//
|
|
||||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
|
||||||
//通过使用 "*",如下所示:
|
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
|
||||||
[assembly: AssemblyVersion("1.0.0.1")]
|
|
||||||
[assembly: AssemblyFileVersion("1.0.0.1")]
|
|
||||||
|
@ -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>
|
|
Reference in New Issue
Block a user