Nessuna descrizione

Printer.cs 4.2KB

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