问题描述
在 Xamarin.Forms 中,我想将我的 xaml 页面 UI(有时我的页面在有更多内容时可滚动)转换为 PDF.我试过 PDFSharp (https://github.com/akgulebubekir/PDFSharp.Xamarin.Forms) 开源.但它仅适用于 UWP,并且在 iOS 和 Android 中存在一些问题.
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.
那么是否有任何免费的开源插件可以在所有三个平台中将 XAML UI 转换为 PDF?如果开源不可用,有没有其他方法或工作可以在 android、ios & 中实现它?UWP?
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?
提前致谢.
推荐答案
我在这方面遇到了一些麻烦,并设法使用 UIkit 和 PdfKit 工具来解决.
I had a bit of trouble with this and managed to do it using UIkit and PdfKit tools.
这是一个例子:
使用UIKit;使用 PdfKit;
//计算滚动视图高度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"));`
这篇关于如何将 Xamarin.Forms XAML UI 页面转换为 PDF 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!