C# printing page height according to the content - c#

I'm doing some printing work in c# and got a small problem. I'm working with a thermal receipt printer. I want to set the height of the page according to the content of it. Which means, when I have fewer items, the page should be smaller and when I have long list of items, the page should grow accordingly.
I tried to set it with PrintPageEventArgs but this didn't result in satisfied result. How can this be done?
Thanks

Just take care of the width. Printer will cut the paper after the last printed element on the last page.

Just for the record. I had the same issue.
At the end what i had done is:
Int Line= starting position (in pixels)
For each line I want to print -> g.DrawString("text",font,xx,margin,line);
and then just before print
ps.Height = Line;
pd.Print();
pd is -> PrintDocument pd = new PrintDocument();
ps is -> PaperSize ps = new PaperSize("",my_width,1));
`enter code here` pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.PrintController = new StandardPrintController();
pd.DefaultPageSettings.Margins.Left = 0;
pd.DefaultPageSettings.Margins.Right = 0;
pd.DefaultPageSettings.Margins.Top = 0;
pd.DefaultPageSettings.Margins.Bottom = 0;
pd.DefaultPageSettings.PaperSize = ps;

Related

Printing to a smartcard printer not working, ideas?

I'm trying to print userdata to a smartcard using a smartcard printer but the result is that i'm getting blank cards out of the printer. I've been stuck on this problem for a day now and there is no(findable) answer on the internet.
With printing to the printer (it's an Datacard SP35 Plus) using no custom papersize and no printdialog. the result is an empty card.
using (PrintDocument pd = new PrintDocument())
{
pd.PrintPage += (object sender, PrintPageEventArgs e) =>
{
Image i = Image.FromFile("E:\\tmp.png");
e.Graphics.DrawImage(i, e.MarginBounds);
};
pd.Print();
}
The image is present and visible and plenty of size.
Furthermore, I came across a post that said that the size of the printdocument has to be set
PaperSize papersize = new PaperSize("Custom", Convert.ToInt32(widthInInch * 100), Convert.ToInt32(heightInInch * 100));
pd.DefaultPageSettings.PaperSize = papersize;
pd.PrinterSettings.DefaultPageSettings.PaperSize = papersize;
But didnt result in a visible print.
I've also tried
e.Graphics.DrawImage(i, 0, 0);
instead of
e.Graphics.DrawImage(i, e.MarginBounds);
but didn't result in a visible print.
The widthInInch is 3.38 and the heightInInch is 2.13(The default size of an CR80 card). This also resulted in an empty card.
When I print to PDF the result is visible and correct(I do not know the whitespace, of course).
Does somebody see the problem or has worked out something similar?
I "fixed" it by rewriting my application to UWP. This uses the new print function and made it work as intended.

Receipt printing in roll paper

I searched for a lot in google and did not find what i actually wanted. I got following code, which will print the variable name. I got a Epson Dot Matrix Printer and Roll Paper (Endless continuous paper).
My problem is that, after printing name paper feeds up to size of A4. I don`t want the paper feed. This application is intended to do print receipts which will have unlimited data which need to be printed flawless ( with out page break).
Can you, the smart folk out there to point me in the correct direction with these codes?
edited this code and changed scenario .. please move down further
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Font Heading2 = new Font("Times New Roman", 13);
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Near;
sf.Alignment = StringAlignment.Center;
//e.HasMorePages = false;
PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200);
pd.DefaultPageSettings.PaperSize = pkCustomSize1;
e.Graphics.DrawString(name.ToString(), Heading1, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width / 2), e.MarginBounds.Top, sf);
}
Edit 1:- #Adriano Repetti suggested this is a duplicate with Form feed in c# printing. What i learned from the above question is that he want to add the form feed. But i want to remove the form feed.
Edit 2:- I got an another hint by googling that setting the page Height equal to line height will make stop feeding which sounds promising. I am trying to figure that out too.
Edit 3:- #Adriano Repetti suggested me with Raw Printing (Directly printing binary data) with KB link. I googled around about it, and found out its c# better equivalent paste bin or pastie.org (provided because it is a handy one) . At first it sounded good and it worked nicely stopping form feeding. But eventually i struck some ice berg.
On my code i had to align some printing quotes to center or align to left`. for which i got only option of using space and tabs. But there will be no guaranty that it will be well formatted as we can not certain about built in fonts with printer.( Refer:SO Question by #syncis )
And secondly, i will have to move my application to unicode(local language support) capable one, at least with in a month or so. In that scenario, raw printing wont help and i will have to go through theses faces again. SO, For avoiding that its better for me to stay with Graphics DrawString. And for this i changed my code as.
//---------
// start button click
//---------
PrintDocument pdoc = new PrintDocument();
pdoc.DefaultPageSettings.PaperSize.Height = 300;
pdoc.Print();
//---------
// end button click
//---------
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Font Heading2 = new Font("Times New Roman", 13);
// changed following statement to met with new **unicode** criteria
//e.Graphics.DrawString(name.ToString(), Heading1, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width / 2), e.MarginBounds.Top, sf);
TextRenderer.DrawText(e.Graphics, "My name in local language is വിനീത്", Heading2, new Point(0, 0), Color.Black);
}
With current problems, i am redefining the Question extending tag to unicode as.
How can print with TextRenderer.DrawText with unicode support without form feeding ? I think setting paper height to line height will solve my problem. If so how or suggest me a better way to stop paper feeding. It really eats a lot of my valuable time...
EDIT 4: Today I found out a very interesting thing about my printer. I cant even set custom paper size manually (Not by coding.. I mean control panel->printers and faxes ->Epson LX-300+ ->properties -> printing preference-> paper/quality -> advanced -> paper size -> BOOOOOM not showing my custom paper size). I am using Epson LX-300+ printer. Do guys think it wont support custom paper sizes? is that causing me problems?
I found the solution by my self ( sorry for my english ) As Hans Passant says ( PrintDocument is page based. end of story ) You must use ( e.HasMorePages = true; )
float cordenadaX;
float cordenadaY;
int totalPages;
int paginaAtual;
int indiceItem;
List<string> items;
public void ImprimeDanfeNFCe()
{
totalPages = 1;
paginaAtual = 1;
indiceItem = 0;
cordenadaX = 0;
cordenadaY = 0;
items = new List<string>();
items.Add("Item1");
items.Add("Item2");
items.Add("Item3");
(............)
PrintDocument recordDoc = new PrintDocument();
recordDoc.DocumentName = "xMarket danfe";
recordDoc.PrintPage += new PrintPageEventHandler(imprimeDanfeReceipt);
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "My printer";
recordDoc.PrinterSettings = ps;
recordDoc.Print();
recordDoc.Dispose();
}
void imprimeDanfeReceipt(PrintPageEventArgs e)
{
float pageHeight = e.MarginBounds.Height;
string text = "";
if (paginaAtual == 1)
{
text = "Cupom header";
e.Graphics.DrawString(text, drawFontDanfeTitulo, drawBrush, new
RectangleF(cordenadaX, cordenadaY, width, height),
drawFormatCenter);
cordenadaY += e.Graphics.MeasureString(text, drawFontDanfeTitulo).Height;
}
for (int i = indiceItem; i < items.Count; i++)
{
int indice = i + 1;
//items[i] Is very important to not print same items again while print next page
e.Graphics.DrawString(items[i], drawFontDanfeItems, drawBrush,
new RectangleF(cordenadaX, cordenadaY, width, height), drawFormatLeft);
cordenadaY += e.Graphics.MeasureString(text, drawFontDanfeTitulo).Height;
indiceItem++;
//cordenadaY+100 is for the size of the footer
if (cordenadaY+100 >= pageHeight)
{
paginaAtual++;
e.HasMorePages = true;
return;
}
}
e.Graphics.DrawString("page footer", drawFontDanfeItems, drawBrush,
new RectangleF(cordenadaX, cordenadaY, width, height), drawFormatLeft);
cordenadaY += e.Graphics.MeasureString(text, drawFontDanfeTitulo).Height;
}

In C#, what's the difference of PrinterSettings from a PrintDocument and PrintDialog?

Well, it's a very strange problem, and I already found a black magical solution. I'm curious about the reason.
I'm writing a program which use Brother QL-700 label printer to print labels. I need the labels be printed without showing the printer dialog.
The label printer support different size of label rolls and the default size of label roll is 29mm, while what I need is 62mm. I found that I can set the Page Size by
PrintDocument doc = new PrintDocument();
PaperSize size = new PaperSize() ;
size.Width = 244;//2.44 inch = 62mm
size.Height = 244;
size.RawKind = 256;//RawKind=0 does not work, I don't know why
doc.PrinterSettings.DefaultPageSettings.PaperSize = size;
However, this won't work, the driver of Printer would show a message saying the width does not fit.
But if I change the copy the PrinterSettings from a PrintDialog(), without showing it,
PrintDialog dlg = new PrintDialog();
doc.PrinterSettings = dlg.PrinterSettings;
Then it works.
In conclusion, what I don't understand is why
size.RawKind = 256;
and
PrintDialog dlg = new PrintDialog();
doc.PrinterSettings = dlg.PrinterSettings;
can make the printer work?
The documentation for RawKind does not state that 0 is a valid value. It also states that any value above 118 indicates a custom paper size. Since you are specifying a custom paper size, the value of 256 indicates that the size is custom and that is why it works.

Issue printing image as expected

I have an image that I'm trying to print to legal size. However, there are a few challenges to this.
The image will vary in size. This is because I'm using a control that has limited print options but can export to an image.
I want to maximize the print area on the page. Smallest margin possible
The user needs to be able to select a printer and set options
Here's the code I'm using:
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(Globals.TempDirectory + #"\temp.jpg");
Point p = new Point(100, 100);
Margins margins = new Margins(50, 50, 50, 50);
pd.DefaultPageSettings.Margins = margins;
args.Graphics.DrawImage(i, p);
};
pd.Print();
I've been having trouble with this because I can't set margins and can't seem to get the print out right. I want it to print in legal but when I print the image, it's not rotated properly and it just prints to a default printer. I'm up for anything to get this to work.
Printing in C# sucks
try a
printdialog()
to allow the user to select a printer and settings. once you get that to work the rest of it might click for you.
Edit: Showing you where and how to use it.
PrintDialog pDialog = new PrintDialog();
if (pDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = pDialog.PrinterSettings;
pd.Print();
}

ZEBRA label printing

I need to print labels on my ZEBRA S4M printer.
PrintDocument pd = new PrintDocument { PrinterSettings = new PrinterSettings { PrinterName = "ZDesigner S4M-203dpi ZPL" } };
pd.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
And when using Portrait landscape - it's alright, but in Landscape mode I have a big space from the left side of label.. How can I print a horizontal text on my labels?
Thanks a lot.
ADDED
I think that this problem is possible because of small PrintingArea property that I can't change. It's size 300x200 and real paper size is about 200x500...
I believe you have two options, either try changing your default paper size, or performing a translate.

Categories

Resources