您好,我在 Apple M4 Pro 上使用 Aspose.PDF V25.3 .0 使用 Aspose.Pdf.Facades.PdfExtractor 來儲存 PDF 中的影像,儲存的圖檔都是 0 byte ,也沒有任何的錯誤。
註: 已透過 brew 安裝 mono-libgdiplus , 並重新開機過。
brew install mono-libgdiplus
程式如下,
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Drawing;
using System;
var contractPdf = @"c2.pdf";
var dataDir = @"/Users/user1/projects/asposeimage/docs/c2/";
using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
{
var pdfFile = System.IO.Path.Combine(dataDir, contractPdf);
extractor.BindPdf(pdfFile);
// Extract images
extractor.ExtractImage();
// Get all the extracted images
var i = 0;
while (extractor.HasNextImage())
{
// Read image into memory stream
MemoryStream memoryStream = new MemoryStream();
extractor.GetNextImage(memoryStream);
// Write to disk, if you like, or use it otherwise
using (FileStream fileStream = new FileStream(dataDir + (++i).ToString() + ".jpg", FileMode.Create))
{
memoryStream.WriteTo(fileStream);
}
break;
}
}
改透過 XImage.Save 會出現 Unhandled exception. System.NotImplementedException: Not supported image type at Aspose.Pdf.XImage.#=zhzaxamk=(ImageFormat #=zkxOGGrE=, Int32 #=zveucm8I=) at Aspose.Pdf.XImage.Save(Stream stream, ImageFormat format, Int32 resolution) at Aspose.Pdf.XImage.Save(Stream stream, ImageFormat format)
的錯誤。
var contractPdf = @"c2.pdf";
var dataDir = @"/Users/user1/projects/asposeimage/docs/c2/";
var pdfFile = System.IO.Path.Combine(dataDir, contractPdf);
var output = System.IO.Path.Combine(dataDir, "1.jpg");
Aspose.Pdf.Document pdfDocument = new(pdfFile);
var page = pdfDocument.Pages[1];
var images = page.Resources.Images;
if (images.Any())
{
var image = images.First();
using (var imageStream = new FileStream(output, FileMode.Create))
{
var imageInfo = (XImage)image;
imageInfo.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}