内存占用问题得到一定缓解

但不排除潜在的内存问题
This commit is contained in:
Chenx221 2023-08-27 20:55:18 +08:00
parent 8c1a818260
commit 2b81bd6f24
2 changed files with 7 additions and 4 deletions

View File

@ -35,6 +35,7 @@
<FileVersion>1.0.0.2</FileVersion> <FileVersion>1.0.0.2</FileVersion>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<StartupObject>ConsoleApp1.Program</StartupObject> <StartupObject>ConsoleApp1.Program</StartupObject>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.8.1"> <BootstrapperPackage Include=".NETFramework,Version=v4.8.1">

View File

@ -8,8 +8,6 @@ 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
{ {
@ -95,8 +93,8 @@ namespace ConsoleApp1
UpdateProgressBar(currentImageIndex, totalImageCount); UpdateProgressBar(currentImageIndex, totalImageCount);
Image image = new(ImageDataFactory.Create(imageFile)); Image image = new(ImageDataFactory.Create(imageFile));
//image.SetAutoScale(true);
//image.SetHorizontalAlignment(HorizontalAlignment.CENTER); // 合适的缩放
float widthRatio = pdfDocument.GetDefaultPageSize().GetWidth() / image.GetImageWidth(); float widthRatio = pdfDocument.GetDefaultPageSize().GetWidth() / image.GetImageWidth();
float heightRatio = pdfDocument.GetDefaultPageSize().GetHeight() / image.GetImageHeight(); float heightRatio = pdfDocument.GetDefaultPageSize().GetHeight() / image.GetImageHeight();
float ratio = Math.Min(widthRatio, heightRatio); float ratio = Math.Min(widthRatio, heightRatio);
@ -108,6 +106,9 @@ namespace ConsoleApp1
image.SetFixedPosition(x, y); image.SetFixedPosition(x, y);
doc.Add(image); doc.Add(image);
GC.Collect();
//image = null;
// 在除最后一张图像外的图像后添加空白页面 // 在除最后一张图像外的图像后添加空白页面
if (currentImageIndex < totalImageCount - 1) if (currentImageIndex < totalImageCount - 1)
{ {
@ -118,6 +119,7 @@ namespace ConsoleApp1
} }
// 关闭文档 // 关闭文档
//doc.Close(); //这里会导致内存无法自动回收
pdfDocument.Close(); pdfDocument.Close();
pdfWriter.Close(); pdfWriter.Close();