Aspose.Pdf如何检测多页PDF文件中是否存在空白页

作者:控件中国网   出处:控件中国网   2016-11-29 09:42:54   阅读:29

Aspose.Pdf是一款可以完全对PDF文件进行各种操作的.NET、Java类控件,可以快速帮助开发人员在其应用程序中进行PDF文件的创建、编辑、和转换,在使用PDF文档时经常会遇到PDF文档中存在空白页,那么怎么来鉴定是空白页呢?一般情况下页面不包含任何操作符、注释、数据、完全白色的图片或者没有任何像素的图片等,要对这些进行鉴定可以通过下面的几个函数来检测,具体如下:
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;
}
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat