//  private void async PrintDocument(object sender, PrintPageEventArgs e, PrintDocumentModel document)
        private void PrintDocument(object sender, PrintPageEventArgs e, PrintDocumentModel document)
        {
            int documentPageWidth = MAX_WIDTH;

            if (document.PageWidth.HasValue)
            {
                documentPageWidth = document.PageWidth.Value;                              //DC Added PageWidth to Document Value
            }
            using (Graphics g = e.Graphics)
            {
                float y = 12; // changed from 10 to 12 DC

                foreach (var section in document.Sections)
                {
                    foreach (var line in section.Lines)
                    {
                        PrintLine(g, line, ref y, documentPageWidth);
                    }
                    y += 12; // changed from 10 to 12 DC
                }
            }

            e.HasMorePages = false;
            return;
        }
        // Partially complete printing a raw described document to PDF as a proof of concept, while eating lunch - DC
        private void PDFRawPrintDocument(object sender, PrintPageEventArgs e, PrintDocumentModel document)
        {
            int documentPageWidth = MAX_WIDTH;

            if (document.PageWidth.HasValue)
            {
                documentPageWidth = document.PageWidth.Value;                              //DC Added PageWidth to Document Value
            }
            //e.PageSettings.PrinterSettings.
            //e.PageSettings.PaperSize.PaperName = "A3";

            using (Graphics g = e.Graphics)
            {
                float y         = 12; // changed from 10 to 12 DC
                int   lineCount = 0;
                foreach (var section in document.Sections)
                {
                    foreach (var line in section.Lines)
                    {
                        PrintLine(g, line, ref y, documentPageWidth);
                        PrintLineRawAsGraphic(g, line, ref y, documentPageWidth, 5);
                    }
                    y        += 12; // changed from 10 to 12 DC
                    lineCount = lineCount++;
                    if (lineCount >= 57)
                    {
                        e.HasMorePages = true; lineCount = 0;
                    }
                }
            }
            e.HasMorePages = false;
        }
        public JsonResult Index([FromBody] PrintDocumentModel printDocumentModel)
        {
            isLandscape = printDocumentModel.IsLandscape;
            isColor     = printDocumentModel.IsColor;
            LoadPdfFromBase(printDocumentModel.PrinterDocument, printDocumentModel.PrinterName, printDocumentModel.PrinterTray, printDocumentModel.PaperSize);

            return(Json(new { printDocumentModel.PrinterName }));
        }
        private void RawPrintDocument(object sender, PrintPageEventArgs e, PrintDocumentModel document)
        {
            int documentPageWidth = MAX_WIDTH;

            if (document.PageWidth.HasValue)
            {
                documentPageWidth = document.PageWidth.Value;                              //DC Added PageWidth to Document Value
            }
            // Initialise Printer
            var          pd          = (System.Drawing.Printing.PrintDocument)sender;
            string       printerName = pd.PrinterSettings.PrinterName;
            string       ESC         = "\u001B";
            string       cmds        = ESC + "@"; //Initializes the printer (ESC @)
            string       condensed   = ESC + "g";
            string       subscript   = ESC + "!17";
            const string FORM_FEED   = "\f";

            RawPrinterHelper.SendStringToPrinter(printerName, cmds);
            if (document.Condensed.Value == true)
            {
                RawPrinterHelper.SendStringToPrinter(printerName, condensed);
            }
            if (document.Condensed.Value == true)
            {
                RawPrinterHelper.SendStringToPrinter(printerName, FORM_FEED);
            }
            //if (document.Condensed.Value == true) RawPrinterHelper.SendStringToPrinter(printerName, subscript);

            foreach (var section in document.Sections)
            {
                foreach (var line in section.Lines)
                {
                    System.Threading.Thread.Sleep(400);
                    // prevent buffer overrun, will need to find optimum value and make configuarable
                    if (document.Condensed.Value == true)
                    {
                        RawPrinterHelper.SendStringToPrinter(printerName, condensed);     // 05/07/2016 Test
                    }
                    RawPrintLine(printerName, line, documentPageWidth);
                    // also add the page length and count lines printed

                    //var lineout = RawPrintLineToFile(line);
                }
            }
            //TestPrintRaw();
        }
 public void PrintDocument(string[] printerNames, PrintDocumentModel document)
 {
     foreach (var printerName in printerNames)
     {
         if (document.RawPrinter == true)
         {
             //var printerNametest = "EPSON FX Series 1 (80)";
             var printerNametest = printerNames[0];
             // var printerNametest = "PDF Complete";
             Task t = Task.Run(() => Print <PrintDocumentModel>("Cheque", printerNametest, document, RawPrintDocument));
             t.Wait();
             //Task.Run(() => Print<PrintDocumentModel>("PDF Print", printerNametest, document, PDFRawPrintDocument));
         }
         else
         {
             //Task.Run(() => Print<PrintDocumentModel>("Customer Receipt", printerName, document, PrintDocument));
             Task t = Task.Run(() => Print <PrintDocumentModel>("Customer Receipt", printerName, document, PrintDocument));
             t.Wait(); // wait until finished to try and avoid Graphics context in use error DC - 25/07/16
         }
     }
 }
