我正在尝试使用itextsharp从pdf中提取所有图像,但似乎无法克服这一个障碍.
System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS)上的错误发生.给出“参数无效”的错误.
我认为它是有效的,当图像是位图,但没有任何其他格式.
我有以下代码 – 对不起长度;
private void Form1_Load(object sender,EventArgs e) { FileStream fs = File.OpenRead(@"reader.pdf"); byte[] data = new byte[fs.Length]; fs.Read(data,(int)fs.Length); List<System.Drawing.Image> ImgList = new List<System.Drawing.Image>(); iTextSharp.text.pdf.RandomAccessFileOrArray RAFObj = null; iTextSharp.text.pdf.PdfReader PDFReaderObj = null; iTextSharp.text.pdf.PdfObject PDFObj = null; iTextSharp.text.pdf.PdfStream PDFStremObj = null; try { RAFObj = new iTextSharp.text.pdf.RandomAccessFileOrArray(data); PDFReaderObj = new iTextSharp.text.pdf.PdfReader(RAFObj,null); for (int i = 0; i <= PDFReaderObj.XrefSize - 1; i++) { PDFObj = PDFReaderObj.GetPdfObject(i); if ((PDFObj != null) && PDFObj.IsStream()) { PDFStremObj = (iTextSharp.text.pdf.PdfStream)PDFObj; iTextSharp.text.pdf.PdfObject subtype = PDFStremObj.Get(iTextSharp.text.pdf.PdfName.SUBTYPE); if ((subtype != null) && subtype.ToString() == iTextSharp.text.pdf.PdfName.IMAGE.ToString()) { byte[] bytes = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw((iTextSharp.text.pdf.PRStream)PDFStremObj); if ((bytes != null)) { try { System.IO.MemoryStream MS = new System.IO.MemoryStream(bytes); MS.Position = 0; System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS); ImgList.Add(ImgPDF); } catch (Exception) { } } } } } PDFReaderObj.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } } //Form1_Load
解决方法
我过去一直使用这个图书馆,没有任何问题.这应该是你正在追求的.