原創|其它|編輯:郝浩|2012-12-11 15:47:15.000|閱讀 654 次
概述:有時PDF中會有一些空白頁存在,掃描出來也是空白的,在使用GdPicture.NET的時候,只需要幾段簡單的代碼,就可以除去PDF中的空白頁。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在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();
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網