Forum Xamarin.Forms

Cannot display ListView data when creating PDF using PdfSharp.Xamarin.Forms

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);
    }
}

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    We should draw the cell in PDFSampleListRendererDelegate , implement the methods in the class .

    Do not implemnt ItemTemplate in xaml

    <ListView pdf:ListRendererDelegate="{StaticResource YourRendererDelegate}" .../>
    

    Refer https://github.com/akgulebubekir/PDFSharp.Xamarin.Forms#listview-rendering.

  • dushan135dushan135 Member ✭✭
    edited January 2020

    I am getting Xamarin.Forms.Xaml.XamlParseException: Position 149:35. StaticResource not found for key PDFSampleListRendererDelegate error . I have used PDFSampleListRendererDelegate as YourRendererDelegate

  • dushan135dushan135 Member ✭✭
    edited January 2020

    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

  • dushan135dushan135 Member ✭✭
    Please
  • dushan135dushan135 Member ✭✭
    Can you tell me how to bind/pass the list data to PDFSampleListRendererDelegate ??
Sign In or Register to comment.