以PDF格式检测彩色页面或BW页面

时间:2017-02-25 12:56:03

标签: itext

有什么方法可以检测PDF文件中的彩页吗? 例如,我有一个5页的PDF文件,第一页和最后一页是彩色的。如何检测彩页? iText能做到吗? 现在我的解决方案是将PDF转换为图像,然后检测图像颜色或黑色和白色,但这需要很长时间才能完成,我需要快速的方法。 通过Adobe Acrobat Code将pdf转换为图像作为研究员

public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
        string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
    {
        Acrobat.CAcroPDDoc pdfDoc = null;
        Acrobat.CAcroPDPage pdfPage = null;
        Acrobat.CAcroRect pdfRect = null;
        Acrobat.CAcroPoint pdfPoint = null;
        // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
        // Note using VisualBasic helper functions, have to add reference to DLL
        pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
        // validate parameter
        if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); }
        if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
        if (startPageNum <= 0) { startPageNum = 1; }
        if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }
        if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
        if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; }
        if (resolution <= 0) { resolution = 1; }
        // start to convert each page
        for (int i = startPageNum; i <= endPageNum; i++)
        {
            pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
            pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
            pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
            int imgWidth = (int)((double)pdfPoint.x * resolution);
            int imgHeight = (int)((double)pdfPoint.y * resolution);
            pdfRect.Left = 0;
            pdfRect.right = (short)imgWidth;
            pdfRect.Top = 0;
            pdfRect.bottom = (short)imgHeight;
            // Render to clipboard, scaled by 100 percent (ie. original size)
            // Even though we want a smaller image, better for us to scale in .NET
            // than Acrobat as it would greek out small text
            pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));
            IDataObject clipboardData = Clipboard.GetDataObject();
            if (clipboardData.GetDataPresent(DataFormats.Bitmap))
            {
                Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
                pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + i.ToString() + "." + imageFormat.ToString(), imageFormat);
                pdfBitmap.Dispose();
            }
        }
        pdfDoc.Close();
        Marshal.ReleaseComObject(pdfPage);
        Marshal.ReleaseComObject(pdfRect);
        Marshal.ReleaseComObject(pdfDoc);
        Marshal.ReleaseComObject(pdfPoint);

}

////detect image  Color  or black and white as fellows 
Bitmap box1 = new Bitmap(PictureBox1.Image);
Color c = new Color()
int rr, gg, bb; 
for(int i=0;i<PictureBox1.Width;i++){
for(int j=0;j<PictureBox1.Height;j++){
c= box1.GetPixel(i,j);
rr= c.R; gg=c.g;bb=c.B;       
if(c ==Color.Black||c= Color.White){
     MessageBox.Show("black and white dot")
  }
  else {
     if(rr==gg==bb){
         MessageBox.Show("Gray dot");
     }
     else {
         MessageBOx.Show("Color dot");
      } 
  }

} }

0 个答案:

没有答案