简体   繁体   中英

How to convert Xamarin.Forms XAML UI page to PDF file?

In Xamarin.Forms, I want to convert my xaml page UI (sometimes my page is scrollable when having more content) into the PDF. I have tried the PDFSharp ( https://github.com/akgulebubekir/PDFSharp.Xamarin.Forms ) open source. But it works only on UWP and having some issues in iOS and Android.

So is there any free open source plugin available to convert XAML UI into PDF in all three platforms? If open source not available, is there any other way or work around to achieve it in android, ios & UWP?

Thanks in advance.

I had a bit of trouble with this and managed to do it using UIkit and PdfKit tools.

Here is an exemple:

using UIKit; using PdfKit;

//Calculate scroll View height double xamlHeight = XamlFullPage.Height; int numberOfPages = (int)Math.Ceiling(xamlHeight / Application.Current.MainPage.Height);

        // Create a new PDF document
        PdfDocument document = new PdfDocument();

        for (int i = 0; i<numberOfPages; i++) //while the all the page have not been taken into account
        {
            await SummaryScrollView.ScrollToAsync(0, i*Application.Current.MainPage.Height, false).ConfigureAwait(false); //find the beginnig of the current page
            //Captures the XAML page as image and returns the image in memory stream
            var image = UIScreen.MainScreen.Capture();
            // Create a page with the printscreen
            PdfPage page = new PdfPage(image);
            //insert page in the i position
            document.InsertPage(page, i);
            page.Dispose();
        }

        //Write file in temp foleder
        document.Write(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TuitionFees" + fileName + ".pdf"));`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM