Skip to content

Commit dfe58f2

Browse files
committed
995608: Added detect black page code example in PDF document
1 parent 99f3741 commit dfe58f2

File tree

7 files changed

+244
-0
lines changed

7 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="Detect-blank-pages-in-PDF/Detect-blank-pages-in-PDF.csproj" />
3+
</Solution>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Syncfusion.Compression.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-100.2460.1.0" newVersion="100.2460.1.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
14+
</configuration>
Binary file not shown.
Lines changed: 84 additions & 0 deletions
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>{4B7242BE-AF63-460E-9F18-8F7BBA75E15E}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Detect_blank_pages_in_PDF</RootNamespace>
10+
<AssemblyName>Detect-blank-pages-in-PDF</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</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=100.2460.1.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Syncfusion.Compression.Base.100.2.38\lib\net462\Syncfusion.Compression.Base.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Syncfusion.ImagePreProcessor.Base, Version=32.2462.4.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Syncfusion.ImagePreProcessor.WinForms.32.2.4\lib\net462\Syncfusion.ImagePreProcessor.Base.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Syncfusion.Licensing, Version=32.2462.4.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Syncfusion.Licensing.32.2.4\lib\net462\Syncfusion.Licensing.dll</HintPath>
46+
</Reference>
47+
<Reference Include="Syncfusion.OCRProcessor.Base, Version=32.2462.4.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Syncfusion.Pdf.OCR.WinForms.32.2.4\lib\net462\Syncfusion.OCRProcessor.Base.dll</HintPath>
49+
</Reference>
50+
<Reference Include="Syncfusion.Pdf.Base, Version=32.2462.4.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
51+
<HintPath>..\packages\Syncfusion.Pdf.WinForms.32.2.4\lib\net462\Syncfusion.Pdf.Base.dll</HintPath>
52+
</Reference>
53+
<Reference Include="Syncfusion.PdfToImageConverter.Base, Version=32.2462.4.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
54+
<HintPath>..\packages\Syncfusion.PdfToImageConverter.WinForms.32.2.4\lib\net462\Syncfusion.PdfToImageConverter.Base.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System" />
57+
<Reference Include="System.Core" />
58+
<Reference Include="System.Drawing" />
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.32.2.4\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets" Condition="Exists('..\packages\Syncfusion.PdfToImageConverter.WinForms.32.2.4\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.32.2.4\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Syncfusion.PdfToImageConverter.WinForms.32.2.4\build\net462\Syncfusion.PdfToImageConverter.WinForms.targets'))" />
81+
<Error Condition="!Exists('..\packages\Syncfusion.Pdf.OCR.WinForms.32.2.4\build\net462\Syncfusion.Pdf.OCR.WinForms.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Syncfusion.Pdf.OCR.WinForms.32.2.4\build\net462\Syncfusion.Pdf.OCR.WinForms.targets'))" />
82+
</Target>
83+
<Import Project="..\packages\Syncfusion.Pdf.OCR.WinForms.32.2.4\build\net462\Syncfusion.Pdf.OCR.WinForms.targets" Condition="Exists('..\packages\Syncfusion.Pdf.OCR.WinForms.32.2.4\build\net462\Syncfusion.Pdf.OCR.WinForms.targets')" />
84+
</Project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Syncfusion.Pdf;
2+
using Syncfusion.Pdf.Parsing;
3+
using Syncfusion.PdfToImageConverter;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Drawing;
8+
using System.Linq;
9+
using System.Runtime.InteropServices;
10+
using System.Drawing.Imaging;
11+
12+
namespace Detect_blank_pages_in_PDF
13+
{
14+
internal class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
19+
using (FileStream file = new FileStream(Path.GetFullPath("Data/Input.pdf"), FileMode.Open, FileAccess.Read))
20+
{
21+
// Load the existing PDF document
22+
PdfLoadedDocument ldoc = new PdfLoadedDocument(file);
23+
PdfToImageConverter converter = new PdfToImageConverter();
24+
converter.Load(file);
25+
List<int> emptyPages = new List<int>();
26+
for (int i = 0; i < ldoc.Pages.Count; i++)
27+
{
28+
PdfLoadedPage lpage = ldoc.Pages[i] as PdfLoadedPage;
29+
if (IsBlankPage(lpage, i, converter))
30+
{
31+
emptyPages.Add(i + 1); // +1 because pages are normally 1‑based
32+
Console.WriteLine($"Page {i + 1} is blank.");
33+
}
34+
}
35+
ldoc.Close(true);
36+
Console.WriteLine();
37+
Console.WriteLine($"Total blank pages: {emptyPages.Count}");
38+
}
39+
40+
}
41+
// Determines whether a page is blank.
42+
private static bool IsBlankPage(PdfLoadedPage lpage, int pageIndex, PdfToImageConverter converter)
43+
{
44+
bool isBlankPage = false;
45+
46+
if (lpage.Annotations.Count > 0)
47+
{
48+
isBlankPage = false;
49+
return isBlankPage;
50+
}
51+
// For images.
52+
System.Drawing.Image[] images = lpage.ExtractImages();
53+
54+
if (images.Length > 0)
55+
{
56+
foreach (System.Drawing.Image img in images)
57+
{
58+
if (!IsEmpty(img as Bitmap))
59+
{
60+
isBlankPage = false;
61+
break;
62+
}
63+
else
64+
isBlankPage = true;
65+
}
66+
}
67+
else
68+
{
69+
//For shapes
70+
Stream imageSteam = converter.Convert(pageIndex, false, false);
71+
imageSteam.Position = 0;
72+
if (imageSteam != null && IsEmpty(new Bitmap(imageSteam)))
73+
{
74+
isBlankPage = true;
75+
}
76+
else
77+
{
78+
isBlankPage = false;
79+
}
80+
81+
}
82+
return isBlankPage;
83+
}
84+
// Determines if a bitmap is "empty" by counting non-white pixels.
85+
private static bool IsEmpty(Bitmap image)
86+
{
87+
Rectangle bounds = new Rectangle(0, 0, image.Width, image.Height);
88+
BitmapData bmpData = image.LockBits(bounds, ImageLockMode.ReadWrite, image.PixelFormat);
89+
IntPtr ptr = bmpData.Scan0;
90+
int bytes = Math.Abs(bmpData.Stride) * image.Height;
91+
byte[] rgbValues = new byte[bytes];
92+
Marshal.Copy(ptr, rgbValues, 0, bytes);
93+
image.UnlockBits(bmpData);
94+
// Count non-white pixels
95+
int nonWhitePixelCount = rgbValues.Count(b => b != 255);
96+
// If less than 1% of pixels are non-white, consider it blank
97+
int threshold = (image.Width * image.Height) / 100; // 1%
98+
return nonWhitePixelCount < threshold;
99+
}
100+
}
101+
}
Lines changed: 33 additions & 0 deletions
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("Detect-blank-pages-in-PDF")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Detect-blank-pages-in-PDF")]
13+
[assembly: AssemblyCopyright("Copyright © 2026")]
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("4b7242be-af63-460e-9f18-8f7bba75e15e")]
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")]
Lines changed: 9 additions & 0 deletions
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="100.2.38" targetFramework="net48" />
4+
<package id="Syncfusion.ImagePreProcessor.WinForms" version="32.2.4" targetFramework="net48" />
5+
<package id="Syncfusion.Licensing" version="32.2.4" targetFramework="net48" />
6+
<package id="Syncfusion.Pdf.OCR.WinForms" version="32.2.4" targetFramework="net48" />
7+
<package id="Syncfusion.Pdf.WinForms" version="32.2.4" targetFramework="net48" />
8+
<package id="Syncfusion.PdfToImageConverter.WinForms" version="32.2.4" targetFramework="net48" />
9+
</packages>

0 commit comments

Comments
 (0)