Created
January 26, 2011 04:14
-
-
Save 7shi/796217 to your computer and use it in GitHub Desktop.
extract JPEG from PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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