0

I am developing an application which is work as print agent without getting user interaction. In there, I have to consider about below conditions.

  • Download file shouldn't be access for user.
  • file sould be deleted after print it.
  • The download document could be either Image/PDF or word.docx
  • First downloaded file should be print first.

So far I able to complete as follow. - watcher method created to catch up new download file.

public void catchDocuments()
        {
            if (!string.IsNullOrEmpty(Program.docDirectory))
            {
                file_watcher = new FileSystemWatcher();
                file_watcher.Path = Program.docDirectory;
                file_watcher.EnableRaisingEvents = true;
                file_watcher.Created += new FileSystemEventHandler(OnChanged);
            }            
        }

when new file came, it will fire Onchange event and print document.

string extension = Path.GetExtension(args.FullPath);
if (extension.Equals(@".png") || extension.Equals(@".jpeg") || extension.Equals(@".jpg"))
{
    docCtrl.imageToByteArray(nFile);
    docCtrl.printImage();
}
else if (extension.Equals(@".pdf"))
{                     
    docCtrl.PrintPDF(nFile);
}

But My problem is, When another files download before complete print process of downloaded file, Application will not work properly.

I used print option as follow.

//For Image printing
public void printImage()
{     
    System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
     pd.PrintPage += new PrintPageEventHandler(PrintPage);        
    PrintDialog pdi = new PrintDialog();
    pdi.Document = pd;
    pdi.PrinterSettings.PrinterName;
    pd.Print();    
}

    //For PDF Printing
public void PrintPDF(string path)
    {
       PrintDialog pdf = new PrintDialog();
       Process p = new Process();
       pdf.AllowSelection = true;
       pdf.AllowSomePages = true;
       p.EnableRaisingEvents = true; //Important line of code
       p.StartInfo = new ProcessStartInfo()
         {
           CreateNoWindow = true,
           Verb = "print",
           FileName = path,
         };
       p.Start();
       p.WaitForExit();
       p.Close();
    }

how could I overcome this issue. I'll be really appreciate your good thoughts.

RiksonTool
  • 147
  • 1
  • 2
  • 13
  • "... will not work properly" is rather vague. Generally an event handler should do as little work as possible so that it can be re-armed as soon as possible. In this case, `OnChange` could add the file to a queue of files to be printed and a background thread could process the queue entries. Do you have any control over the application(s) that provide files to be printed, e.g. could you modify them to explicitly group files that should be printed together (`GROUP_00042_FILE_02_OF_09.PDF`)? Or have the application(s) enter the filenames into a `MessageQueue` after the files are created? – HABO Oct 23 '16 at 18:53
  • "Do you have any control over the application(s) that provide files to be printed.?" No, I am not using any control – RiksonTool Oct 24 '16 at 14:58

0 Answers0