c# 判斷圖片、pdf是A0、A1、A2、A3、A4

過朢發表於2024-03-11

//("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資源,對接下來的操作會有影響

相關文章