B2019-041: The merging of pdfs crashes when there is a bad (corrupt) procedure pdf to merge

This commit is contained in:
Kathy Ruffing 2019-03-22 13:15:43 +00:00
parent 3aa1834cca
commit 17ae2c3df7

View File

@ -42,6 +42,7 @@ namespace Volian.Print.Library
// selects the 'Merge' button off of the print dialog when doing a Print All. (C2019-012) // selects the 'Merge' button off of the print dialog when doing a Print All. (C2019-012)
public class MergedPdf public class MergedPdf
{ {
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private List<MergedPdfProc> _mergedPdfs = null; // This list has all procedure pdfs that will be included in the merged file. private List<MergedPdfProc> _mergedPdfs = null; // This list has all procedure pdfs that will be included in the merged file.
public List<MergedPdfProc> MergedPdfs public List<MergedPdfProc> MergedPdfs
{ {
@ -116,31 +117,40 @@ namespace Volian.Print.Library
FlexibleMessageBox.Show("Error in finding pdf files to merge. Cannot print with date/time as part of pdf name. Or check that individual pdfs were generated.", "Error on CreatePdf", MessageBoxButtons.OK, MessageBoxIcon.Warning); FlexibleMessageBox.Show("Error in finding pdf files to merge. Cannot print with date/time as part of pdf name. Or check that individual pdfs were generated.", "Error on CreatePdf", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false; return false;
} }
PdfReader reader = new PdfReader(_folder + @"\" + mpp.PdfFileName); PdfReader reader = null;
int numPages = reader.NumberOfPages; try // B2019-041: added a try/catch for when a corrupt pdf was created for a procedure, just skip & add message to error log.
int currentPageNumber = 0;
PdfOutline outline = null;
do // merging pages into complete...
{ {
currentPageNumber += 1; reader = new PdfReader(_folder + @"\" + mpp.PdfFileName);
mergedPageNumber += 1; int numPages = reader.NumberOfPages;
doc.SetPageSize(PageSize.LETTER); int currentPageNumber = 0;
doc.NewPage(); PdfOutline outline = null;
PdfImportedPage page = writer.GetImportedPage(reader, currentPageNumber); // gets a page that is 'ready' to be written to combined pdf do // merging pages into complete...
if (doPageNum) // get the string & fill in with <page> and <of> numbers
{ {
string outputpageof = _pageof.Replace("<page>", mergedPageNumber.ToString()).Replace("<of>", totalPages.ToString()); currentPageNumber += 1;
AddPageNumberToPage(page, canvas, outputpageof); mergedPageNumber += 1;
} doc.SetPageSize(PageSize.LETTER);
PdfDestination dest = new PdfDestination(PdfDestination.FIT); doc.NewPage();
// if on the first page, add the pdfname & title as part of outline. If on remaining pages, just put out page number. PdfImportedPage page = writer.GetImportedPage(reader, currentPageNumber); // gets a page that is 'ready' to be written to combined pdf
// Later this may need expanded to put sections/steps/etc. if (doPageNum) // get the string & fill in with <page> and <of> numbers
if (currentPageNumber == 1) {
outline = new PdfOutline(canvas.RootOutline, dest, mpp.PdfFileName.Replace(".pdf", "")+" - "+mpp.Title, false); string outputpageof = _pageof.Replace("<page>", mergedPageNumber.ToString()).Replace("<of>", totalPages.ToString());
else AddPageNumberToPage(page, canvas, outputpageof);
new PdfOutline(outline, dest, "Page " + currentPageNumber.ToString(), false); }
canvas.AddTemplate(page, 0, 0); // adds the page to the combined pdf PdfDestination dest = new PdfDestination(PdfDestination.FIT);
} while (currentPageNumber < numPages); // if on the first page, add the pdfname & title as part of outline. If on remaining pages, just put out page number.
// Later this may need expanded to put sections/steps/etc.
if (currentPageNumber == 1)
outline = new PdfOutline(canvas.RootOutline, dest, mpp.PdfFileName.Replace(".pdf", "") + " - " + mpp.Title, false);
else
new PdfOutline(outline, dest, "Page " + currentPageNumber.ToString(), false);
canvas.AddTemplate(page, 0, 0); // adds the page to the combined pdf
} while (currentPageNumber < numPages);
}
catch (Exception ex)
{
string tmp = string.Format("Error merging pdf {0}", mpp.PdfFileName);
_MyLog.Error(tmp, ex);
}
} }
doc.Close(); doc.Close();
return true; return true;