Itext 检测PDF中的彩色页面或BW页面
有什么方法可以检测PDF文件中的彩色页面吗? 例如,我有一个5页的PDF文件,第一页和最后一页都是彩色的。如何检测彩色页面?我能做到吗? 现在我的解决方案是将PDF转换为图像,然后检测图像的颜色或黑白,但这需要很长时间,我需要一种快速的方法。 使用Adobe Acrobat代码将pdf转换为图像Itext 检测PDF中的彩色页面或BW页面,itext,Itext,有什么方法可以检测PDF文件中的彩色页面吗? 例如,我有一个5页的PDF文件,第一页和最后一页都是彩色的。如何检测彩色页面?我能做到吗? 现在我的解决方案是将PDF转换为图像,然后检测图像的颜色或黑白,但这需要很长时间,我需要一种快速的方法。 使用Adobe Acrobat代码将pdf转换为图像 public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string image
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);
public static void ConvertPDF2Image(字符串pdfInputPath、字符串imageOutputPath、,
字符串imageName,int StartPageGenum,int endPageNum,ImageFormat ImageFormat,双分辨率)
{
Acrobat.CAcroPDDoc pdfDoc=null;
Acrobat.CAcroPDPage pdfPage=null;
Acrobat.CAcroRect pdfRect=null;
Acrobat.CAcroPoint pdfPoint=null;
//创建文档(只能使用后期绑定创建AcroExch.PDDoc对象)
//注意:使用VisualBasic辅助函数时,必须添加对DLL的引用
pdfDoc=(Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject(“AcroExch.PDDoc”,”);
//验证参数
如果(!pdfDoc.Open(pdfInputPath)){抛出新文件NotFoundException();}
如果(!Directory.Exists(imageOutputPath)){Directory.CreateDirectory(imageOutputPath);}
如果(startPageNum pdfDoc.GetNumPages()| | endPageNum endPageNum){int tempPageNum=startPageNum;startPageNum=endPageNum;endPageNum=startPageNum;}
如果(imageFormat==null){imageFormat=imageFormat.Jpeg;}
如果(分辨率哪个版本的iText?5或7?多长时间过长,以毫秒为单位?能否共享一个示例文件和当前代码?iText版本5.300页pdf,转换为图像大约一秒钟一页,因此转换为图像大约需要5分钟,然后检测图像颜色黑色和白色,也几乎一秒钟一幅图像,所以一个300页的pdf完成这项工作需要5分钟到10分钟。这不是很多信息。我希望得到一个示例文件和您当前的代码。“我想知道itext是否可以直接检测彩色页面和黑白页面”-对于您来说,什么是彩色页面或黑白页面?您的代码表明,如果以单一给定分辨率渲染的位图图像只有完全黑色或完全白色的像素,则该页面是黑白页面。可能有以更高分辨率渲染的页面显示彩色像素。图像中的插值可能会(取决于分辨率和渲染器)有时显示非黑白像素,有时不显示。这种方法的问题是:某些对象本身可能有颜色,但另一些对象重叠,可能会将其更改为黑白。
////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");
}
}