感谢您的查看和帮助!
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;
}