0

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.

MindSwipe
  • 7,193
  • 24
  • 47
  • Windows Services can't interact with the user, so can't show a print dialog, for instance. – Diado Nov 29 '19 at 12:03
  • Possible duplicate of [Printing from a Windows Service](https://stackoverflow.com/questions/4945177/printing-from-a-windows-service) – Diado Nov 29 '19 at 12:05
  • What is oDL ? Maybe its not re-throwing exceptions ! – m5c Nov 29 '19 at 12:07

0 Answers0