//Methods
//88888888888888888888888888888888888888888888888888888888888888888888888
/// <summary>
/// Divide the source pdf by QR code seperatorrs into chunks that
/// can be ripped and saved from the source.
/// </summary>
public void FindPdfChunks()
{
GhostscriptVersionInfo Gvi; //Ghostscript info object
GhostscriptRasterizer Rasterizer = null; //Rasterizer
Bitmap pageImg; //Image of one PDF page.
string docType; //Value of document-type property in QR code json.
int qrCodeGap = 0;
//Only process PDFs
if (SrcIsPdf())
{
try
{
//TEST_OUTPUT
//Console.WriteLine("BASE DIRECTORY{0}{1}", AppDomain.CurrentDomain.BaseDirectory + "gsdll64.dll", Environment.NewLine);
//Init GhostScript and Rasterize
Gvi = new GhostscriptVersionInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "gsdll64.dll"));
Rasterizer = new GhostscriptRasterizer();
Rasterizer.Open(SrcPdf.FullName, Gvi, false); //Raserize the source PDF.
totalPages = Rasterizer.PageCount; //Total pages
//Loop from page 1 to page n-1 (No need to read the last page).
//Note: 1 page PDF will automatically be skipped.
for (int i = 1; i < Rasterizer.PageCount; i++)
{
pageImg = (Bitmap)Rasterizer.GetPage(72, 72, i);
docType = getDocTypeStr(QrReader.ReadQr(pageImg));
//Assert that page has a doc type
if (!isBlankOrNullStr(docType))
{
qrCount++; //Increment found qr code count.
//Throw exception if more than one QR code is found
//and QR Gap is less than 1 (Double Feed)
if (qrCount > 1 && qrCodeGap < 1)
{
throw new ApplicationException("File: " + SrcPdf.Name + " has two consecutive QR code pages. "
+ "QR code double feed will result with an empty document.");
}
else
{
qrCodeGap = 0; //Reset Gap//
}
//Console.WriteLine("DOC TYPE: {0}{1}", docType, Environment.NewLine); //TEST OUTPUT
//Create chunk with src page # for first page (The one after QR page) and name (srcFileName_docType_qr#).
initChunk(i + 1, Path.GetFileNameWithoutExtension(SrcPdf.Name) + "_" + docType + "_" + qrCount);
updatePreviousChunk(i - 1); //Update the previous chunks last page.
}
else if(i == 1 && isBlankOrNullStr(docType))
{
//Exit loop if the first page is not a QR code seperator page
Console.WriteLine("SKIPPED: {0}{1}", SrcPdf.Name, Environment.NewLine); //TEST OUTPUT
break;
}
else
{
//Else count as a content page
contentCount++;
//Increment QR gap//
qrCodeGap++;
}
}
//END LOOP
contentCount++; //Loop ends before the last page which is assumed to be content.
//Assert that we have some chuncks
if (Chunks.Count > 0)
{
//Set last page of the last chunk to the last page of the source document.
Chunks[Chunks.Count - 1].lastPage = Rasterizer.PageCount;
}
}
catch (Exception ex)
{
throw new ApplicationException("Unable to complete chunk search. " + ex.Message);
}
finally
{
//Dispose raterizer resource.
if (Rasterizer != null)
{
Rasterizer.Close();
Rasterizer.Dispose();
}
}
}
//End outer if
}