My code for get all the images from pdf
PdfDocument document = PdfReader.Open(textBox_fichero.Text);
PdfSharp.Pdf.PdfObject[] Todosobjetos = document.Internals.GetAllObjects();
for(int I=0;I<Todosobjetos.Length;I++)
{
if(Todosobjetos[I].GetType().Name == typeof(PdfDictionary).Name)
{
PdfDictionary Referencia = Todosobjetos[I] as PdfDictionary;
string Tipo = Referencia.Elements.GetString("/Type");
if(Tipo== "/XObject")
{
if(Referencia.Elements.GetString("/Subtype")=="/Image")
{
string extension = "";
ExportImage(Referencia, Nombre, ref extension))
Contador_Imagenes++;
}
}
}
}
then extract image. works for jpg and png
pubilc void ExportImage(PdfDictionary image, string nombre_fichero,ref string extension)
{
string filter;
try
{
filter = image.Elements.GetName("/Filter");
}
catch
{
filter = "/DCTDecode";
}
try
{
switch (filter)
{
case "/DCTDecode":
{
extension=".jpg";
ExportJpegImage(image, nombre_fichero);
}
break;
case "/FlateDecode":
{
extension = ".png";
ExportAsPngImage(image, nombre_fichero);
}
break;
}
}
catch
{
Errores ++ ;
}
}
private void ExportJpegImage(PdfDictionary ImagePdfDictionary, string nombre_fichero)
{
byte[] stream = ImagePdfDictionary.Stream.Value;
if (CheckBox_InvertirColores.Checked)
{
using (MagickImage magickImage = new MagickImage(stream))
{
magickImage.Negate();
magickImage.Write(nombre_fichero);
}
}
else
{
FileStream fs = new FileStream(nombre_fichero, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(stream);
bw.Close();
bw.Dispose();
fs.Close();
fs.Dispose();
}
}
private void ExportAsPngImage(PdfDictionary image, string nombre_fichero)
{
int width = image.Elements.GetInteger(PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfImage.Keys.Height);
var canUnfilter = image.Stream.TryUnfilter();
byte[] decodedBytes;
if (canUnfilter)
{
decodedBytes = image.Stream.Value;
}
else
{
PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
decodedBytes = flate.Decode(image.Stream.Value);
}
int bitsPerComponent = 0;
while (decodedBytes.Length - ((width * height) * bitsPerComponent / 8) != 0)
{
bitsPerComponent++;
}
System.Drawing.Imaging.PixelFormat pixelFormat;
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 16:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppArgb1555;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
case 32:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
break;
case 64:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format64bppArgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}
Array.Reverse(decodedBytes);
Bitmap bmp = new Bitmap(width, height, pixelFormat);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(decodedBytes, 0, bmpData.Scan0, decodedBytes.Length);
bmp.UnlockBits(bmpData);
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
bmp.Save(nombre_fichero, System.Drawing.Imaging.ImageFormat.Png);
}