Nav apraksta

Printer.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Shapes;
  12. using System.Printing;
  13. namespace WpfApplication19
  14. {
  15. public class Printer
  16. {
  17. //public int c;
  18. public static void Print(string s, int ncol){
  19. var n = s.Split('\n').Length - 1;
  20. int fn;
  21. int limitFont = 11;
  22. if( ncol >= 3 && n <= 5){
  23. fn = limitFont;
  24. }
  25. else{
  26. fn = 16 - n;
  27. if (fn < limitFont)
  28. fn = limitFont;
  29. }
  30. char last = s[s.Length - 1];
  31. if( last != '\n'){
  32. s = s + '\n';
  33. }
  34. var s3 = s.Split('\n');
  35. n = s3.Length - 1;
  36. for (int i = 0; i < n; i += 7)
  37. {
  38. string k;
  39. if (i + 7 > n)
  40. {
  41. k = string.Join("\n", s3, i, n - i);
  42. //Console.WriteLine("*" + k);
  43. }
  44. else
  45. {
  46. k = String.Join("\n", s3, i, 7);
  47. //visual.Text = k;
  48. //Console.WriteLine(k);
  49. }
  50. PrintRawLabel(k, fn);
  51. }
  52. }
  53. //public int c;
  54. public static void Print(string s)
  55. {
  56. PrintDialog printDialog = new PrintDialog();
  57. printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
  58. //string line1 = "x.23 2.42 5.23 23.23 23.42 23.42 23.42\n";
  59. Run run = new Run(s);
  60. /*
  61. Viewbox vb = new Viewbox();
  62. vb.Stretch = Stretch.Uniform;
  63. vb.VerticalAlignment = VerticalAlignment.Top;
  64. vb.HorizontalAlignment = HorizontalAlignment.Left;
  65. */
  66. // Wrap it in a TextBlock.
  67. TextBlock visual = new TextBlock(run);
  68. //RotateTransform rt = new RotateTransform();
  69. //rt.Angle = 90;
  70. //visual.LayoutTransform = rt;
  71. //visual.RenderTransform = rt;
  72. visual.Margin = new Thickness(10);
  73. // Allow wrapping to fit the page width.
  74. visual.TextWrapping = TextWrapping.WrapWithOverflow;
  75. visual.FontSize = Int32.Parse(Utils.getSetting("fontSize"));
  76. visual.FontFamily = new FontFamily(Utils.getSetting("fontName"));
  77. visual.FontWeight = FontWeights.Bold;
  78. //visual.FontStretch = FontStretches.ExtraCondensed;
  79. // Scale the TextBlock in both dimensions.
  80. //visual.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100);
  81. // Get the size of the page.
  82. Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
  83. // Trigger the sizing of the element.
  84. visual.Measure(pageSize);
  85. visual.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
  86. visual.HorizontalAlignment = HorizontalAlignment.Left;
  87. visual.VerticalAlignment = VerticalAlignment.Top;
  88. // Print the element.
  89. //vb.Child = visual;
  90. //printDialog.PrintVisual(visual, "A Scaled Drawing");
  91. printDialog.PrintVisual(visual, "A Scaled Drawing");
  92. }
  93. static void PrintRawLabel(string s, int fontSize){
  94. //
  95. PrintDialog printDialog = new PrintDialog();
  96. printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
  97. Run run = new Run(s);
  98. // Wrap it in a TextBlock.
  99. TextBlock visual = new TextBlock(run);
  100. //visual.FontFamily = new FontFamily("Cordia New");
  101. visual.FontSize = fontSize;
  102. visual.Margin = new Thickness(15, 0, 15, 0);
  103. // Allow wrapping to fit the page width.
  104. visual.TextWrapping = TextWrapping.Wrap;
  105. visual.FontFamily = new FontFamily(Utils.getSetting("fontName"));
  106. //visual.FontSize = Int32.Parse(Util.getSetting("fontSize"));
  107. visual.FontWeight = FontWeights.Bold;
  108. visual.FontStretch = FontStretches.ExtraCondensed;
  109. // Scale the TextBlock in both dimensions.
  110. Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
  111. visual.Measure(pageSize);
  112. visual.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
  113. printDialog.PrintVisual(visual, "InspectionLabel");
  114. }
  115. }
  116. }