это C# проект winforms
Учитывая файловую папку, нужно открыть любой файл PDF и для каждого файла, оценить каждый объект. Если объект является изображением, извлеките его, преобразуйте и сохраните как файл JPEG в этой же папке.
private void btnExtractImages_Click(object sender, EventArgs e)
{
try
{
DirectoryInfo di = new DirectoryInfo(txtSourcePdf.Text);
foreach (FileInfo fi in di.GetFiles())
{
PdfDocument doc = new PdfDocument(new PdfReader(fi.FullName));
string fNm = fi.Name.Replace(".pdf", ""); //Filename without the extension
int pgs = doc.GetNumberOfPages();
for (int pg = 1; pg <= pgs; pg++)
{
PdfPage pdfPage = doc.GetPage(pg);
PdfFormXObject xObj = pdfPage.CopyAsFormXObject(doc);
iText.Layout.Element.Image img = new iText.Layout.Element.Image(xObj);
//good so far.
//need to turn this xObj or img into a jpg and save to disk
...
}
// This works, however it creates a .dat file, not a .jpg file
// int objs = doc.GetNumberOfPdfObjects();
// for (int o = 1; o <= objs; o++)
// {
// PdfObject obj = doc.GetPdfObject(o);
// if (obj != null && obj.IsStream())
// {
// byte[] b;
// try
// {
// // Get decoded stream bytes.
// b = ((PdfStream)obj).GetBytes();
// }
// catch (Exception)
// {
// //Get originally encoded stream bytes
// b = ((PdfStream)obj).GetBytes(false);
// }
// using (FileStream fos = new FileStream(String.Format(txtSourcePdf.Text + "/" + fNm + "{0}.jpg", o), FileMode.Create))
// {
// fos.Write(b, 0, b.Length);
// }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}