Skip to content

Commit 622366d

Browse files
committed
Task-995608-detect Blank sample
1 parent 2b87406 commit 622366d

File tree

9 files changed

+244
-0
lines changed

9 files changed

+244
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="How-to-detect-blank-Pages-in-a-PDF/How-to-detect-blank-Pages-in-a-PDF.csproj" Id="d1c6ae22-ebab-4908-ba4a-22ef418a9a1d" />
3+
</Solution>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{D1C6AE22-EBAB-4908-BA4A-22EF418A9A1D}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>How_to_detect_blank_Pages_in_a_PDF</RootNamespace>
10+
<AssemblyName>How-to-detect-blank-Pages-in-a-PDF</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19+
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>AnyCPU</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="Syncfusion.Compression.Base, Version=31.2462.18.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Syncfusion.Compression.Base.31.2.18\lib\net462\Syncfusion.Compression.Base.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Syncfusion.Licensing, Version=31.2462.18.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Syncfusion.Licensing.31.2.18\lib\net462\Syncfusion.Licensing.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Syncfusion.OCRProcessor.Base, Version=31.2462.18.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Syncfusion.Pdf.OCR.WinForms.31.2.18\lib\net462\Syncfusion.OCRProcessor.Base.dll</HintPath>
46+
</Reference>
47+
<Reference Include="Syncfusion.Pdf.Base, Version=31.2462.18.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Syncfusion.Pdf.WinForms.31.2.18\lib\net462\Syncfusion.Pdf.Base.dll</HintPath>
49+
</Reference>
50+
<Reference Include="Syncfusion.PdfToImageConverter.Base, Version=31.2462.18.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
51+
<HintPath>..\packages\Syncfusion.PdfToImageConverter.WinForms.31.2.18\lib\net462\Syncfusion.PdfToImageConverter.Base.dll</HintPath>
52+
</Reference>
53+
<Reference Include="System" />
54+
<Reference Include="System.Core" />
55+
<Reference Include="System.Drawing" />
56+
<Reference Include="System.Drawing.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
57+
<HintPath>..\packages\System.Drawing.Common.10.0.1\lib\net462\System.Drawing.Common.dll</HintPath>
58+
</Reference>
59+
<Reference Include="System.Xml.Linq" />
60+
<Reference Include="System.Data.DataSetExtensions" />
61+
<Reference Include="Microsoft.CSharp" />
62+
<Reference Include="System.Data" />
63+
<Reference Include="System.Net.Http" />
64+
<Reference Include="System.Xml" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<Compile Include="Program.cs" />
68+
<Compile Include="Properties\AssemblyInfo.cs" />
69+
</ItemGroup>
70+
<ItemGroup>
71+
<None Include="App.config" />
72+
<None Include="packages.config" />
73+
</ItemGroup>
74+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
75+
<Import Project="..\packages\Syncfusion.PdfToImageConverter.WinForms.31.2.18\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets" Condition="Exists('..\packages\Syncfusion.PdfToImageConverter.WinForms.31.2.18\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets')" />
76+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
77+
<PropertyGroup>
78+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
79+
</PropertyGroup>
80+
<Error Condition="!Exists('..\packages\Syncfusion.PdfToImageConverter.WinForms.31.2.18\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Syncfusion.PdfToImageConverter.WinForms.31.2.18\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets'))" />
81+
<Error Condition="!Exists('..\packages\Syncfusion.Pdf.OCR.WinForms.31.2.18\build\net462\Syncfusion.Pdf.OCR.WinForms.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Syncfusion.Pdf.OCR.WinForms.31.2.18\build\net462\Syncfusion.Pdf.OCR.WinForms.targets'))" />
82+
</Target>
83+
<Import Project="..\packages\Syncfusion.Pdf.OCR.WinForms.31.2.18\build\net462\Syncfusion.Pdf.OCR.WinForms.targets" Condition="Exists('..\packages\Syncfusion.Pdf.OCR.WinForms.31.2.18\build\net462\Syncfusion.Pdf.OCR.WinForms.targets')" />
84+
</Project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using Syncfusion.Pdf;
2+
using Syncfusion.Pdf.Parsing;
3+
using Syncfusion.PdfToImageConverter;
4+
using System;
5+
using System.Drawing;
6+
using System.Drawing.Imaging;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Runtime.InteropServices;
10+
11+
namespace How_to_detect_blank_Pages_in_a_PDF
12+
{
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
string[] files = Directory.GetFiles("Data", "*.pdf");
18+
19+
foreach (string file in files)
20+
{
21+
//Load the existing PDF document
22+
PdfLoadedDocument ldoc = new PdfLoadedDocument(file);
23+
24+
PdfToImageConverter converter = new PdfToImageConverter();
25+
converter.Load(new FileStream(file, FileMode.Open));
26+
27+
int blankPageCount = 0;
28+
29+
for (int i = 0; i < ldoc.Pages.Count; i++)
30+
{
31+
PdfLoadedPage lpage = ldoc.Pages[i] as PdfLoadedPage;
32+
if (IsBlankPage(lpage, i, converter))
33+
blankPageCount++;
34+
}
35+
36+
ldoc.Close(true);
37+
38+
Console.WriteLine(file + " has " + blankPageCount + " blank pages");
39+
}
40+
}
41+
private static bool IsBlankPage(PdfLoadedPage lpage, int pageIndex, PdfToImageConverter converter)
42+
{
43+
bool isBlankPage = false;
44+
45+
if (lpage.Annotations.Count > 0)
46+
{
47+
isBlankPage = false;
48+
return isBlankPage;
49+
}
50+
// For images.
51+
Image[] images = lpage.ExtractImages();
52+
53+
if (images.Length > 0)
54+
{
55+
foreach (Image img in images)
56+
{
57+
if (!IsEmpty(img as Bitmap))
58+
{
59+
isBlankPage = false;
60+
break;
61+
}
62+
else
63+
isBlankPage = true;
64+
}
65+
}
66+
else
67+
{
68+
//For shapes
69+
Stream imageSteam = converter.Convert(pageIndex, false, false);
70+
imageSteam.Position = 0;
71+
if (imageSteam != null && IsEmpty(new Bitmap(imageSteam)))
72+
{
73+
isBlankPage = true;
74+
}
75+
else
76+
{
77+
isBlankPage = false;
78+
}
79+
80+
}
81+
return isBlankPage;
82+
}
83+
private static bool IsEmpty(Bitmap image)
84+
{
85+
Rectangle bounds = new Rectangle(0, 0, image.Width, image.Height);
86+
BitmapData bmpData = image.LockBits(bounds, ImageLockMode.ReadWrite, image.PixelFormat);
87+
88+
IntPtr ptr = bmpData.Scan0;
89+
int bytes = Math.Abs(bmpData.Stride) * image.Height;
90+
byte[] rgbValues = new byte[bytes];
91+
92+
Marshal.Copy(ptr, rgbValues, 0, bytes);
93+
image.UnlockBits(bmpData);
94+
95+
// Count non-white pixels
96+
int nonWhitePixelCount = rgbValues.Count(b => b != 255);
97+
98+
// If less than 1% of pixels are non-white, consider it blank
99+
int threshold = (image.Width * image.Height) / 100; // 1%
100+
return nonWhitePixelCount < threshold;
101+
}
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("How-to-detect-blank-Pages-in-a-PDF")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("How-to-detect-blank-Pages-in-a-PDF")]
13+
[assembly: AssemblyCopyright("Copyright © 2025")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("d1c6ae22-ebab-4908-ba4a-22ef418a9a1d")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Syncfusion.Compression.Base" version="31.2.18" targetFramework="net462" />
4+
<package id="Syncfusion.Licensing" version="31.2.18" targetFramework="net462" />
5+
<package id="Syncfusion.Pdf.OCR.WinForms" version="31.2.18" targetFramework="net462" />
6+
<package id="Syncfusion.Pdf.WinForms" version="31.2.18" targetFramework="net462" />
7+
<package id="Syncfusion.PdfToImageConverter.WinForms" version="31.2.18" targetFramework="net462" />
8+
<package id="System.Drawing.Common" version="10.0.1" targetFramework="net462" />
9+
</packages>

0 commit comments

Comments
 (0)