title: Get image from PDF created with iText7 (C#)
body: <p>I have a question. I have this program in C# which creates a PDF using iText7, using exclusively images. (here's an example)</p>
  <pre><code>public void fromJPEGFileToPDF(String[] pathFiles, string path)
          {
              PdfWriter writer = new PdfWriter(path);
              PdfDocument pdfDoc = new PdfDocument(writer);
              Document doc = new Document(pdfDoc);
  
              for (int i = 0; i &lt; pathFiles.Length; i++)
              {try
                  {
                      ImageData imageData = ImageDataFactory.Create(FileToByte(pathFiles[i]));
                      ImagePDF imgTemp = new ImagePDF(imageData);
                      doc.Add(imgTemp);
                  }
                  catch (Exception e)
                  {
                      //DO SOMETHING
                  }
              }
              doc.Close();
          }
  </code></pre>
  <p>After that, I have this other function that extracts back the images from the file PDF, but it doesn't work as I'd want, as I often get images that don't open. To be fair, I get usually the proper image(s) I put in the pdf + some void files of small dimension that don't look like image files at all. And lately I got some pdf with not extractable images at all. Can I ask for your help?
  Here's my code:</p>
  <pre><code>public void fromPDFtoJPEGFiles(string pathFolder, string filename )
          {
              PdfDocument pdfDoc = new PdfDocument(new PdfReader(pathFolder+ &quot;\\&quot; + filename + &quot;.pdf&quot;));
              PdfObject obj;
              List&lt;int&gt; streamLengths = new List&lt;int&gt;();
              for (int i = 1; i &lt;= pdfDoc.GetNumberOfPdfObjects(); i++)
              {
                  obj = pdfDoc.GetPdfObject(i);
                  if (obj != null &amp;&amp; obj.IsStream())
                  {
                      byte[] b;
                      try
                      {
                          b = ((PdfStream)obj).GetBytes();
                      }
                      catch (PdfException exc)
                      {
                          b = ((PdfStream)obj).GetBytes(false);
                      }
                      MemoryStream fos = new MemoryStream(b);
                      FileStream file = new FileStream(pathFolder+ filename + &quot;(&quot; + (i + 1) + &quot;).jpg&quot;, FileMode.Create, System.IO.FileAccess.Write);
                      fos.WriteTo(file);
  
                      streamLengths.Add(b.Length);
                      fos.Close();
                      file.Close();
                  }
              }
              pdfDoc.Close();
          }
  </code></pre>