Beispiel #6
0
        public string Index([FromBody] PdfModel pdfModel)
        {
            PdfDocument pdf     = new PdfDocument();
            PdfPage     pdfPage = pdf.AddPage();

            XFont fontTitle = new XFont("Arial", 0.7055555555556, XFontStyle.Bold);
            XFont fontText  = new XFont("Arial", 0.388056, XFontStyle.Regular);

            pdfPage.Size               = PdfSharpCore.PageSize.A4;
            pdfPage.TrimMargins.Top    = 2.5;
            pdfPage.TrimMargins.Left   = 2.5;
            pdfPage.TrimMargins.Right  = 2.5;
            pdfPage.TrimMargins.Bottom = 2;

            XGraphics      graph         = XGraphics.FromPdfPage(pdfPage, XGraphicsUnit.Centimeter);
            XTextFormatter textFormatter = new XTextFormatter(graph);
            double         pdfWidth      = pdfPage.Width.Centimeter - 5;
            double         pdfHeight     = pdfPage.Height.Centimeter - 4.5;

            XRect rect = new XRect(0, 0, pdfWidth, pdfHeight);

            XImage xImage = XImage.FromStream(() => new MemoryStream(Convert.FromBase64String(pdfModel.Photo)));

            double ySpace = 0;

            graph.DrawImage(xImage, new XRect((pdfWidth - (pdfWidth * 0.2f)), 0, (pdfWidth * 0.23f), (pdfHeight * 0.2f)));


            textFormatter.DrawString("Registration Bestätigung", fontTitle, XBrushes.Black, new XRect(0, 0, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 1.5;
            textFormatter.DrawString(pdfModel.Sex, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 0.5;
            textFormatter.DrawString(pdfModel.Firstname + " " + pdfModel.Lastname, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 0.5;
            textFormatter.DrawString(pdfModel.Street + " " + pdfModel.StreetNr, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 0.5;
            textFormatter.DrawString(pdfModel.ZipCode + " " + pdfModel.City, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 0.5;
            textFormatter.DrawString(pdfModel.Country, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 1;
            textFormatter.DrawString("Telefonnummer: " + pdfModel.PhoneNumber, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 0.5;
            textFormatter.DrawString("Geburtsdatum: " + pdfModel.BirthdayDate, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 0.5;
            textFormatter.DrawString("Beruf: " + pdfModel.Occupation, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);
            ySpace += 1;
            textFormatter.DrawString("Bemerkung: \n" + pdfModel.Description, fontText, XBrushes.Black, new XRect(0, ySpace, pdfWidth, pdfHeight), XStringFormats.TopLeft);


            //pdf.Save(@".\Temp\test.pdf");
            MemoryStream stream = new MemoryStream();

            pdf.Save(stream, false);
            byte[] bytes = stream.ToArray();

            PrintDocumentController printDocument = new PrintDocumentController();

            PrintDocumentModel printDocumentModel = new PrintDocumentModel();

            printDocumentModel.PrinterName     = pdfModel.PrinterName;
            printDocumentModel.PrinterTray     = pdfModel.PrinterTray;
            printDocumentModel.PaperSize       = pdfModel.PaperSize;
            printDocumentModel.IsLandscape     = pdfModel.IsLandscape;
            printDocumentModel.IsColor         = pdfModel.IsColor;
            printDocumentModel.PrinterDocument = bytes;

            printDocument.Index(printDocumentModel);
            pdf.Clone();
            return("Done");
        }