Esempio n. 1
0
        private PrintDocument CreatePrinterDocument(PrinterOptions options)
        {
            var doc = new PrintDocument();

            SetPrinterFromSettings(doc);
            doc.PrintPage += T_PrintPage;
            doc.DefaultPageSettings.Landscape = true;
            doc.DefaultPageSettings.Margins   = new Margins(0, 0, 0, 0);
            foreach (PaperSource paperSource in doc.PrinterSettings.PaperSources)
            {
                if (paperSource.SourceName.Equals(GetSourceName(options.LabelSource ?? PrinterSettings.LabelSource), StringComparison.CurrentCultureIgnoreCase))
                {
                    doc.DefaultPageSettings.PaperSource = paperSource;
                    break;
                }
            }
            doc.DefaultPageSettings.PaperSize = new PaperSize(_labelProperties.LabelName, _labelProperties.Dimensions.Width, _labelProperties.Dimensions.Height * _labelProperties.LabelCount);
            // generate the label as an image
            _paperRect = new Rectangle(0, 0, doc.DefaultPageSettings.PaperSize.Width, doc.DefaultPageSettings.PaperSize.Height);
            for (var i = 1; i <= _labelProperties.LabelCount; i++)
            {
                _labelStart.Add(new PointF(0, _labelProperties.TopMargin + _paperRect.Height - (_paperRect.Height / i)));
            }
            var printImage = new Bitmap(_paperRect.Width, _paperRect.Height);

            printImage.SetResolution(doc.DefaultPageSettings.PrinterResolution.X, doc.DefaultPageSettings.PrinterResolution.Y);
            _printImage = printImage;
            return(doc);
        }
Esempio n. 2
0
        public Image PrintLabel(ICollection <LineConfiguration> lines, PrinterOptions options)
        {
            if (lines == null || !lines.Any())
            {
                throw new ArgumentNullException(nameof(lines));
            }
            _labelProperties = GetLabelDimensions(options.LabelName);
            var doc = CreatePrinterDocument(options);
            var g   = Graphics.FromImage(_printImage);

            DrawLabel(g, lines, _paperRect);

            // for debugging label layout
            if (options.ShowDiagnostic)
            {
                DrawDebug(g);
            }

            if (!options.GenerateImageOnly)
            {
                doc.Print();
            }
            return(_printImage);
        }
Esempio n. 3
0
        public Image PrintLabel(LabelContent content, PrinterOptions options)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            _labelProperties = GetLabelDimensions(PrinterSettings.LabelName);
            var doc = CreatePrinterDocument(options);
            var g   = Graphics.FromImage(_printImage);

            DrawLabel(g, content, _paperRect);

            // for debugging label layout
            if (options.ShowDiagnostic)
            {
                DrawDebug(g);
            }

            if (!options.GenerateImageOnly)
            {
                doc.Print();
            }
            return(_printImage);
        }