Hi I would like to print a product barcode in a sheet but if the user write 6 products on stock it must print 6 times this code on the sheet, I have a code but it prints just one code per image, please see it below.
private void generar_codigo_btn_Click(object sender, EventArgs e)
{
string codigo_generado = nom_prd_txt.Text;
BarcodeLib.Barcode Codigo = new BarcodeLib.Barcode();
Codigo.IncludeLabel = true;
codigo_pic.BackgroundImage = Codigo.Encode(BarcodeLib.TYPE.CODE128, codigo_generado, Color.Black, Color.White, 173, 102);
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
doc.PrintPage += Doc_PrintPage;
pd.Document = doc;
if (pd.ShowDialog() == DialogResult.OK)
doc.Print();
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(codigo_pic.Width, codigo_pic.Height);
codigo_pic.DrawToBitmap(bm, new Rectangle(5, 5, codigo_pic.Width, codigo_pic.Height));
e.Graphics.DrawImage(bm, 0, 0);
bm.Dispose();
}
You have to draw your bar-code multiple times in bitmap
int NoOfTimesToPrint = 5;
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(codigo_pic.Width, codigo_pic.Height);
for(int i=1;i<=NoOfTimesToPrint;i++)
{
codigo_pic.DrawToBitmap(bm, new Rectangle(5, i*5, codigo_pic.Width, codigo_pic.Height));
}
e.Graphics.DrawImage(bm, 0, 0);
bm.Dispose();
}
Please Note you have to Change NoOfTimesToPrint variable in order to work this code as you need
int NumOfLabel = Convert.ToInt16(textBox_StockAddQuntity_StockEntry.Text); /* here for example i set to total copy */
PrintDialog pDlg = new PrintDialog();
pDlg.Document = printDocument_Barcode;
pDlg.AllowSelection = true;
pDlg.AllowSomePages = true;
pDlg.PrinterSettings.Copies = (short)NumOfLabel;
printDocument_Barcode.Print();
Related
I have the following code, but its printing from the start of the label and trying to print forward when it should start from the end of the label and print forward.. I have an image below for reference, I have looked on stackoverflow for similar questions but no luck. Thank you for your time and efforts to help me.
using System;
using System.Drawing;
using System.Drawing.Printing;
using Gtk;
public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;
}
public void Print()
{
using (PrintDocument pd = new PrintDocument())
{
var width = 2.44 * 100;
var height = 100;
var s = new System.Drawing.Printing.PaperSize("Custom", (int)width, (int)height);
pd.DefaultPageSettings.PaperSize = s;
pd.DefaultPageSettings.Landscape = true;
pd.DocumentName = "Label";
pd.OriginAtMargins = true;
pd.PrintPage += Pd_PrintPage;
pd.Print();
}
}
private void Pd_PrintPage(object sender, PrintPageEventArgs e)
{
Font drawFont = new Font("Arial", 60);
SolidBrush drawBrush = new SolidBrush(Color.Black);
StringFormat drawFormat = new StringFormat();
e.HasMorePages = false;
e.Graphics.DrawString(LabelPreview.Buffer.Text, drawFont, drawBrush, 0, 0, drawFormat);
}
protected void OnButton1Released(object sender, EventArgs e)
{
Print();
}
}
Here is the image
Please try to add pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); after pd.OriginAtMargins = true;.
The default value of pd.DefaultPageSettings.Margins is 1-inch margins on all sides.
The code below is showing me half part of the group box in print preview:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
// Bitmap bmp = new Bitmap(groupBox1.ClientRectangle.Width, groupBox1.ClientRectangle.Height);
// groupBox1.DrawToBitmap(bmp, groupBox1.ClientRectangle);
// e.Graphics.DrawImage(bmp, 0, 0);
Bitmap bmp = new Bitmap(groupBox1.Width, groupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
groupBox1.DrawToBitmap(bmp, new Rectangle(0, 0, groupBox1.Width, groupBox1.Height));
e.Graphics.DrawImage((Image) bmp, 0, 0);
}
private void button1_Click(object sender, EventArgs e) {
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument Pd = new PrintDocument();
Pd.PrintPage += this.printDocument1_PrintPage;
ppd.Document = Pd;
ppd.ShowDialog();
}
You need to draw within the bounds of the page.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var g = e.Graphics;
var srcRect = new Rectangle(0, 0, groupBox1.Width, groupBox1.Height);
var desRect = new Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, srcRect.Height);
//Or to draw within the margin
//var desRect = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, srcRect.Height);
using (var bmp = new Bitmap(srcRect.Width, srcRect.Height))
{
groupBox1.DrawToBitmap(bmp, srcRect);
g.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel);
}
}
Also, create the PrintDocument object and register its PrintPage event in the Form's constructor or Load event to avoid repeating that again and again in the button1_Click event. And don't forget to dispose the disposable objects.
I'm trying to print a panel that has pictureboxes, labels and textboxes. I want to print it on A4 paper so the panel has 595x842.
I have tried:
private void print_Click(object sender, EventArgs e)
{
PrintPanel(panel1);
}
private void PrintPanel(Panel pnl)
{
PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(pnl.Width, pnl.Height);
pnl.DrawToBitmap(memoryImage, pnl.ClientRectangle);
if (myPrintDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
}
printDocument1.Dispose();
}
When I try to print it I go to the One Note and all I get is a white page. What seems to be the problem?
I modified the code a bit. Now I get some output but it's still not good...
This is the code I used:
private void print_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Panel grd = new Panel();
Bitmap bmp = new Bitmap(grd.Width, grd.Height, grd.CreateGraphics());
grd.DrawToBitmap(bmp, new Rectangle(0, 0, grd.Width, grd.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
And I get this:
http://i.stack.imgur.com/HB1EH.png
Can someone help?
You are printing the wrong panel. Your code is creating a new empty panel and trying to print that (which it successfully does). You need to reference the existing panel you want to print:
void doc_PrintPage(object sender, PrintPageEventArgs e) {
using (Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height)) {
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top,
bounds.Width, factor * bounds.Width);
}
}
I have a panel which have labels and a datagridview. I'm trying to print the panel with its contents using this code
PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(panel2.Width, panel2.Height);
panel2.DrawToBitmap(memoryImage, panel2.ClientRectangle);
myPrintDialog.ShowDialog();
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
printDocument1.Dispose();
But it prints nothing. I remove the datagridview but still prints nothing. Then I change the panel back color but again it prints white page. Kindly Guide me How I do this?
Add this code to PrintPage event and call the Print() method from PrintDocument class object
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(panel2.Width, panel2.Height);
panel2.DrawToBitmap(bmp, new Rectangle(0, 0, panel2.Width, panel2.Height));
e.Graphics.DrawImage((Image)bmp, x, y);
}
Call the method like this
PrintDocument doc = new PrintDocument();
doc.PrintPage += this.doc_PrintPage;
PrintDialog dlg = new PrintDialog();
dlg.Document = doc;
if (dlg.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
Right now, I'm trying to build my form on a PrintDocument, but the only way for me to see where stuff is actually appearing on the page is to print a paper. It works, but I have a lot of stuff I need to add, and I'd rather not waste tons of paper. Right now I have a print dialogue come up, but theres no print preview button. Is there a way I can make one appear? Thanks!
public partial class Form4 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
public Form4()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Single yPos = 0;
Single leftMargin = e.MarginBounds.Left;
Single topMargin = e.MarginBounds.Top;
Image img = Image.FromFile("logo.bmp");
Rectangle logo = new Rectangle(40, 40, 50, 50);
using (Font printFont = new Font("Arial", 20.0f))
{
e.Graphics.DrawImage(img, logo);
e.Graphics.DrawString("Header", printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
}
using (SolidBrush blueBrush = new SolidBrush(Color.Black))
{
Rectangle rect = new Rectangle(100, 100, 500, 120);
e.Graphics.FillRectangle(blueBrush, rect);
}
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button();
this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example";
printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click);
this.Controls.Add(printButton);
}
}
You can use a PrintPreviewDialog for this:
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
PrintDialog printdlg = new PrintDialog();
PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();
// preview the assigned document or you can create a different previewButton for it
printPrvDlg.Document = pd;
printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below
printdlg.Document = pd;
if (printdlg.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}