[英]c# PDF to Bmp for free
我正在編寫一個使用 OCR (tessnet2) 掃描圖像文件並提取某些信息的程序。 在我發現要從 Exchange 服務器掃描 PDF 附件之前,這很容易。
我正在處理的第一個問題是如何將我的 PDF 轉換為 BMP 文件。 就我目前所知的 TessNet2 而言,它只能讀取圖像文件——特別是 BMP。 所以我現在的任務是將不確定大小(2 - 15 頁)的 PDF 轉換為 BMP 圖像。 完成后,我可以使用我已經用 TessNet2 構建的代碼輕松掃描每個圖像。
我已經看到使用 Ghostscript 來完成這項任務的事情——我只是想知道是否有另一種免費的解決方案,或者你們中的一個優秀的人是否可以給我一個關於如何使用 Ghostscript 執行此操作的速成課程。
找到有關將PDF轉換為圖像的CodeProject文章:
http://www.codeproject.com/Articles/57100/Simple-and-Free-PDF-to-Image-Conversion
您也可以使用ImageMagick 。 它完全免費! 沒有試用或付款。
只需從這里下載ImageMagick .exe。 安裝它並在此處下載NuGet文件。
有代碼! 希望我幫忙! (盡管問題是在6年前提出的......)
程序:
using ImageMagick;
public void PDFToBMP(string output)
{
MagickReadSettings settings = new MagickReadSettings();
// Settings the density to 500 dpi will create an image with a better quality
settings.Density = new Density(500);
string[] files= GetFiles();
foreach (string file in files)
{
string fichwithout = Path.GetFileNameWithoutExtension(file);
string path = Path.Combine(output, fichwithout);
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(fich);
foreach (MagickImage image in images)
{
settings.Height = image.Height;
settings.Width = image.Width;
image.Format = MagickFormat.Bmp; //if you want to do other formats of image, just change the extension here!
image.Write(path + ".bmp"); //and here!
}
}
}
}
函數GetFiles()
:
public string[] GetFiles()
{
if (!Directory.Exists(@"your\path"))
{
Directory.CreateDirectory(@"your\path");
}
DirectoryInfo dirInfo = new DirectoryInfo(@"your\path");
FileInfo[] fileInfos = dirInfo.GetFiles();
ArrayList list = new ArrayList();
foreach (FileInfo info in fileInfos)
{
if(info.Name != file)
{
// HACK: Just skip the protected samples file...
if (info.Name.IndexOf("protected") == -1)
list.Add(info.FullName);
}
}
return (string[])list.ToArray(typeof(string));
}
我承認這是一個非常古老的問題,但這是一個持續存在的問題。 如果您的目標是 .NET 6 或更高版本,我希望您看看我的圖書館Melville.PDF 。
Melville.Pdf 是 PDF 渲染器的 MIT 許可 C# 實現。 我希望這能滿足我一段時間以來的需求。
如果您嘗試從 PDF 文檔中獲取文本,則渲染 + OCR 可能是最困難的方法。 一些 PDF 文件只是圖像對象的薄包裝,但實際上許多文件內部都有文本。 Melville.PDF(還)不進行文本提取,但它可能是從某些文件中獲取文本的更簡單方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.