weixin_33725515 2017-05-18 13:51 采纳率: 0%
浏览 33

从网站打印票证

Hello I'm trying to print a receipt/ticket from my web site but I doesn't has a normal printer, I have an Epson TM-T88V Receipt printer so I can't use the normal ctrl + p or print as a normal document, how can I print using this printer? I tried with this code but seems to only works in a windows forms application not with web forms maybe the problem is that I'm trying to execute my code in a WebMethod using an ajax call.

WebMethod and Print code

[WebMethod]
public string print()
{
   try
   {
       pd.PrintPage += new PrintPageEventHandler(Imprimir);
       return "Printing...";
    }
    catch (Exception ex)
    {
        return "Error: " + ex.Message;
    }
}

public void Imprimir(object sender, PrintPageEventArgs e)
{
   string titulo = "MyCompany";
   string direccion = "Address";
   string datos = "number             RFC: ";
   string datos2 = "description             17/05/2017 3:55 p.m.";
   PrintDocument ticket = new PrintDocument();

   Graphics g = e.Graphics;
   g.DrawRectangle(Pens.Black, 5, 5, 410, 530);

   Font fBody = new Font("Lucida Console", 15, FontStyle.Bold);
   Font fBody1 = new Font("Lucida Console", 15, FontStyle.Regular);
   Font fBody2 = new Font("Lucida Console", 9, FontStyle.Regular);
   Font rs = new Font("Stencil", 25, FontStyle.Bold);
   Font fTType = new Font("", 150, FontStyle.Bold);
   SolidBrush sb = new SolidBrush(Color.Black);

   g.DrawString(titulo, fBody, sb, 10, 120);
   g.DrawString(direccion, fBody1, sb, 10, 120);
   g.DrawString(datos, fBody1, sb, 10, 120);
   g.DrawString(datos2, fBody1, sb, 10, 120);

   g.DrawString("------------------------------", fBody1, sb, 10, 120);

   g.Dispose();

   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.Print();
}

AJAX CALL

function Imprimir() {
      CallWM('../../ws_webservice.asmx/print',
         {  },
         function (r_json) {
              bootbox.alert(r_json);
          });
}

I get the Printing... message but nothing is printed, which is my problem? Also I want to know if is possible call Imprimir method but sending parameters and how can I do that?

PD. I'm using C#, ASP.NET, html page and ajax.

  • 写回答

1条回答 默认 最新

  • 七度&光 2017-05-18 14:00
    关注

    Take this part of code from Imprimir to print():

       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.Print();
    

    For more information about PrintDocument look here

    评论

报告相同问题?