public void Parse(string FilePath) { ID = ""; coordinateSystem = null; labelLayout = null; XmlDocument xDoc = new XmlDocument(); xDoc.Load(FilePath); XmlNodeList node = xDoc.GetElementsByTagName("paper"); foreach (XmlNode nodex in node) { foreach (XmlAttribute attrib in nodex.Attributes) { if (attrib.Name.Equals("type", StringComparison.OrdinalIgnoreCase)) { ID = attrib.Value; } } foreach (XmlNode nodexx in nodex.ChildNodes) { if (nodexx.Name.Equals("coordinates", StringComparison.OrdinalIgnoreCase)) { coordinateSystem = new Tools.CoordinateSystem(); coordinateSystem.Parse(nodexx); } else if (nodexx.Name.Equals("size", StringComparison.OrdinalIgnoreCase)) { size = new ACA.LabelX.Tools.Size(0, 0, coordinateSystem.units); size.Parse(nodexx); } else if (nodexx.Name.Equals("labelpos", StringComparison.OrdinalIgnoreCase)) { labelLayout = new LabelLayout(coordinateSystem.units); labelLayout.Parse(nodexx); } else if (nodexx.Name.Equals("offsets", StringComparison.OrdinalIgnoreCase)) { foreach (XmlNode nodexxx in nodexx) { if (nodexxx.Name.Equals("offset", StringComparison.OrdinalIgnoreCase)) { Offset offset = new Offset(coordinateSystem.units); offset.Parse(nodexxx); Offsets.Add(string.Format("{0}@{1}", offset.Printer, offset.Machine), offset); } } } } } }
public Tools.Size GetLabelSize() { if (labelLayout == null) { throw new ApplicationException("Please, parse a paper definition first"); } Tools.Size NettoPageSize = GetPrintablePageSize(); Tools.Length TotalHorizontalInterlabelGap = GetHorizontalInterlabelGap() * (int)(labelLayout.HorizontalCount - 1); Tools.Length LabelWidth = (NettoPageSize.Width - TotalHorizontalInterlabelGap) / (int)labelLayout.HorizontalCount; Tools.Length TotalVerticalInterlabelGap = GetVerticalInterlabelGap() * (int)(labelLayout.VerticalCount - 1); Tools.Length LabelHeight = (NettoPageSize.Height - TotalVerticalInterlabelGap) / (int)labelLayout.VerticalCount; return(new Tools.Size(LabelWidth, LabelHeight)); }
public void Print(string DocumentName, string Printer, string Tray, string PaperType, uint FromPage, uint ToPage, int Language) { PrinterSettings p = new PrinterSettings(); p.PrinterName = Printer; try { PreparePrint(Printer, PaperType); } catch (ApplicationException e) { GlobalDataStore.Logger.Error("Error: " + e.Message); } this.FromPage = FromPage; this.ToPage = ToPage; this.NextPage = FromPage; this.Language = Language; PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); //printDocument.PrinterSettings.PrinterName = Printer; printDocument.PrinterSettings = p; PaperSize gevondenSize = null; foreach (PaperSize ps in printDocument.PrinterSettings.PaperSizes) { if (ps.PaperName.Equals(PaperType, StringComparison.OrdinalIgnoreCase)) { gevondenSize = ps; break; } } if (gevondenSize == null) { System.Drawing.Printing.PrinterSettings.PaperSizeCollection col; col = printDocument.PrinterSettings.PaperSizes; Tools.Size theSize = PaperDef.GetLabelSize(); PaperSize sz = new PaperSize(PaperType, (int)(PaperDef.GetPhysicalLabelSize().Width.InInch() * 100), (int)(PaperDef.GetPhysicalLabelSize().Height.InInch() * 100)); printDocument.PrinterSettings.DefaultPageSettings.PaperSize = sz; } else { printDocument.PrinterSettings.DefaultPageSettings.PaperSize = gevondenSize; } if ((FromPage > 0) || (ToPage != uint.MaxValue)) { printDocument.PrinterSettings.PrintRange = PrintRange.SomePages; printDocument.PrinterSettings.FromPage = (int)FromPage; printDocument.PrinterSettings.ToPage = (int)ToPage; } else { printDocument.PrinterSettings.PrintRange = PrintRange.AllPages; } printDocument.DocumentName = DocumentName; try { printDocument.DefaultPageSettings.PaperSource.SourceName = Tray; } catch (InvalidPrinterException) { GlobalDataStore.Logger.Error("Printer " + printDocument.DefaultPageSettings.PrinterSettings.PrinterName + " does not exist."); } catch (Exception ex) { GlobalDataStore.Logger.Error(ex.Message); } printDocument.PrintController = new StandardPrintController(); if (printDocument.PrinterSettings.PrinterName.Contains("Microsoft XPS Document Writer")) { try { printDocument.PrinterSettings.PrintToFile = true; printDocument.PrinterSettings.PrintFileName = PreviewFileName + "tmp"; theBoundsCached = false; printDocument.Print(); printDocument.Dispose(); FileInfo fi = new FileInfo(PreviewFileName + "tmp"); try { if (File.Exists(PreviewFileName)) { File.Delete(PreviewFileName); } } catch (Exception) { GlobalDataStore.Logger.Error(string.Format("Cannot remove file {0}", PreviewFileName)); } try { fi.MoveTo(PreviewFileName); } catch { int i = 0; bool ready = false; while ((!ready) && (i < 10)) { i++; //Windows 8.1 locks files longer. Try another move after a wait System.Threading.Thread.Sleep(100); try { fi.MoveTo(PreviewFileName); ready = true; } catch (Exception) { //GlobalDataStore.Logger.Error("Cannot rename file from " + PreviewFileName + "tmp " + "to " + PreviewFileName + ". "); } } if (!ready) { GlobalDataStore.Logger.Error("Cannot rename file from " + PreviewFileName + "tmp " + "to " + PreviewFileName + "."); } } } catch (ArgumentNullException) { GlobalDataStore.Logger.Error("Cannot print to an XPS writer without a filename!"); } catch (Exception e) { GlobalDataStore.Logger.Error("An unexpected error occured: " + e.Message); } } else { try { printDocument.Print(); } catch (InvalidPrinterException pe) { GlobalDataStore.Logger.Error("Printing error. Selected printerdriver removed from your system?"); GlobalDataStore.Logger.Error(pe.Message); } } StaticVarslabel.Values.Clear();//Clear all static variables after printjob printDocument.Dispose(); }
public void PrintPreview(string Printer, string PaperType, string Tray, uint FromPage, uint ToPage, int Language) { PrinterSettings p; bool bGood = false; p = new PrinterSettings(); if (PrinterSettings.InstalledPrinters.Count > 0) { foreach (string s in PrinterSettings.InstalledPrinters) { if (s.Equals(Printer, StringComparison.OrdinalIgnoreCase)) { p.PrinterName = s; bGood = true; } } if (!bGood) { p.PrinterName = PrinterSettings.InstalledPrinters[0]; bGood = true; } } if (bGood == false) { MessageBox.Show("No installed printers were found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } PreparePrint(Printer, PaperType); this.FromPage = FromPage; this.ToPage = ToPage; this.NextPage = FromPage; this.Language = Language; PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); printDocument.PrinterSettings = p; PaperSize gevondenSize = null; foreach (PaperSize ps in printDocument.PrinterSettings.PaperSizes) { if (ps.PaperName.Equals(PaperType, StringComparison.OrdinalIgnoreCase)) { gevondenSize = ps; break; } } if (gevondenSize == null) { System.Drawing.Printing.PrinterSettings.PaperSizeCollection col; col = printDocument.PrinterSettings.PaperSizes; Tools.Size theSize = PaperDef.GetLabelSize(); PaperSize sz = new PaperSize(PaperType, (int)(PaperDef.GetPhysicalLabelSize().Width.InInch() * 100), (int)(PaperDef.GetPhysicalLabelSize().Height.InInch() * 100)); //PaperSize sz = new PaperSize(PaperType, (int)(PaperDef.GetPhysicalLabelSize().Width.InInch() * 96), (int)(PaperDef.GetPhysicalLabelSize().Height.InInch() * 96)); //TODO: DPI values printDocument.PrinterSettings.DefaultPageSettings.PaperSize = sz; } else { printDocument.PrinterSettings.DefaultPageSettings.PaperSize = gevondenSize; } if ((FromPage > 0) || (ToPage != uint.MaxValue)) { printDocument.PrinterSettings.PrintRange = PrintRange.SomePages; printDocument.PrinterSettings.FromPage = (int)FromPage; printDocument.PrinterSettings.ToPage = (int)ToPage; } else { printDocument.PrinterSettings.PrintRange = PrintRange.AllPages; } printDocument.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); printDocument.DocumentName = "Preview document"; printDocument.DefaultPageSettings.PaperSource.SourceName = Tray; PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog(); printPreviewDialog.Document = printDocument; printPreviewDialog.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; printPreviewDialog.UseAntiAlias = true; //remove later if proves to be to slow. ((ToolStrip)printPreviewDialog.Controls[1]).Items[0].Enabled = false; printPreviewDialog.ShowDialog(); StaticVarslabel.Values.Clear(); //Clear static variables after printjob printPreviewDialog.Dispose(); }