Skip to content

Instantly share code, notes, and snippets.

@7shi
Created January 26, 2011 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/796217 to your computer and use it in GitHub Desktop.
Save 7shi/796217 to your computer and use it in GitHub Desktop.
extract JPEG from PDF
private void ExtractJPEG(string pdf)
{
var dir1 = Path.GetDirectoryName(pdf);
var fn = Path.GetFileNameWithoutExtension(pdf);
var dir2 = Path.Combine(dir1, fn);
if (!Directory.Exists(dir2)) Directory.CreateDirectory(dir2);
using (var doc = new PdfDocument(pdf))
{
int n = doc.PageCount;
for (int i = 1; i <= n; i++)
{
var page = doc.GetPage(i);
var rsrc = page.GetObject("/Resources");
if (rsrc == null) continue;
var xobj = rsrc.GetObject("/XObject");
if (xobj == null) continue;
foreach (var key in xobj.Keys)
{
var obj = xobj.GetObject(key);
if (obj != null
&& obj.GetText("/Subtype") == "/Image"
&& obj.GetText("/Filter") == "/DCTDecode")
{
File.WriteAllBytes(
Path.Combine(dir2, string.Format("{0:0000}.jpg", i)),
obj.GetStreamBytes());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment