问题
I'm trying to render a list view using RenderTargetBitmap. The code below only renders one page. It doesn't add pages to the downloaded pdf according to the list view. This code takes snapshot of what is shown on the page but not whole list. I followed this link: XAML To PDF Conversion
This is my code behind file:
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace XAMLtoPDF_Sample
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public class ItemsListPDF
{
public string CarrierName { get; set; }
public string PackageID { get; set; }
public int Bill { get; set; }
public string Location { get; set; }
public string ItemType { get; set; }
public string Quantity { get; set; }
public string DeliverTo { get; set; }
public string Sender { get; set; }
public string CreationDate { get; set; }
public string PONumber { get; set; }
public string ControlNumber { get; set; }
}
public MainPage()
{
this.InitializeComponent();
var item1 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item2 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item3 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item4 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item5 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item6 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item7 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item8 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item9 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item10 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item11 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var item12 = new ItemsListPDF { CarrierName = "asdasd", PackageID = "erwerwer", Bill = 1, Location = "er wrrty", ItemType = "xcvxcv", Quantity = "4", DeliverTo = "kiran", Sender = "tushar", CreationDate = "12/12/19", PONumber = "1", ControlNumber = "1" };
var itemsList = new List<ItemsListPDF>();
itemsList.Add(item1);
itemsList.Add(item1);
itemsList.Add(item2);
itemsList.Add(item3);
itemsList.Add(item4);
itemsList.Add(item5);
itemsList.Add(item6);
itemsList.Add(item7);
itemsList.Add(item8);
itemsList.Add(item9);
itemsList.Add(item10);
itemsList.Add(item11);
itemsList.Add(item12);
PDFItemsList.ItemsSource = itemsList;
}
private async void button_Click(object sender, RoutedEventArgs e)
{
//Creating new PDF Document
PdfDocument document = new PdfDocument();
//Initializing to render to Bitmap
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var renderTargetBitmap = new RenderTargetBitmap();
//Create the Bitmpap from xaml page
await renderTargetBitmap.RenderAsync(SyncfusionForm);
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 > 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));
}
//Save the Pdf document
MemoryStream docStream = new MemoryStream();
document.Save(docStream);
document.Close(true);
Save(docStream, "Sample.pdf");
}
#region Helper Methods
async void Save(MemoryStream 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 = "Sample";
savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".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.SetLength(0);
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);
}
}
}
#endregion
}
}
This is the Xaml code:
<Page
x:Class="XAMLtoPDF_Sample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XAMLtoPDF_Sample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="SyncfusionForm" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--<TextBlock Text="First Name
" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="64,91,0,0" RenderTransformOrigin="-3.711,-1.5" Width="130" Foreground="#FF1038EC" Height="30"/>
<TextBlock Text="Last Name" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.842,4.5" Margin="64,128,0,0" Foreground="#FF1038EC"/>
<TextBox HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" VerticalAlignment="Top" Width="212" Margin="293,128,0,0" RenderTransformOrigin="0.351,0.761"/>
<TextBox HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" VerticalAlignment="Top" Width="211" Margin="292,91,0,0"/>
<TextBlock Text="Email" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.395,6" Margin="64,163,0,0" Foreground="#FF1038EC"/>
<TextBlock Text="Mobile No
" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="64,200,0,0" Width="84" Foreground="#FF1038EC" RenderTransformOrigin="0.637,0.548" Height="30"/>
<TextBox HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" VerticalAlignment="Top" Width="211" Margin="292,205,0,0"/>
<TextBox HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" VerticalAlignment="Top" Width="211" Margin="294,165,0,0" />
<TextBlock Text="Education Grade
" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="51,295,0,0" Width="120" Foreground="#FFAA0909" RenderTransformOrigin="0.439,-0.619" Height="26"/>
<TextBlock Text="General Information" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="2.211,2.346" Margin="51,65,0,0" Foreground="#FFAA0909" Width="156"/>
<TextBlock Text="Highest Qualification" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="51,335,0,0" RenderTransformOrigin="0.842,-1.231" Foreground="#FF1038EC"/>
<CheckBox Content="Post Graduate" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="64,406,0,0" Foreground="#FF1038EC" RenderTransformOrigin="0.5,0.5" Height="20" Width="126">
</CheckBox>
<CheckBox Content="College" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="367,370,0,0" Foreground="#FF1038EC" Width="95" Height="24"/>
<CheckBox Content="Bachelor degree" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="236,370,0,0" Foreground="#FF1038EC" Height="24" Width="125"/>
<CheckBox Content="Associate degree" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="64,370,0,0" Width="143" Foreground="#FF1038EC" Height="24"/>
<CheckBox Content="MBA" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="237,411,0,0" Foreground="#FF1038EC" RenderTransformOrigin="-0.75,0.503" Height="26" Width="62">
</CheckBox>-->
<Button x:Name="button" Click="button_Click" Content="Submit" HorizontalAlignment="Center" VerticalAlignment="Top" Height="31"/>
<ListView Grid.Row="1" x:Name="PDFItemsList" Height="Auto" VerticalAlignment="Top" Margin="0,40,0,0" Foreground="Black" Background="White" Visibility="Visible" IsItemClickEnabled="False">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<ContentControl>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding CarrierName}" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding PackageID}" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Bill}" Foreground="Black" Grid.Row="2" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Location}" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding ItemType}" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Quantity}" Foreground="Black" Grid.Row="2" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="2" HorizontalAlignment="Right">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding DeliverTo}" HorizontalAlignment="Right" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Sender}" HorizontalAlignment="Right" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/>
<TextBlock Text="" Foreground="Black" HorizontalAlignment="Right" Grid.Row="2" Margin="20,20,0,0"/>
<TextBlock Text="Signature" HorizontalAlignment="Right" Foreground="Black" FontWeight="ExtraLight" Grid.Row="3" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="3" HorizontalAlignment="Right">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding CreationDate}" HorizontalAlignment="Right" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding PONumber}" HorizontalAlignment="Right" Grid.Row="1" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding ControlNumber}" HorizontalAlignment="Right" Grid.Row="2" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="" Foreground="Black" Grid.Row="3" Margin="20,20,0,0"/>
<TextBlock Text="Print Name Here" HorizontalAlignment="Right" Foreground="Black" FontWeight="ExtraLight" Grid.Row="4" Margin="20,20,0,0"/>
</Grid>
</Grid>
</ContentControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Thanks in advance.
来源:https://stackoverflow.com/questions/55489563/how-to-render-a-large-items-listview-into-rendertargetbitmap