Skip to content

Instantly share code, notes, and snippets.

@cdesch
Last active June 7, 2017 00:30
Show Gist options
  • Save cdesch/aebbf85a5360131e74691e9043565d10 to your computer and use it in GitHub Desktop.
Save cdesch/aebbf85a5360131e74691e9043565d10 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using BarcodeLib;
using QRCoder;
namespace Sample
{
class CouponPrinter
{
public String PrinterName { get; set; }
public CouponPrinter(String printerName)
{
PrinterName = printerName;
}
public void PrintReceiptForTransaction()
{
PrintDocument recordDoc = new PrintDocument();
recordDoc.DocumentName = "Receipt";
recordDoc.PrintPage += new PrintPageEventHandler(CouponPrinter.PrintReceiptPage);
recordDoc.PrintController = new StandardPrintController();
// Comment if debugging
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = PrinterName;
recordDoc.PrinterSettings = ps;
recordDoc.Print();
// --------------------------------------
// Uncomment if debugging - shows dialog instead
/*
PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();
printPrvDlg.Document = recordDoc;
printPrvDlg.Width = 1200;
printPrvDlg.Height = 800;
printPrvDlg.ShowDialog();
*/
// --------------------------------------
recordDoc.Dispose();
}
private static void PrintReceiptPage(object sender, PrintPageEventArgs e)
{
string transactionId = "038000356216";
float x = 10;
float y = 5;
float width = 260.0F; // max width I found through trial and error
float height = 0F;
Font drawFontArial12Bold = new Font("Arial", 12, FontStyle.Bold);
Font drawFontArial10Regular = new Font("Arial", 10, FontStyle.Regular);
Font drawFontArial7Regular = new Font("Arial", 7, FontStyle.Regular);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Set format of string.
StringFormat drawFormatCenter = new StringFormat();
drawFormatCenter.Alignment = StringAlignment.Center;
StringFormat drawFormatLeft = new StringFormat();
drawFormatLeft.Alignment = StringAlignment.Near;
StringFormat drawFormatRight = new StringFormat();
drawFormatRight.Alignment = StringAlignment.Far;
// Draw string to screen.
string text = "Company Name";
e.Graphics.DrawString(text, drawFontArial12Bold, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);
y += e.Graphics.MeasureString(text, drawFontArial12Bold).Height;
text = "Company Address Address";
e.Graphics.DrawString(text, drawFontArial10Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);
y += e.Graphics.MeasureString(text, drawFontArial10Regular).Height;
text = "Company URL";
e.Graphics.DrawString(text, drawFontArial10Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);
y += e.Graphics.MeasureString(text, drawFontArial10Regular).Height;
// Line feed
y += e.Graphics.MeasureString(text, drawFontArial10Regular).Height;
// Generate Bar Code
Bitmap barcode = generateBarCode(transactionId);
float barcodeWidth = x + (width / 2) - (barcode.Width / 2);
e.Graphics.DrawImage(barcode, new Rectangle((int)barcodeWidth, (int)y, barcode.Width, barcode.Height));
y += barcode.Height;
// Print Barcode Number underneath
text = transactionId;
e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);
y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;
Bitmap qrCodeImage = generateQRCode("The text which should be encoded.");
e.Graphics.DrawImage(qrCodeImage, qrCodeImage.Width, qrCodeImage.Height);
float qrcodeWidth = x + (width / 2) - (qrCodeImage.Width / 2);
e.Graphics.DrawImage(qrCodeImage, new Rectangle((int) qrcodeWidth, (int) y, qrCodeImage.Width, qrCodeImage.Height));
y += qrCodeImage.Height;
text = "Company Name";
e.Graphics.DrawString(text, drawFontArial10Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);
y += e.Graphics.MeasureString(text, drawFontArial10Regular).Height;
text = "Company URL";
e.Graphics.DrawString(text, drawFontArial10Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);
y += e.Graphics.MeasureString(text, drawFontArial10Regular).Height;
}
private static Bitmap generateQRCode(string url)
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
return qrCode.GetGraphic(2);
}
private static Bitmap generateBarCode(string code)
{
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
Image img = b.Encode(BarcodeLib.TYPE.UPCA, code, Color.Black, Color.White, 100, 25);
return new Bitmap(img);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment