我試圖使用以下代碼從PDF中提取圖像。它適用於像DCTDecode這樣的過濾器,但不適用於JPXDEcode。在參數image.GetDrawingImage()被調用時出現「參數無效」錯誤。使用iTextSharp從包含JPXDecode過濾器的PDF中提取圖像時出現「參數無效」錯誤
using System.Drawing.Imaging;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
...
PdfReader pdf = new PdfReader(currfilename);
PdfReaderContentParser parser = new PdfReaderContentParser(pdf);
ImageRender listener = new ImageRender();
for (int i = 1; i <= pdf.NumberOfPages; i++)
{
try
{
parser.ProcessContent(i, listener);//calls RenderImage() at this point
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public void RenderImage(ImageRenderInfo renderInfo)
{
PdfImageObject image = renderInfo.GetImage();
PdfName filter = image.Get(PdfName.FILTER) as PdfName;
if (renderInfo.GetRef() != null && image != null)
{
using (System.Drawing.Image dotnetImg = image.GetDrawingImage())//exception occurs at this point
{
if (dotnetImg != null)
{
ImageNames.Add(string.Format("{0}.tiff", renderInfo.GetRef().Number));
using (MemoryStream ms = new MemoryStream())
{
dotnetImg.Save(ms, ImageFormat.Tiff);
Images.Add(ms.ToArray());
}
}
}
}
}
我試圖這些鏈接的解決方案
Extract images using iTextSharp
Extract Image from a particular page in PDF
,並能提取使用PdfReader.GetStreamBytesRaw()函數的原始圖像字節,但是 「參數無效」異常總是發生在System.Drawing.Image.FromStream(內存流)被調用的地方。
我也檢查過這個鏈接"Parameter is not valid" exception from System.Drawing.Image.FromStream() method,但找不到任何有用的東西。
請幫
說明謝謝。我將通過這些 – Sree