Aspose.pdf PDF To jpg

感谢您的查看和帮助!
1.业务:PDF转JPG图片,然后进行OCR识别,服务器返回双层PDF文件。
2.当我使用Aspose.pdf.dll 22.11.0.jpeg图片,OCR返回的双层PDF是正常的,可以正常打开。
3.当我使用Aspose.pdf via Drawing.dll 25.4.0.jpeg图片,OCR返回的双层PDF是错误的,文件损坏。
当我对比:Aspose.pdf.dll 22.11.0.jpeg和Aspose.pdf via Drawing.dll 25.4.0.jpeg,发现Aspose.pdf via Drawing.dll 25.4.0.jpeg图片比Aspose.pdf.dll 22.11.0.jpeg多了,分辨率单位:2、颜色表示:sRGB,如何让新的API通过设置参数,实现旧的API输出图片,保留我之前的正确业务。

Aspose.pdf.dll 22.11.0.jpeg (21.8 KB)
Aspose.pdf via Drawing.dll 25.4.0.jpeg (22.6 KB)

对比:
360截图20250902031939.png (31.6 KB)

旧代码架构:C#+.net 4.72+Aspose.pdf.dll 22.11.0 输出:Aspose.pdf.dll 22.11.0.jpeg
新代码架构:C#+.net8++Aspose.pdf via Drawing.dll 25.4.0 输出:Aspose.pdf via Drawing.dll 25.4.0.jpeg

如下代码:

// 使用 Aspose.PDF 将 PDF 转换为图片
public static List ConvertPdfToImages(string pdfPath, string outputDirectory, int dpi = 300, ImageType format = ImageType.Png, CancellationToken cancellationToken = default)
{
var imagePaths = new List();

 // 确保输出目录存在
 Directory.CreateDirectory(outputDirectory);

 // 初始化许可证(实际项目中应从安全位置加载)
 // new Aspose.Pdf.License().SetLicense("Aspose.Total.lic");

 using (var document = new Aspose.Pdf.Document(pdfPath))
 {
     // 创建分辨率对象
     var resolution = new Aspose.Pdf.Devices.Resolution(dpi);

     // 处理每一页
     for (int i = 1; i <= document.Pages.Count; i++)
     {
         string imagePath = System.IO.Path.Combine(outputDirectory, $"{i}_page_{System.IO.Path.GetFileNameWithoutExtension(pdfPath)}.{format.ToString().ToLower()}");

         using (var stream = new FileStream(imagePath, FileMode.Create))
         {
             // 根据格式处理不同设备类型
             switch (format)
             {
                 case ImageType.Jpeg:
                     var jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution, 85);
                     jpegDevice.Process(document.Pages[i], stream);
                     break;
                 case ImageType.Tiff:
                     var tiffDevice = new Aspose.Pdf.Devices.TiffDevice(resolution);
                     tiffDevice.Process(document.Pages[i], stream);
                     break;
                 case ImageType.Bmp:
                     var bmpDevice = new Aspose.Pdf.Devices.BmpDevice(resolution);
                     bmpDevice.Process(document.Pages[i], stream);
                     break;
                 default: // Png
                     var pngDevice = new Aspose.Pdf.Devices.PngDevice(resolution);
                     pngDevice.Process(document.Pages[i], stream);
                     break;
             }
         }

         // 取消检查点
         cancellationToken.ThrowIfCancellationRequested();

         imagePaths.Add(imagePath);
     }
 }

 return imagePaths;

}

@fhn123456

要确保使用新的Aspose.PDF via Drawing.dll API生成的JPEG图像与旧版本的Aspose.PDF.dll API生成的图像相同,您可以按照以下步骤进行操作:

  1. 设置分辨率:确保在创建Resolution对象时使用与旧版本相同的DPI值。例如,如果旧版本使用的是300 DPI,请在新代码中也使用300 DPI。

  2. 调整JPEG压缩质量:在创建JpegDevice时,您可以设置压缩质量。确保使用与旧版本相同的质量设置(例如,85)。

  3. 颜色空间:检查生成的图像的颜色空间设置。确保新API生成的图像使用sRGB颜色空间。

  4. 输出格式:确保输出格式与旧版本一致,您可以通过ImageType参数来指定。

以下是更新后的代码示例,确保与旧版本的输出一致:

public static List<string> ConvertPdfToImages(string pdfPath, string outputDirectory, int dpi = 300, ImageType format = ImageType.Jpeg, CancellationToken cancellationToken = default)
{
    var imagePaths = new List<string>();
    Directory.CreateDirectory(outputDirectory);

    using (var document = new Aspose.Pdf.Document(pdfPath))
    {
        var resolution = new Aspose.Pdf.Devices.Resolution(dpi);

        for (int i = 1; i <= document.Pages.Count; i++)
        {
            string imagePath = System.IO.Path.Combine(outputDirectory, $"{i}_page_{System.IO.Path.GetFileNameWithoutExtension(pdfPath)}.{format.ToString().ToLower()}");

            using (var stream = new FileStream(imagePath, FileMode.Create))
            {
                if (format == ImageType.Jpeg)
                {
                    var jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution, 85); // 确保质量设置为85
                    jpegDevice.Process(document.Pages[i], stream);
                }
                // 其他格式处理...
            }

            cancellationToken.ThrowIfCancellationRequested();
            imagePaths.Add(imagePath);
        }
    }

    return imagePaths;
}

请根据您的需求调整参数。如果您仍然遇到问题,请检查生成的图像的详细信息,确保所有设置都与旧版本一致。希望这能帮助您解决问题!如有其他疑问,请随时联系我以获取进一步的支持。

我需要人工技术帮我提供解决方案,因为你提供的方案,和我的代码相同。

@fhn123456

请尝试使用最新的Aspose.PDF for .NET 25.8版本,并告知我们是否仍然发现相同的差异。同时,请分享您是如何对通过API获取的图像执行OCR操作的?如果可能,能否提供您的OCR工具显示的错误截图或描述?