c# PDF to Bmp for free

本文关键字:for free Bmp to PDF | 更新日期: 2023-09-27 18:21:39

我正在编写一个程序,该程序使用OCR(tessnet2)扫描图像文件并提取某些信息。在我发现要从Exchange服务器扫描PDF的附件之前,这很容易。

我正在处理的第一个问题是如何将PDF转换为BMP文件。据我所知,到目前为止,TessNet2只能读取图像文件,特别是BMP。所以我现在的任务是将不确定大小的PDF(2-15页)转换为BMP图像。完成后,我可以使用我已经用TessNet2构建的代码轻松地扫描每个图像。

我看到过使用Ghostscript执行这项任务的情况——我只是想知道是否有另一个免费的解决方案,或者你们中的一个优秀的人是否可以给我一个关于如何使用Ghostscript执行这项工作的速成课程。

c# PDF to Bmp for free

您也可以使用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));
    }

找到一篇关于将PDF转换为图像的CodeProject文章:

http://www.codeproject.com/Articles/57100/Simple-and-Free-PDF-to-Image-Conversion

我知道这是一个非常古老的问题,但它是一个持续的问题。如果你的目标是.NET 6或更高版本,我希望你能看看我的库Melville.PDF.

Pdf是麻省理工学院授权的Pdf渲染器的C#实现。我希望这能满足我一段时间以来的需求。

如果你想从PDF文档中提取文本,那么渲染+OCR可能是最困难的方法。有些PDF文件只是图像对象的一个薄薄的包装,但许多文件实际上都包含文本。Melville.PDF还不进行文本提取,但它可能是从某些文件中提取文本的一种更简单的方法。