How to remove blank pages in PDF-gdpicture. Net tutorial

Source: Internet
Author: User

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();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.