InPDFWhen a file is scanned, sometimes some blank pages in the PDF file exist, and the scanning results are also blank. Therefore, it is often necessary to load the file before scanning.Gdpicture. net, Check one page at a time, which is often very troublesome. In fact, gdpicture is used. net, you only need a few simple codes to remove the blank pages in the PDF. Here we provide a sample code for gdpicture. net 8.5.3 or later.
"Gdpicture. Net latest trial version
The sample code is as follows:
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();