2017-01-29 5 views
1

Je travaille avec un logiciel d'impression de factures de restaurants.Formater un reçu avec l'application WinForms

J'ai développé un reçu. mais ils ne ressemblent pas vraiment à un reçu.

J'ai suivi une solution de codeProject. Voici mon code de la construction d'un reçu:

//Get the Graphics object 
     Graphics g = printTextEvArgs.Graphics; 

     //Create a font Arial with size 16 
     Font font = new Font("Arial", 10); 
     float fontHeight = font.GetHeight(); 
     string underLine = "------------------------------------------"; 

     int startX = 10; 
     int startY = 10; 
     int offset = 40; 

     //Create a solid brush with black color 
     SolidBrush brush = new SolidBrush(Color.Black); 
     if (RecieptType == "ktcprinter") 
     { 
     } 
     else if (RecieptType == "billprinter") 
     { 
      g.DrawString(restaurantInfo.name, new Font("Courier New", 16), new SolidBrush(Color.Black), startX, startY); 
      offset = offset + (int)fontHeight + 20; 

      var wc = new WebClient(); 
      Image imgFromUrl = Image.FromStream(wc.OpenRead(b.restaurant_info.logo)); 
      g.DrawImage(imgFromUrl, 60, 40, 150, 100); 

      offset = offset + (int)fontHeight + 50; 

      g.DrawString("Address: " + restaurantInfo.address, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Phone: " + restaurantInfo.phone, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Vat Reg. No.: " + restaurantInfo.vat_reg_no, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Served By: " + employeeInfo.served_by, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + 13; 
      g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + 13; 

      foreach (var item in b.items) 
      { 
       string menuTitle = item.menu_title + item.quantity + item.price; 
       g.DrawString(menuTitle, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
       offset = offset + (int)fontHeight + 5; 
      } 

      //Get UnderLine 
      offset = offset - 8; 
      g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + 15; 

      g.DrawString("Sub Total: " + calculation.sub_total, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Discount: " + calculation.discount, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5;     
      g.DrawString("Vat: " + calculation.vat, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Service Charge: " + calculation.service_charge, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Delivery Charge: " + calculation.delivery_charge, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 

      //Get UnderLine 
      offset = offset + 12; 
      g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + 12; 

      g.DrawString("Total: " + calculation.total.PadRight(30), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 

      //Get UnderLine 
      offset = offset - 11; 
      g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + 13; 


      foreach (var item in b.payments) 
      { 
       string paymentMethod = item.method + item.amount; 
       g.DrawString(paymentMethod, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
       offset = offset + (int)fontHeight + 5; 
      } 

      g.DrawString("Change: " + calculation.change, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      g.DrawString("Pay Tip: " + calculation.pay_tip, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset); 
      offset = offset + (int)fontHeight + 5; 
      offset = offset + 20; 

      //offset = offset + 20; 
      //g.DrawString("Total To Pay".PadRight(30) + string.Format("{0:c}"), variable, font, new SolidBrush(Color.Black), startX, startY + offset); 
     } 

     //Draw "Hello Printer!"; 
     //new Rectangle(margin X, margin Y, Width, Height) 
     //g.DrawString(printString.ToString(), 
     //font, brush, new Rectangle(5, 0, 350, 500)); 
    } 

Ce que je suis arrivé est comme l'image ci-dessous. Je veux faire comme un vrai reçu.

Les quantités doivent être dans une colonne et le prix dans une autre colonne.

(Notez que, la solution que je suivais a parfaitement fonctionné pour eux.)

Ma sortie au moment:

enter image description here

+0

Pour créer des colonnes, il suffit d'imprimer les différents éléments à des positions x fixes sur la même ligne. Ou: Puisque vous utilisez une police fixe, vous pouvez également remplir la partie gauche avec des espaces pour un certain nombre de caractères. - Aussi: Je suggère, quand on pose des questions sur le formatage, de mettre un peu plus d'effort dans le formatage de votre question! – TaW

Répondre

0

Si vous voulez aller avec une police fixe, comme Courier ou Consolas, votre problème peut être facilement résolu par rembourrage la partie gauche de chaque ligne à une certaine longueur avec des espaces.

L'étape suivante consiste à remplir la partie droite de sorte que les chiffres soient alignés à droite.

Pour cela, vous mieux écrire une petite fonction d'aide:

string alignedNumber(decimal number, int length) 
{ 
    return ("$" + number).PadLeft(length, ' '); 
} 

Vous écrivez:

g.DrawString("Change: ".PadRight(25, ' ') + alignedNumber(price, 8)...); 

..pour toutes vos sorties avec des colonnes. (Choisissez vos propres numéros!)

Si vous choisissez de passer à une police proportionnelle, vous devrez écrire des appels DrawString distincts pour la position et définir le décalage x pour chacun. Pour le bon alignement, vous devrez également mesurer la largeur du nombre de chaînes aura Graphics.MeasureString. Voir here for an example

+0

Merci frère. Cela fonctionne parfaitement. Il y a une chose que je voulais savoir. J'ajoute PadRight sur supposer menuTitle. Si le menuTitle croise la taille du pad, ces mots supplémentaires seront-ils dans une nouvelle ligne? –

+0

Non. Par défaut, tout ce que vous dessinez comme ça va glisser du pad et être perdu. Vous pouvez compter les caractères (pour une police fixe) et diviser les données en deux lignes ou utiliser l'une des [DrawString overload] (https://msdn.microsoft.com/en-us/library/21kdfbzs%28v=vs. 110% 29.aspx) qui prennent un Rectangle englobant. Utilisez Graphics.MeasureString pour mesurer avant de dessiner afin de connaître la hauteur résultante !!! - Si vous êtes satisfait d'une réponse, veuillez considérer [accepter] (http://stackoverflow.com/help/accepted-answer) it ..! – TaW

+0

désolé frère, je ne savais pas le processus d'acceptation. Maintenant j'ai accepté la réponse car cela m'a beaucoup aidé. BTW, frère s'il y a un tutoriel ou quelque chose pour ce "diviser les données en deux lignes" question qui pourrait m'aider s'il vous plaît me suggérer. Je serai très reconnaissant. et bien sûr monsieur, si cette question est utile, votez s'il vous plaît. et merci encore –