public string[] GetPrintjobPreview(PrintJobInfo fi)
 {
     string[] lines;
     lines = new string[1000];
     lines.Initialize();
     lock (GlobalDataStore.LockClass)
     {
         if (File.Exists(fi.FullFilename))
         {
             using (System.IO.StreamReader sr = File.OpenText(fi.FullFilename))
             {
                 string input;
                 int    tel = 0;
                 while ((input = sr.ReadLine()) != null)
                 {
                     lines[tel] = input;
                     tel++;
                     if (tel > 999)
                     {
                         break;
                     }
                 }
                 sr.Close();
                 sr.Dispose();
             }
         }
         else
         {
             GlobalDataStore.Logger.Warning("File " + fi.FullFilename + " not found when trying to get a printjob preview");
         }
     }
     return(lines);
 }
        public bool DiscardPrintJob(PrintJobInfo fi)
        {
            int  teller = 0;
            bool bRet   = false;

            while ((teller < 5) && (bRet == false))
            {
                //
                //It can be another tread is reading this file.
                //Wait sometime until they release the file.
                try
                {
                    File.Delete(fi.FullFilename);
                    bRet = true;
                }
                catch (Exception e)
                {
                    GlobalDataStore.Logger.Error("Unable to discard printjob file: " + e.Message);
                }
                teller++;
                if (bRet == false)
                {
                    System.Threading.Thread.Sleep(2000);
                }
            }
            return(bRet);
        }
