Make sure that SearchResults.pdf donot cause a failure.

Calculate Table Location
This commit is contained in:
Rich
2010-10-12 15:01:54 +00:00
parent e278a2ca37
commit c6d2003414
2 changed files with 85 additions and 20 deletions

View File

@@ -195,23 +195,10 @@ namespace Volian.Print.Library
public void Build()
{
iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 36, 36, 36, 36);
if (!CreateResultsPDF(document)) return;
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_FileName, FileMode.Create));
// we add some meta information to the document
//document.AddAuthor("Gerald Henson");
//document.AddSubject("This is the result of a Test.");
document.SetMargins(36, 36, 36, 36);
// step 3: we open the document
document.Open();
BuildTable1(document);
//AddTable(document, 10, 90);
//AddTable(document, 10, 100);
//AddTable(document, 15, 75);
//AddTable(document, 20, 70);
//AddTable(document, 25, 65);
//AddTable(document, 30, 60);
}
catch (Exception ex)
{
@@ -226,10 +213,72 @@ namespace Volian.Print.Library
}
finally
{
document.Close();
if (document.IsOpen())
{
document.Close();
System.Diagnostics.Process.Start(_FileName);
}
}
}
/// <summary>
/// Attempt to open a file for the PDF output.
/// If the file cannot be opened because it is in use, try adding a suffix
/// If the file cannot be opened for some other reason, display an error message
/// </summary>
/// <param name="document"></param>
/// <returns></returns>
private bool CreateResultsPDF(iTextSharp.text.Document document)
{
bool result = false;
string suffix = "";
int i = 0;
// Try to open a file for creating the PDF.
while (result == false)
{
string fileName = _FileName.ToLower().Replace(".pdf", suffix + ".pdf");
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.SetMargins(36, 36, 36, 36);
document.Open();
_FileName = fileName;
result = true;
}
catch (System.IO.IOException exIO)
{
if (exIO.Message.Contains("because it is being used by another process"))
suffix = string.Format("_{0}", ++i);// If this file is in use, increment the suffix and try again
else // If some other error, display a message and don't print the results
{
ShowException(exIO);
return false;
}
}
catch (Exception ex)
{
ShowException(ex);
return false; // Could not open the output file
}
}
return true;
}
private static void ShowException(Exception ex)
{
Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message);
StringBuilder msg = new StringBuilder();
string sep = "";
string indent = "";
while (ex != null)
{
msg.Append(string.Format("{0}{1}{2}:\r\n{1}{3}", sep, indent, ex.GetType().Name, ex.Message));
ex = ex.InnerException;
sep = "\r\n";
indent += " ";
}
System.Windows.Forms.MessageBox.Show(msg.ToString(), "Error during PDF creation for search:", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
}
/// <summary>
/// Use Cells within Cells to see if I can limit the way Cells break from Page to Page
/// </summary>
/// <param name="document"></param>