c# 判断图片、pdf是A0、A1、A2、A3、A4

//("A0 841*1189(mm)999949");

//("A1 594*841(mm)499554");

//("A2 420*594(mm)249485");

//("A3 297*420(mm)124740");

//("A4 210*297(mm)62370");

//("B3 353*500(mm)176500");

//("B4 250*353(mm)88250");

//("B6 125*176(mm)22000");

1.图片

int[] array = { 999949, 499554, 249485, 124740, 62370};

string path = @"E:\测试格式图片\A0.jpg";

using (System.Drawing.Image img = System.Drawing.Image.FromFile(path))

{

float dpiX = img.HorizontalResolution;

float dpiY = img.VerticalResolution;

double w = 1.0 * img.Width / dpiX * 25.4;

double h = 1.0 * img.Height / dpiY * 25.4;

string checkSize = "";

var nearest = array.OrderBy(x => Math.Abs((long)x - w * h)).First();

switch (nearest)

{

case 999949: checkSize = "A0";

break;

case 499554: checkSize = "A1";

break;

case 249485: checkSize = "A2";

break;

case 124740: checkSize = "A3";

break;

case 62370: checkSize = "A4";

break;

default: checkSize = "未判断出";

break;

}

}

2. pdf

需引用iTextSharp.dll

https://download.csdn.net/download/weixin_42020830/87610255

string path1 = @"E:\测试格式图片\D.3.1.2019-0782-0017.pdf";

PdfReader reader = new PdfReader(path1);

int iPageNum = reader.NumberOfPages; //获取pdf总页数

for (int i = 1; i < iPageNum + 1; i++)

{

string checkSize = "";

iTextSharp.text.Rectangle rc = reader.GetPageSize(i); //pdf拿到第一页数据

float height = rc.Height; //pdf的长

float width = rc.Width;//pdf的宽

// A0 长X宽 118.9X84.1cm 10011.38

// A1 长X宽 84.1X59.4cm 4995.54

// A2 长X宽 59.4X42cm 2494.80

// A3 长X宽 42X29.7cm 1234.80

// A4 长X宽 29.7X21cm 623.70

double v1 = height * 25.4 / 72; //换算后真实高

double v2 = width * 25.4 / 72; //换算后真实宽

double v3 = v1 * v2;

var nearest = array.OrderBy(x => Math.Abs((long)x - v3)).First();

switch (nearest)

{

case 999949: checkSize = "A0";

break;

case 499554: checkSize = "A1";

break;

case 249485: checkSize = "A2";

break;

case 124740: checkSize = "A3";

break;

case 62370: checkSize = "A4";

break;

default: checkSize = "未判断出";

break;

}

}

reader.Close(); //不关闭会一直占用pdf资源,对接下来的操作会有影响

你可能感兴趣的:(c#)