Here is the code i use to print in my windows form but doesn't work in Service.
public void printPDFWithAcrobat(cReport o)
{
string Filepath = @"C:\PDF\BatchNo_" + o.tr_BatchNumber + "_ReportNo_" + o.tr_Number +
".pdf";
using (PrintDialog Dialog = new PrintDialog())
{
try
{
ProcessStartInfo printProcessInfo = new ProcessStartInfo()
{
Verb = "print",
CreateNoWindow = true,
FileName = Filepath,
WindowStyle = ProcessWindowStyle.Hidden
};
Process printProcess = new Process();
printProcess.StartInfo = printProcessInfo;
printProcess.Start();
printProcess.WaitForInputIdle();
Thread.Sleep(5000);
WriteToFile("Printed" + DateTime.Now);
}
catch (Exception ex)
{
throw ex;
}
}
}
I've been trying to catch exceptions and write them to a file but it is not giving any exceptions. The code below is where i am calling the print method. The writing to file where it says "Stepping into print" is the last thing that is written to the file.
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
cReport o = new cReport();
Int32 recordID = Int32.Parse(dr["tr_RecordID"].ToString());
o = oDL.get_Report(recordID);
oDL.SaveReport(o);
o.tr_RecordID = Convert.ToInt32(recordID);
o.tr_Print = 1;
oDL.save(o);
oDL.WriteToFile("Stepping into print.");
try
{
oDL.printPDFWithAcrobat(o);
}
catch(Exception ex)
{
oDL.WriteToFile("Printing Error: " + ex.ToString() + "");
}
oDL.WriteToFile("Didnt make it past print.");
}
}
All the above runs perfectly in a windows form application.