【问题标题】:How to convert Xamarin.Forms XAML UI page to PDF file?如何将 Xamarin.Forms XAML UI 页面转换为 PDF 文件?
【发布时间】:2020-02-17 23:36:11
【问题描述】:

在 Xamarin.Forms 中,我想将我的 xaml 页面 UI(有时我的页面在有更多内容时可滚动)转换为 PDF。我已经尝试过 PDFSharp (https://github.com/akgulebubekir/PDFSharp.Xamarin.Forms) 开源。但它仅适用于 UWP,并且在 iOS 和 Android 中存在一些问题。

那么是否有任何免费的开源插件可用于在所有三个平台中将 XAML UI 转换为 PDF?如果开源不可用,是否有任何其他方法或解决方法可以在 android、ios 和 UWP 中实现它?

提前致谢。

【问题讨论】:

  • 您想将当前的应用程序屏幕导出/发送/保存为 PDF 文件吗?为什么需要 PDF?可以截图吗?
  • 它说它应该适用于所有平台。你遇到什么麻烦了?也许值得解决这些问题?
  • @EvZ,我想保存为 PDF 供以后使用(电子邮件、打印、共享等)。我只想要 PDF 格式的最终​​结果。我还尝试截取屏幕截图,然后将该位图图像转换为 PDF。但我的屏幕有可滚动的内容。在这种情况下,它不起作用。
  • 如何使用动画缩放视图以使其适合屏幕,截取屏幕截图并恢复比例?
  • @François,如果我们向外扩展,内容的宽度也会减小。但内容只能垂直滚动。我也不知道内容可以滚动多长时间。

标签: xaml pdf xamarin xamarin.forms pdf-conversion


【解决方案1】:

我在这方面遇到了一些麻烦,并设法使用 UIkit 和 PdfKit 工具做到了。

这是一个例子:

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"));`

【讨论】:

    猜你喜欢
    • 2011-03-16
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2011-07-01
    • 2012-04-04
    • 2011-01-13
    相关资源
    最近更新 更多