title: Printing from Win RT Application (Windows 8.1 App)
body: <p>I am developing a POS application in windows 8.1 (Universal App).
  Order receipt will be printed from the application and even I am able to do so.</p>
  
  <p><strong>Printer</strong> - EPSON TM-U220D</p>
  
  <p><strong>Input</strong> - A grid is being created pragmatically with dynamic content within the ViewModel. So this grid is the input for printer</p>
  
  <p><strong>Output</strong> - In the print preview (Attahced pic 1) all looks good but when the receipt is actually printed then content is cut off from the end   (Attahced pic 2)</p>
  
  <p>PIC 1
  <a href="https://i.stack.imgur.com/isICS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/isICS.png" alt="enter image description here"></a></p>
  
  <p>PIC 2
  <a href="https://i.stack.imgur.com/NQE9E.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQE9E.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Observations</strong> -</p>
  
  <ol>
  <li>If I print a normal text file manually using print command (Right click file and then print), then all content is printed perfectly.</li>
  <li>If I print that SAME content, from the application, by creating Dynamic grid, then with Small font size print is good but with bit bigger font size content again cuts off.</li>
  </ol>
  
  <p><strong>Tried</strong> -</p>
  
  <ol>
  <li>Optimized the code for Creating Gird, by specifying height</li>
  </ol>
  
  <p><strong>Questions</strong>-</p>
  
  <ol>
  <li>If preview is all good then why not output</li>
  <li>Did anyone tried using ePOS-Print_SDK_141020E for Windows Store app?</li>
  </ol>
  
  <p><strong>Generate Dynamic Grid Code</strong> </p>
  
  <pre><code> private void AddRows(Grid grid, int count)
      {
          for (int i = 0; i &lt; count; i++)
          {
              RowDefinition row = new RowDefinition();
              row.Height = GridLength.Auto;
              grid.RowDefinitions.Add(row);
          }
      }
  
      private void AddColumns(Grid grid, int count)
      {
          for (int i = 0; i &lt; count; i++)
          {
              grid.ColumnDefinitions.Add(new ColumnDefinition());
  
          }
      }
  
      private TextBlock CreateTextBlock(string text, Color color, FontWeight fw, double fs = 10, int thick = 5)
      {
          if (color == null) color = Colors.Black;
          TextBlock txtBlock1 = new TextBlock();
          txtBlock1.Text = text;
          txtBlock1.FontSize = fs;
          txtBlock1.FontWeight = fw;
          txtBlock1.Foreground = new SolidColorBrush(color);
          txtBlock1.VerticalAlignment = VerticalAlignment.Center;
          txtBlock1.Margin = new Thickness(thick);
          return txtBlock1;
      }
  
  
      private async Task&lt;Grid&gt; CreateDynamicWPFGrid()
      {
          Grid ParentGrid = new Grid();
          AddRows(ParentGrid, 8);
          /* Start First Grid*/
          Grid DynamicGrid = new Grid();
          DynamicGrid.Width = 230;
          DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
          DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
          DynamicGrid.Margin = new Thickness(24, 0, 0, 0);
  
          AddColumns(DynamicGrid, 2);
          AddRows(DynamicGrid, 3);
  
          TextBlock txtBlock1 = CreateTextBlock(DateTime.Now.ToString("M/d/yy"), Colors.Black, FontWeights.Normal);
          Grid.SetRow(txtBlock1, 0);
          Grid.SetColumn(txtBlock1, 1);
          .
          .
          .
          .
          Return ParentGrid;
      }
  </code></pre>
  
  <p><strong>Printer Events Code</strong></p>
  
  <pre><code>       //Register Print Contract
      async Task RegisterPrintContract()
      {
          PrintManager manager = PrintManager.GetForCurrentView();
          manager.PrintTaskRequested += OnPrintTaskRequested;
          await PrintManager.ShowPrintUIAsync();
      }
      //Unregister Print Contract
      void UnregisterPrintContract()
      {
          PrintManager printMan = PrintManager.GetForCurrentView();
          printMan.PrintTaskRequested -= OnPrintTaskRequested;
      }
      void OnPrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
      {
          // If I need to be asynchronous, I can get a deferral. I don't *need*  
          // to do this here, I'm just faking it.  
          var deferral = args.Request.GetDeferral();
  
          PrintTask printTask = args.Request.CreatePrintTask("My Print Job", OnPrintTaskSourceRequestedHandler);
  
          printTask.Completed += OnPrintTaskCompleted;
  
          deferral.Complete();
      }
      void OnPrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
      {
          // TODO: Tidy up.  
          this._document = null;
          this._pages = null;
      }
      async void OnPrintTaskSourceRequestedHandler(PrintTaskSourceRequestedArgs args)
      {
          var deferral = args.GetDeferral();
  
          await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =&gt;
            {
                this._document = new PrintDocument();
                this._document.Paginate += OnPaginate;
                this._document.GetPreviewPage += OnGetPreviewPage;
                this._document.AddPages += OnAddPages;
  
                // Tell the caller about it.  
                args.SetSource(this._document.DocumentSource);
            });
          deferral.Complete();
      }
      void OnAddPages(object sender, AddPagesEventArgs e)
      {
          // Loop over all of the preview pages and add each one to  add each page to be printied
          // We should have all pages ready at this point...
          foreach (var page in this._pages)
          {
              //this._pages[page.Key]
              this._document.AddPage(this._pages[page.Key]);
          }
  
          PrintDocument printDoc = (PrintDocument)sender;
          // Indicate that all of the print pages have been provided
          printDoc.AddPagesComplete();
      }
  
      async void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
      {
          Grid x = await CreateDynamicWPFGrid();
          await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =&gt;
            {
                // NB: assuming it's ok to keep all these pages in  
                // memory. might not be the right thing to do  
                // of course.  
                if (this._pages == null)
                {
                    this._pages = new Dictionary&lt;int, UIElement&gt;();
                }
                if (!this._pages.ContainsKey(e.PageNumber))
                {
  
                    this._pages[e.PageNumber] = x;
                }
                if (this._document == null)
                    this._document = new PrintDocument();
                this._document.SetPreviewPage(e.PageNumber, this._pages[e.PageNumber]);
            }
          );
      }
      async void OnPaginate(object sender, PaginateEventArgs e)
      {
          await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =&gt;
            {
                // I have one page and that's *FINAL* !  
                this._document.SetPreviewPageCount(e.CurrentPreviewPageNumber, PreviewPageCountType.Final);
            });
      }
  </code></pre>