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