怎样去除PDF中的空白页 - GdPicture.NET使用教程

PDF文件进行扫描的时候,有时PDF中会有一些空白页存在,扫描出来也是空白的,所以在扫描前往往需要加载GdPicture.NET,先一页一页的进行检查,这样往往会很麻烦,实际上在使用GdPicture.NET的时候,只需要几段简单的代码,就可以除去PDF中的空白页,这里提供一段示例代码,适用于GdPicture.NET 8.5.3或者更高的版本。

》》》GdPicture.NET最新试用版下载地址

示例代码如下:

const int RASTER_DPI = 200;
            GdPictureImaging oGdPictureImaging = new GdPictureImaging();
            GdPicturePDF oGdPicturePDF = new GdPicturePDF();
            oGdPicturePDF.LoadFromFile(@"c:\input.pdf", false);
            oGdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitPoint);
            oGdPicturePDF.EnableCompression(true);
            for (int i = 1; i <= oGdPicturePDF.GetPageCount(); i++)
            {
                oGdPicturePDF.SelectPage(i);
                int rasterImageID = oGdPicturePDF.RenderPageToGdPictureImageEx(RASTER_DPI, false);
                int bitDepth = oGdPictureImaging.GetBitDepth(rasterImageID);
                if (oGdPictureImaging.IsBlank(rasterImageID))
                {
                    //page is blank, we remove it
                    oGdPicturePDF.DeletePage(i--);
                }
                else
                {
                    //if the page is based on a single color image, we convert it to 1bpp. Warning: not 100% safe
                    if (bitDepth > 8 && !oGdPicturePDF.PageHasText() && !oGdPicturePDF.PageHasShape() && oGdPicturePDF.GetPageImageCount() == 1)
                    {
                        if (oGdPictureImaging.ConvertTo1BppAT(rasterImageID) == GdPictureStatus.OK)
                        {
                            //we remove the page then create a new one to remove unused resources on pack
                            float pageWidth = oGdPicturePDF.GetPageWidth();
                            float pageHeight = oGdPicturePDF.GetPageHeight();
                            oGdPicturePDF.DeletePage(i);
                            oGdPicturePDF.InsertPage(pageWidth, pageHeight, i);
                            string pdfImageResName = oGdPicturePDF.AddImageFromGdPictureImage(rasterImageID, false, false);
                            if (pdfImageResName != "")
                            {
                                oGdPicturePDF.DrawImage(pdfImageResName, 0, 0, pageWidth, pageHeight);
                            }
                            else
                            {
                                throw new Exception("Error embedding bitmap in PDF");
                            }
                        }
                        else
                        {
                            throw new Exception("Error during bitmap thresholding");
                        }
                    }
                }
                oGdPictureImaging.ReleaseGdPictureImage(rasterImageID);
            }
            oGdPicturePDF.SaveToFile(@"c:\newpdf.pdf", true); //we have to pack the doc to remove unused enbedded bitmaps
            oGdPicturePDF.CloseDocument();


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值