I am new to xamarin and I am using PdfSharp.Xamarin.Forms nuget to create a PDF in Xamarin forms for both Android and iOS. Problem is I cannot render ListView. They have mentioned about it, and need to write a renderer for it. But I have no idea how to create and bind it.
This is how I did it.
<Grid x:Name="mainGrid"> <ScrollView> <StackLayout Padding="4" Orientation="Vertical"> <!--<Image HeightRequest="80" Source="logojpeg.jpg" Margin="0,0,0,5"/>--> <Label FontSize="18" TextColor="Black" FontFamily="{StaticResource timesNewRomanBold}" HorizontalOptions="CenterAndExpand" Text="Monthly Motor Renew List of Jayasekara (900585) as at January, 2020"/> <Label FontSize="18" TextColor="Black" FontFamily="{StaticResource timesNewRomanBold}" HorizontalOptions="CenterAndExpand" Text="Report generated on 27 December, 2019" Margin="0,0,0,5"/> <ListView x:Name="renewListView" Footer="" pdf:PdfRendererAttributes.ListRendererDelegate="{StaticResource PDFSampleListRendererDelegate}" BackgroundColor="White" SeparatorVisibility="None" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCell IsEnabled="false"> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </ScrollView> </Grid>
Code Behind
public partial class MotorRenewalFinalPrint : ContentPage { public MotorRenewalFinalPrint() { InitializeComponent(); } public MotorRenewalFinalPrint (List<MotorRenewalPrintData> newdd) { InitializeComponent (); Title = "Save as PDF"; renewListView.ItemsSource = newdd; } private void pdf_Clicked(object sender, EventArgs e) { var pdf = PDFManager.GeneratePDFFromView(mainGrid); var fileManager = DependencyService.Get<IFileIO>(); string filePath = Path.Combine(fileManager.GetMyDocumentsPath(), "formpdf.pdf"); DependencyService.Get<IPdfSave>().Save(pdf, filePath); DependencyService.Get<IPDFPreviewProvider>().TriggerPreview(filePath); } }
PDFSampleListRendererDelegate renderer
public class PDFSampleListRendererDelegate : PdfListViewRendererDelegate { public override void DrawCell(ListView listView, int section, int row, XGraphics page, XRect bounds, double scaleFactor) { base.DrawCell(listView, section, row, page, bounds, scaleFactor); } public override void DrawFooter(ListView listView, int section, XGraphics page, XRect bounds, double scaleFactor) { base.DrawFooter(listView, section, page, bounds, scaleFactor); } public override double GetFooterHeight(ListView listView, int section) { return base.GetFooterHeight(listView, section); } }
Did you define the key for PDFSampleListRendererDelegate
class in ContentPage.Resources ?
Add the following code into xmal .
// xmlns:local="clr-namespace:YourNameSpace" <ContentPage.Resources> <ResourceDictionary> <local:PDFSampleListRendererDelegate x:Key="PDFSampleListRendererDelegate" /> </ResourceDictionary> </ContentPage.Resources>
Answers
We should draw the cell in
PDFSampleListRendererDelegate
, implement the methods in the class .Do not implemnt
ItemTemplate
in xamlRefer https://github.com/akgulebubekir/PDFSharp.Xamarin.Forms#listview-rendering.
I am getting Xamarin.Forms.Xaml.XamlParseException: Position 149:35. StaticResource not found for key PDFSampleListRendererDelegate error . I have used PDFSampleListRendererDelegate as YourRendererDelegate
Did you define the key for
PDFSampleListRendererDelegate
class in ContentPage.Resources ?Add the following code into xmal .
Error has went now. But how should I map my list data to the ItemSource using this renderer? I have created a sample project and updated to the github. If you can please modify it with binding data. It would be grateful. ListView has two labels
link : https://github.com/dush135/PdfSharpTestCreate