我需要为pdf的页面提取一个图像,我有一个从stackoverflow中的另一个问题中提取的代码从pdf中提取图像,有时候Works完美,但有时候不按照顺序提取图像我希望(第一页它将是第一个图像),第一个图像对应于pdf文件的第一页,这是代码:
private static int WriteImageFile(string pdf, string path)
{
int nfotos = 0;
try
{
// Get a List of Image
List<System.Drawing.Image> ListImage = ExtractImages(pdf);
nfotos = ListImage.Count;
for (int i = 0; i < ListImage.Count; i++)
{
try
{
ListImage[i].Save(path+ "\\Image" + i + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
catch (Exception e)
{ MessageBox.Show(e.Message); }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return nfotos;
}
private static List<System.Drawing.Image> ExtractImages(String PDFSourcePath)
{
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(PDFSourcePath);
PDFReaderObj = new iTextSharp.text.pdf.PdfReader(RAFObj, null);
Form1 formulario = new Form1();
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())
{
try
{
iTextSharp.text.pdf.parser.PdfImageObject PdfImageObj =
new iTextSharp.text.pdf.parser.PdfImageObject((iTextSharp.text.pdf.PRStream)PDFStremObj);
System.Drawing.Image ImgPDF = PdfImageObj.GetDrawingImage();
ImgList.Add(ImgPDF);
}
catch (Exception)
{
}
}
}
}
PDFReaderObj.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return ImgList;
}
我做错了什么?或者是否有任何方法可以知道将图像与pdf页面关联的处理页面?