4
votes

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?

1
Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not"!user57508
I don`t think it is duplicate. Because in Form feed in c# printing trying insert form feed and here i am trying to remove the form field.vinrav
Sorry, I pasted wrong link. AFAIK problem is that PrintDocument will add a form-feed for each page and all it's logic assume you're printing a page each time. Yes you may keep data until a page is completed or do it in the right (hard) way as described in this article (for VB.NET but code is pretty easy to translate): support.microsoft.com/en-us/kb/322090Adriano Repetti
Thank you @Adriano Repetti for [KB link](support.microsoft.com/en-us/kb/322090). I run the code in separate VB Project and it executed as if it was made for me. I will be converting the code shortly and will post updates on it.vinrav
Using the PrintDocument class is just the wrong approach. Printing in Windows is page-based, as long as you use the printer driver you will always get the formfeed. It is also notoriously slow, too slow for point-of-sale apps. You need to bypass the driver and send the commands to the printer directly, this KB article shows how. Multiply the "eating time" problem by at least a factor of pi squared.Hans Passant

1 Answers

0
votes

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;

}