title: How to convert a xaml page to pdf file and show it in a pdf viewer inside a UWP application?
body: <p>I am trying to use Syncfusion PDF viewer but in manner to use that I have to eventually save a xaml page as a pdf file. How do I do that?</p>
  
  <p>I followed this link but it doesn't give me what I want. <a href="https://www.syncfusion.com/kb/8060/how-to-convert-xaml-to-pdf-in-uwp" rel="nofollow noreferrer">XAML to PDF Conversion</a></p>
  
  <p>I'm trying below code to save a xaml page [<strong>new GenericManifestPDF(_manifestPDFDataModel);</strong>] from my view model class and getting this error: '<strong>Value does not fall within the expected range</strong>' in this line <strong>await renderTargetBitmap.RenderAsync(new GenericManifestPDF(_manifestPDFDataModel));</strong></p>
  
  <pre><code>   PdfDocument document = new PdfDocument();
     //Add a new page
     PdfPage page = document.Pages.Add();
     //Initialize render to bitmap
     var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
     var renderTargetBitmap = new RenderTargetBitmap();
     //Create a Bitmap from XAML page
     await renderTargetBitmap.RenderAsync(new GenericManifestPDF(_manifestPDFDataModel));
     var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
     //Save the XAML in bitmap image
     using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
     {
     var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
     encoder.SetPixelData(
     BitmapPixelFormat.Bgra8,
     BitmapAlphaMode.Ignore,
     (uint)renderTargetBitmap.PixelWidth,
     (uint)renderTargetBitmap.PixelHeight,
     logicalDpi,
     logicalDpi,
     pixelBuffer.ToArray());
     await encoder.FlushAsync();
     //Load and draw the bitmap image in PDF
     PdfImage img = PdfImage.FromStream(stream.AsStream());
     if (img.Width &gt; img.Height)
        document.PageSettings.Orientation = PdfPageOrientation.Portrait;
            else                                                                                      document.PageSettings.Orientation = PdfPageOrientation.Landscape;
         var section = document.Sections.Add();
         section.PageSettings.Size = new SizeF(img.Width, img.Height);
         page = section.Pages.Add();
         page.Graphics.DrawImage(img, new RectangleF(0, 0, img.Width, img.Height));
                                          }
          //Save the document
          MemoryStream docStream = new MemoryStream();
          document.Save(docStream);
          //Close the document
          document.Close(true);
          Save(docStream, "SampleManifest.pdf");
  </code></pre>
  
  <p>This is Save method:</p>
  
  <pre><code> public async void Save(Stream stream, string filename)
      {
          stream.Position = 0;
          StorageFile stFile;
          if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
          {
              FileSavePicker savePicker = new FileSavePicker();
              savePicker.DefaultFileExtension = ".pdf";
              savePicker.SuggestedFileName = "Output";
              savePicker.FileTypeChoices.Add("Adobe PDF Document", new List&lt;string&gt;() { ".pdf" });
              stFile = await savePicker.PickSaveFileAsync();
          }
          else
          {
              StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
              stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
          }
          if (stFile != null)
          {
              Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
              Stream st = fileStream.AsStreamForWrite();
              st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
              st.Flush();
              st.Dispose();
              fileStream.Dispose();
              MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File created.");
              UICommand yesCmd = new UICommand("Yes");
              msgDialog.Commands.Add(yesCmd);
              UICommand noCmd = new UICommand("No");
              msgDialog.Commands.Add(noCmd);
              IUICommand cmd = await msgDialog.ShowAsync();
              if (cmd == yesCmd)
              {
                  // Launch the retrieved file
                  bool success = await Windows.System.Launcher.LaunchFileAsync(stFile);
              }
          }
      }
  </code></pre>
  
  <p>This is how I'm binding the data in the upper mentioned page constructor:</p>
  
  <pre><code>public GenericManifestPDF(ManifestPDFDataModel PDFDataModel)
      {
          this.InitializeComponent();
          Function.Text = "Function :" + PDFDataModel.Function;
          Counts.Text = "Count :" + PDFDataModel.Counts;
          PrintedValue.Text = "Printed :" + PDFDataModel.CreationDate;
          RouteValue.Text = "Route :" + PDFDataModel.Route;
          BatchIDValue.Text = "Batch :" + PDFDataModel.BatchID;
          BatchBarcodeText.Text = "Batch Barcode :" + PDFDataModel.Barcode;
          IBarcodeWriter writer = new BarcodeWriter
          {
              Format = BarcodeFormat.CODE_39,
              Options = new ZXing.Common.EncodingOptions
              {
                  Height = 100,
                  Width = 450
              }
          };
  
          var result = writer.Write(PDFDataModel.Barcode);
          BarcodeImage.Source = result;
          PDFItemsList.ItemsSource = PDFDataModel.ItemsPDFList;
      }
  </code></pre>
  
  <p>This is the xaml code of the page:</p>
  
  <pre><code>&lt;Grid x:Name="ManifestPDFGrid"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="30*"/&gt;
          &lt;RowDefinition Height="70*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid BorderBrush="Black" BorderThickness="2" Margin="30"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="80*"/&gt;
              &lt;RowDefinition Height="20*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition/&gt;
                      &lt;RowDefinition/&gt;
                      &lt;RowDefinition/&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock Text="Manifest PDF Report" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/&gt;
                  &lt;TextBlock x:Name="Function" Foreground="Black" Grid.Row="1" FontWeight="Bold" Margin="20,20,0,0"/&gt;
                  &lt;TextBlock x:Name="Counts" Grid.Row="2" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/&gt;
              &lt;/Grid&gt;
              &lt;Grid Grid.Column="1"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition/&gt;
                      &lt;RowDefinition/&gt;
                      &lt;RowDefinition/&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock x:Name="PrintedValue" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/&gt;
                  &lt;TextBlock x:Name="RouteValue" Foreground="Black" Grid.Row="1" FontWeight="Bold" Margin="20,20,0,0"/&gt;
                  &lt;TextBlock x:Name="BatchIDValue" Grid.Row="2" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/&gt;
              &lt;/Grid&gt;
              &lt;Grid Grid.Column="2"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition/&gt;
                      &lt;RowDefinition/&gt;
                      &lt;RowDefinition/&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Image x:Name="BarcodeImage" Margin="20,0,0,0" HorizontalAlignment="Center"/&gt;
                  &lt;TextBlock x:Name="BatchBarcodeText" Grid.Row="1" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Row="2"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBlock Text="Delivered By :" Foreground="Black" Margin="20,0,0,0" VerticalAlignment="Center"/&gt;
              &lt;TextBlock Text="Batch Delivery Time :" Foreground="Black" Grid.Column="1" Margin="20,0,0,0" VerticalAlignment="Center"/&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
      &lt;Grid Grid.Row="2"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="10*"/&gt;
              &lt;RowDefinition Height="90*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBlock FontWeight="Bold" Text="Carrier/PKG/BILL#" Foreground="Black" TextDecorations="Underline" Margin="20,20,0,0"/&gt;
              &lt;TextBlock FontWeight="Bold" Grid.Column="1" Foreground="Black" TextDecorations="Underline" Text="Location/Item Type" Margin="20,20,0,0"/&gt;
              &lt;TextBlock FontWeight="Bold" Grid.Column="2" Foreground="Black" TextDecorations="Underline" Text="Deliver To/ Sender" Margin="20,20,0,0"/&gt;
              &lt;TextBlock FontWeight="Bold" Grid.Column="3" Foreground="Black" TextDecorations="Underline" Text="DATE/TIME/PO/Control#" Margin="20,20,0,0"/&gt;
          &lt;/Grid&gt;
          &lt;ListView Grid.Row="1" x:Name="PDFItemsList" IsItemClickEnabled="False"&gt;
              &lt;ListView.ItemContainerStyle&gt;
                  &lt;Style TargetType="ListViewItem"&gt;
                      &lt;Setter Property="BorderThickness" Value="0,0,0,1" /&gt;
                      &lt;Setter Property="BorderBrush" Value="Black"/&gt;
                      &lt;Setter Property="Width" Value="Auto"/&gt;
                  &lt;/Style&gt;
              &lt;/ListView.ItemContainerStyle&gt;
              &lt;ListView.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ContentControl Style="{StaticResource EmptyContentControlStyle}"&gt;
                          &lt;Grid&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="1*"/&gt;
                                  &lt;ColumnDefinition Width="1*"/&gt;
                                  &lt;ColumnDefinition Width="1*"/&gt;
                                  &lt;ColumnDefinition Width="1*"/&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;TextBlock Text="{Binding CarrierName}" Foreground="Black" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding PackageID}" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding Bill}" Foreground="Black" Grid.Row="2" Margin="20,20,0,0"/&gt;
                              &lt;/Grid&gt;
                              &lt;Grid Grid.Column="1"&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;TextBlock Text="{Binding Location}" Foreground="Black" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding ItemType}" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding Quantity}" Foreground="Black" Grid.Row="2" Margin="20,20,0,0"/&gt;
                              &lt;/Grid&gt;
                              &lt;Grid Grid.Column="2" HorizontalAlignment="Right"&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;TextBlock Text="{Binding DeliverTo}" HorizontalAlignment="Right" Foreground="Black" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding Sender}" HorizontalAlignment="Right" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="" Foreground="Black" HorizontalAlignment="Right" TextDecorations="Underline" Grid.Row="2" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="Signature" HorizontalAlignment="Right" Foreground="Black" FontWeight="ExtraLight" Grid.Row="3" Margin="20,20,0,0"/&gt;
                              &lt;/Grid&gt;
                              &lt;Grid Grid.Column="3" HorizontalAlignment="Right"&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                      &lt;RowDefinition/&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;TextBlock Text="{Binding CreationDate}" HorizontalAlignment="Right" Foreground="Black" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding PONumber}" HorizontalAlignment="Right" Grid.Row="1" Foreground="Black" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="{Binding ControlNumber}" HorizontalAlignment="Right" Grid.Row="2" Foreground="Black" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="" Foreground="Black" TextDecorations="Underline" Grid.Row="3" Margin="20,20,0,0"/&gt;
                                  &lt;TextBlock Text="Print Name Here" HorizontalAlignment="Right" Foreground="Black" FontWeight="ExtraLight" Grid.Row="4" Margin="20,20,0,0"/&gt;
                              &lt;/Grid&gt;
                          &lt;/Grid&gt;
                      &lt;/ContentControl&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.ItemTemplate&gt;
          &lt;/ListView&gt;
      &lt;/Grid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>I'm able to save it in a pdf format but it's not showing properly, I think I need to change something here </p>
  
  <pre><code>  using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                                          {
                                              var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                                              encoder.SetPixelData(
                                              BitmapPixelFormat.Bgra8,
                                              BitmapAlphaMode.Ignore,
                                              (uint)renderTargetBitmap.PixelWidth,
                                              (uint)renderTargetBitmap.PixelHeight,
                                              logicalDpi,
                                              logicalDpi,
                                              pixelBuffer.ToArray());
                                              await encoder.FlushAsync();
                                              //Load and draw the bitmap image in PDF
                                              PdfImage img = PdfImage.FromStream(stream.AsStream());
                                              if (img.Width &gt; img.Height)
                                                  document.PageSettings.Orientation = PdfPageOrientation.Portrait;
                                              else
                                                  document.PageSettings.Orientation = PdfPageOrientation.Landscape;
                                              var section = document.Sections.Add();
                                              section.PageSettings.Size = new SizeF(img.Width, img.Height);
                                              PdfPage page = section.Pages.Add();
                                              page.Graphics.DrawImage(img, new RectangleF(0, 0, img.Width, img.Height));
                                          }
  </code></pre>
  
  <p>The only problem I'm facing now is how do I generate pdf pages according to the list view items. The code given above generates only 1 page every time so the list won't be visible fully.</p>
  
  <p>Thanks in advance.</p>