2

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.

Selvamz
  • 362
  • 3
  • 16
  • Do you want to export / send / save externally your current application screen as PDF file? Why do you need a PDF? Can it be a screenshot? – EvZ May 04 '18 at 10:38
  • It says it should work on all platforms. What trouble are you running into? Maybe it's worth solving those? – Gerald Versluis May 04 '18 at 10:58
  • @EvZ, I want to save as PDF for later use (email, print, share, etc.). I want my end result in PDF format only. I've also tried to take screenshot and then convert that bitmap image as PDF. But my screen has scrollable content. In this case, it doesn't works. – Selvamz May 04 '18 at 12:26
  • What about using animation to scale out the view so it fits on the screen, take the screenshot and revert the scale back? – François May 06 '18 at 09:49
  • @François, If we scale out, width of the content also get decreased. But content is only scrollable vertically. Also I don't how long the content is scrollable. – Selvamz May 08 '18 at 06:07
  • I think Scroll view had a content height that you can compare against scroll view height. – François May 08 '18 at 06:30
  • You could check [PDFSharp.Xamarin.Forms](https://github.com/akgulebubekir/PDFSharp.Xamarin.Forms), but it's pretty outdated, but check the repo and you might got some ideas to implement yourself – FabriBertani May 22 '23 at 18:32

1 Answers1

4

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

Here is an example:

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 all the page have not been taken into account
{
        await SummaryScrollView.ScrollToAsync(0, i*Application.Current.MainPage.Height, false).ConfigureAwait(false); //find the beginning of the current page
        //Captures the XAML page as an 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 folder
          document.Write(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TuitionFees" + fileName + ".pdf"));`



Bryce Friha
  • 186
  • 2
  • 11
Loque Ness
  • 41
  • 4