|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using CBR.Core.Helpers;
namespace CBR.Core.Files.Conversion
{
internal class PDFImageReader : IReaderContract
{
public bool Read(string inputFileorFolder, string outputFolder, List<byte[]> imageBytes, List<string> imageNames, ContractParameters settings, ProgressDelegate progress)
{
PdfReader reader = null;
PDFImageListener listener = null;
try
{
reader = new PdfReader(inputFileorFolder);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
listener = new PDFImageListener();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
parser.ProcessContent(i, listener);
}
if (settings.CheckResult && reader.NumberOfPages != listener.ImageNames.Count)
{
progress(string.Format("Error extracting {0} : {1} images for {2} pages !!", inputFileorFolder, listener.ImageNames.Count, reader.NumberOfPages));
throw new Exception("PDF check error");
}
else
progress(string.Format("Extracting {0} images in {1} pages", listener.ImageNames.Count, reader.NumberOfPages));
imageBytes.AddRange( listener.ImageBytes );
imageNames.AddRange( listener.ImageNames );
progress(string.Format("{0} images extracted...", listener.ImageBytes.Count));
}
catch (Exception err)
{
ExceptionHelper.Manage("PDFImageReader:Read", err);
settings.Result = false;
listener.ImageNames.Clear();
listener.ImageBytes.Clear();
return false;
}
finally
{
if (reader != null)
reader.Close();
}
return true;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside
https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/