Hi Andrew,
Thanks for your inquiry. In reference to PDF, you can check a blank page using DOM. please check following code it checks whether page doesn’t contain any operators/annotations, data with white color, images with white pixels and no text. Please try IsBlankPage() instead of IsPageBlank as following.
Moreover I’m moving your request to Aspose.Total, there my colleague from Aspose.Words will address the generation of blank page during mail merge.
…
if (IsBlankPage(page))
…
static private bool HasOnlyWhiteColor(Page page)
{
foreach (Operator op in page.Contents)
if (op is Operator.SetColorOperator)
{
Operator.SetColorOperator opSC = op as Operator.SetColorOperator;
System.Drawing.Color color = opSC.getColor();
if (color.R != 255 || color.G != 255 || color.B != 255)
return false;
}
return true;
}
static private bool IsWhiteImage(XImage image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
for (int j = 0; j < bmp.Height; j++)
for (int i = 0; i < bmp.Width; i++)
{
System.Drawing.Color color = bmp.GetPixel(i, j);
if (color.R != 255 || color.G != 255 || color.B != 255)
return false;
}
return true;
}
static private bool HasOnlyWhiteImages(Page page)
{
// return true if no images exist or all images are white
if (page.Resources.Images.Count == 0)
return true;
foreach (XImage image in page.Resources.Images)
if (!IsWhiteImage(image))
return false;
return true;
}
static private bool HasNoText(Page page)
{
var textabsorber = new Aspose.Pdf.Text.TextAbsorber();
page.Accept(textabsorber);
string content = textabsorber.Text;
if (content.Trim().Length == 0)
return true;
return false;
}
static private bool IsBlankPage(Page page)
{
if ((page.Contents.Count == 0 && page.Annotations.Count == 0) ||HasNoText(page)||
(HasOnlyWhiteColor(page) && HasOnlyWhiteImages(page)))
return true;
return false;
}
Best Regards