Beispiel #3
0
        public void SetUp()
        {
            _queue = new Queue();
            _queue.Initialize();

            CreateTestPages(1);

            _printJob     = _queue.NextJob;
            _printJobInfo = _printJob.PrintJobInfo;
        }
        public bool DiscardPrintJob(PrintJobInfo fi)
        {
            bool bRet = false;

            try
            {
                bRet = RemoteObject.DiscardPrintJob(fi);
            }
            catch (Exception ex)
            {
                throw new RemClientControlObjectProxyException("Call to printer service failed.", ex);
            }
            return(bRet);
        }
 public string[] GetPrintjobPreview(PrintJobInfo fi)
 {
     string[] ret;
     ret = null;
     try
     {
         ret = RemoteObject.GetPrintjobPreview(fi);
     }
     catch (Exception ex)
     {
         throw new RemClientControlObjectProxyException("Call to printer service failed.", ex);
     }
     return(ret);
 }
        public bool UpdatePrintjobStatus(PrintJobInfo fi)
        {
            GlobalDataStore.Logger.Debug("RemClientControlObject.UpdatePrintjobStatus");
            //Try to update the printjob xml... maybe it has already been printed...
            bool        bRet;
            XmlDocument theDoc;

            theDoc = new XmlDocument();
            bRet   = true;
            lock (GlobalDataStore.LockClass)
            {
                if (File.Exists(fi.FullFilename))
                {
                    try
                    {
                        theDoc.Load(fi.FullFilename);
                        XmlNode node = theDoc.SelectSingleNode("/printjob/autorelease");
                        if (node != null)
                        {
                            node.InnerText = fi.AutoRelease ? "true" : "false";
                        }
                        else
                        {
                            XmlNode node2 = theDoc.SelectSingleNode("/printjob");
                            if (node2 != null)
                            {
                                XmlElement el = theDoc.CreateElement("autorelease");
                                el.InnerText = fi.AutoRelease ? "true" : "false";
                                node2.AppendChild(el);
                            }
                        }
                        theDoc.Save(fi.FullFilename);
                    }
                    catch (Exception)
                    {
                        bRet = false;
                    }
                }
                else
                {
                    bRet = false;
                }
            }
            return(bRet);
        }
        private bool MarkPrintJobReady(PrintJobInfo jobinfo, string printedToName)
        {
            bool ret = true;

            lock (GlobalDataStore.LockClass)
            {
                string      path   = jobinfo.FullFilename;
                XmlDocument theDoc = new XmlDocument();
                if (!File.Exists(path))
                {
                    return(false);
                }
                theDoc.Load(path);

                XmlNode printedto = theDoc.SelectSingleNode("/printjob/destination/printedto");
                if (printedto == null)
                {
                    XmlNode    destinationnode = theDoc.SelectSingleNode("/printjob/destination");
                    XmlElement NewPrintedTo    = theDoc.CreateElement("printedto");
                    NewPrintedTo.InnerText = printedToName;
                    destinationnode.AppendChild(NewPrintedTo);
                }
                else
                {
                    printedto.InnerText = printedToName;
                }

                XmlNode lastprinted = theDoc.SelectSingleNode("/printjob/destination/lastprinted");
                if (lastprinted == null)
                {
                    XmlNode    lastprintednode = theDoc.SelectSingleNode("/printjob/destination");
                    XmlElement NewLastPrinted  = theDoc.CreateElement("lastprinted");
                    NewLastPrinted.InnerText = System.DateTime.Now.ToString("s");
                    lastprintednode.AppendChild(NewLastPrinted);
                }
                else
                {
                    lastprinted.InnerText = System.DateTime.Now.ToString("s");
                }

                XmlNode autorelease = theDoc.SelectSingleNode("/printjob/autorelease");
                autorelease.InnerText = "false";


                int  counter        = 0;
                bool saveSuccessful = false;
                while ((counter < 5) && (saveSuccessful == false))
                {
                    try
                    {
                        theDoc.Save(path);
                        Thread.Sleep(2000);
                        saveSuccessful = true;
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(2000);
                        counter += 1;
                        GlobalDataStore.Logger.Warning(string.Format("The file {0} could not be saved: {1}", theDoc.Name, ex.Message));
                    }
                }
                if (!saveSuccessful)
                {
                    ret = false;
                }
            }
            return(ret);
        }
        private bool HandlePrintJob(PrintGroupItem it, PrintJobInfo jobinfo, string PrintJobsRootFolder, string PaperDefinitionsRootFolder, string LabelDefinitionsRootFolder, int language)
        {
            bool   ret = true;
            string sXMLFile;

            sXMLFile = jobinfo.FullFilename;

            //Retrieve some information for the selected printjob
            // The requested queueu, the printgroup (should be the current), and the LabelType
            string sPrintQueue;
            string sPrintGroup;
            string sLabelType;

            sPrintGroup = string.Empty;
            sPrintQueue = string.Empty;
            sLabelType  = string.Empty;
            try
            {
                XPathDocument doc;
                doc = new XPathDocument(sXMLFile);

                XPathNavigator nav;
                nav = doc.CreateNavigator();

                XPathNodeIterator theNode;
                theNode = nav.Select("/printjob/destination/printqueue");

                if (theNode != null)
                {
                    theNode.MoveNext();
                    sPrintQueue = theNode.Current.Value;
                }
                theNode = nav.Select("/printjob/destination/printgroup");

                if (theNode != null)
                {
                    theNode.MoveNext();
                    sPrintGroup = theNode.Current.Value;
                }

                theNode = nav.Select("/printjob/destination/labeltype");

                if (theNode != null)
                {
                    theNode.MoveNext();
                    sLabelType = theNode.Current.Value;
                }
            }
            catch (Exception e)
            {
                GlobalDataStore.Logger.Warning(string.Format("The following error occured while handeling file {0}:", jobinfo.FullFilename)
                                               + Environment.NewLine + e.Message);
                return(false);
            }
            //We now have the labeltype requested... we gather information on which paper this can be printed...
            List <string> papertypes;

            papertypes = new List <string>();

            try
            {
                papertypes = GetPaperTypesOfLabelType(sLabelType);
            }
            catch (System.IO.FileNotFoundException)
            {
                //dit zou alleen mogen gebeuren als het labeltype niet bestaat als xmlbestand...
                GlobalDataStore.Logger.Warning(string.Format("Label {0} not defined. Job on hold.", sLabelType));

                if (papertypes.Count > 0)
                {
                    papertypes.Clear();
                }
                return(false);
            }

            try
            {
                if (papertypes.Count == 0)
                {
                    //can not print on no paper...
                    GlobalDataStore.Logger.Warning(string.Format("Label {0} has no defined paper types. Job on hold.", sLabelType));
                    return(false);
                }
                else
                {
                    if (papertypes.Count > 1)
                    {
                        //only one default allowed for now.
                        GlobalDataStore.Logger.Warning(string.Format("Label {0} has more than one default paper type. Job on hold.", sLabelType));
                        return(false);
                    }
                    //check if papertype exists as an xml file
                    string paperFileName = PaperDefinitionsRootFolder + papertypes[0] + ".xml";
                    if (!File.Exists(paperFileName))
                    {
                        GlobalDataStore.Logger.Warning(string.Format("Papertype {0} has no xml file. Job on hold.", papertypes[0]));
                        return(false);
                    }
                }
                PrinterItemLocals pils;
                pils = new PrinterItemLocals();
                foreach (PrinterItem pit in it.GroupPrinters)
                {
                    PrinterItemLocal pil = new PrinterItemLocal();
                    pil.item = pit;
                    pils.Add(pil);
                    //
                    //Gather windowsinformation for each printer in the windows printer queue
                    // We gather the numberofjobs and if the printer is online.
                    //
                }
                PrinterItemLocal bestFit;
                bestFit = null;
                foreach (PrinterItemLocal pil in pils)
                {
                    //we do not print to printers that are physically offline
                    if (!pil.item.Online)
                    {
                        continue;
                    }

                    //we do not print to printers that are marked disabled.
                    if (!pil.item.Enabled)
                    {
                        continue;
                    }

                    if (pil.item.Trays != null)
                    {
                        foreach (PrinterTrayItem tit in pil.item.Trays)
                        {
                            if (tit.CurrentPapertypeName.Equals(papertypes[0], StringComparison.OrdinalIgnoreCase) || pil.item.LongName.Contains("Microsoft XPS Document Writer"))
                            {
                                //papiersoort klopt..
                                if (bestFit == null)
                                {
                                    pil.Tray = tit;
                                    bestFit  = pil;
                                }
                                else
                                {
                                    if (pil.item.QueueLength < bestFit.item.QueueLength)
                                    {
                                        pil.Tray = tit;
                                        bestFit  = pil;
                                    }
                                }
                            }
                        }
                    }
                }
                //We have selected a printer to print to
                if (bestFit != null)
                {
                    ACA.LabelX.PrintJob.PrintJob p = new ACA.LabelX.PrintJob.PrintJob(PaperDefinitionsRootFolder, LabelDefinitionsRootFolder);
                    p.Parse(jobinfo.FullFilename);

                    //
                    if (p.labels.Count == 0)
                    {
                        //Oeps.. a printjob with a default label, but no actual label is detected
                        //Mark this job as handled now, without error
                        ret = MarkPrintJobReady(jobinfo, "No Label in job.");
                        return(ret);
                    }

                    //Check if all pictures are available
                    ACA.LabelX.Label.LabelSet labelset = new ACA.LabelX.Label.LabelSet();
                    labelset.CurrentLabel    = p.labels[0];
                    labelset.DefaultLabel    = p.Defaultlabel;
                    labelset.BaseLabel       = p.LabelDef.DefaultLabel;
                    labelset.StaticVarsLabel = p.StaticVarslabel;

                    if (!p.LabelDef.CheckAllPicturesAvailable(labelset, language, true))
                    {
                        GlobalDataStore.Logger.Warning(string.Format("Missing picture to print. Job on hold.", sLabelType));
                        return(false);
                    }
                    //
                    //check if th language requested is pressent in the printjob
                    //
                    bool bFound = false;
                    if (p.languages != null)
                    {
                        if (p.languages.Count > 0)
                        {
                            foreach (PrintJob.PrintJob.PrintLanguage x in p.languages)
                            {
                                if (x.Id == language)
                                {
                                    bFound = true;
                                }
                            }
                        }
                    }

                    //We have selected a language this job can be printed in...
                    if (bFound)
                    {
                        ACA.LabelX.PrintEngine.PrintEngine pi = new ACA.LabelX.PrintEngine.PrintEngine(Environment.MachineName);
                        pi.DesignMode = false;
                        pi.AddPrintJob(p);
                        GlobalDataStore.Logger.Info("Printing " + p.ID.ToString());
                        pi.Print(p.ID.ToString(), bestFit.item.LongName, bestFit.Tray.TrayName, papertypes[0], 0, uint.MaxValue, language);
                        bestFit.item.QueueLength++;     //There is now one job more in the queue..


                        ret = MarkPrintJobReady(jobinfo, bestFit.item.LongName);
                        //Voeg printernaam en datum toe aan printjob xml file
                        //mve, 2016-03-06

                        /*
                         * lock (GlobalDataStore.LockClass)
                         * {
                         *  string path = jobinfo.FullFilename;
                         *  XmlDocument theDoc = new XmlDocument();
                         *  if (!File.Exists(path))
                         *  {
                         *      return false;
                         *  }
                         *  theDoc.Load(path);
                         *
                         *  XmlNode printedto = theDoc.SelectSingleNode("/printjob/destination/printedto");
                         *  if (printedto == null)
                         *  {
                         *      XmlNode destinationnode = theDoc.SelectSingleNode("/printjob/destination");
                         *      XmlElement NewPrintedTo = theDoc.CreateElement("printedto");
                         *      NewPrintedTo.InnerText = bestFit.item.LongName;
                         *      destinationnode.AppendChild(NewPrintedTo);
                         *  }
                         *  else
                         *  {
                         *      printedto.InnerText = bestFit.item.LongName;
                         *  }
                         *
                         *  XmlNode lastprinted = theDoc.SelectSingleNode("/printjob/destination/lastprinted");
                         *  if (lastprinted == null)
                         *  {
                         *      XmlNode lastprintednode = theDoc.SelectSingleNode("/printjob/destination");
                         *      XmlElement NewLastPrinted = theDoc.CreateElement("lastprinted");
                         *      NewLastPrinted.InnerText = System.DateTime.Now.ToString("s");
                         *      lastprintednode.AppendChild(NewLastPrinted);
                         *  }
                         *  else
                         *  {
                         *      lastprinted.InnerText = System.DateTime.Now.ToString("s");
                         *  }
                         *
                         *  XmlNode autorelease = theDoc.SelectSingleNode("/printjob/autorelease");
                         *  autorelease.InnerText = "false";
                         *
                         *
                         *  int counter = 0;
                         *  bool saveSuccessful = false;
                         *  while ((counter < 5) && (saveSuccessful == false))
                         *  {
                         *      try
                         *      {
                         *          theDoc.Save(path);
                         *          Thread.Sleep(2000);
                         *          saveSuccessful = true;
                         *      }
                         *      catch (Exception ex)
                         *      {
                         *          Thread.Sleep(2000);
                         *          counter += 1;
                         *          GlobalDataStore.Logger.Warning(string.Format("The file {0} could not be saved: {1}", theDoc.Name, ex.Message));
                         *      }
                         *  }
                         *  if (!saveSuccessful)
                         *      ret = false;
                         *
                         * }
                         */
                    }
                    else
                    {
                        GlobalDataStore.Logger.Warning(String.Format("Language of printjob {0} not suitable for this location.", p.ID.ToString()));
                        ret = false;
                    }
                }
                else
                {
                    GlobalDataStore.Logger.Warning(String.Format("No suitable printer found for file {0}.", jobinfo.FullFilename));
                    ret = false;
                }
            }
            catch (Exception egen)
            {
                GlobalDataStore.Logger.Error(string.Format("Unexpected Error in HandlePrintJob: {0}.", egen.Message));
                GlobalDataStore.Logger.Error(string.Format("Extra info: {0}.", egen.StackTrace));
                if (egen.InnerException != null)
                {
                    GlobalDataStore.Logger.Error(string.Format("Extra info: {0}.", egen.InnerException.Message));
                }
                return(false);
            }

            return(ret);
        }