Xamarin Android以pdf格式保存webview [英] Xamarin Android save webview in pdf
问题描述
我正在使用xamarin为Android开发一个应用程序。
I'm developing an app in xamarin for android.
目标是让用户填写文档并将其另存为PDF。
The objective is for the user to fill a document and save it as a PDF.
我尝试过这个过程:
HTML to PDF
我设计了一个cshtml文件,它包含与文档相同的结构,以及用户插入的输入。到目前为止一切顺利,它在webview中看起来很不错。我现在希望将其转换为PDF,但我似乎无法找到转换它的方法。
I have designed a cshtml file, that contains the same structure as the document, with the input that the user inserted. So far so good, it looks good in the webview. I now wish to convert this to PDF but I can't seem to find a way to convert it.
有没有办法将CSHTML转换为PDF,或以某种方式将webview的内容转换为PDF?
Is there a way to convert CSHTML to PDF, or somehow convert the content of the webview to PDF?
我尝试过一个名为iTextSharp的扩展来创建和操作PDF(我尝试将html转换为PDF文件,但html中使用的样式和图像不是正在应用)。
I have tried an extension called "iTextSharp" to create and manipulate PDF's (my attempt to convert html to a PDF file but the styles and images used in html aren't being applied).
最终目标是创建一个PDF文件,其结构与我拥有的HTML页面相同。
The final objective is to create a PDF file that has the same structure as an HTML Page that I own.
我能以任何方式完成此任务吗?非常欢迎想法
Any way I can accomplish this? Ideas are most welcome
推荐答案
我可以转换整个 WebView
页面内容使用以下代码的pdf。希望它可以帮到你。但是,我刚刚在Simulator中尝试了这个。
I could convert the whole WebView
page content to pdf with below code. Hope it may help you. However, I have just tried this in Simulator.
[Activity(Label = "DroidWebview", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
WebView myWebView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
myWebView = FindViewById<WebView>(Resource.Id.webview);
myWebView.Settings.JavaScriptEnabled = true;
myWebView.SetWebViewClient(new MyWebViewClient());
myWebView.LoadUrl("https://stackoverflow.com/questions/46978983/xamarin-android-save-webview-in-pdf");
Button myPrintButton = FindViewById<Button>(Resource.Id.myPrintButton);
myPrintButton.Click += (sender, e) =>
{
var printManager = (PrintManager)GetSystemService(PrintService);
string fileName = "MyPrint_" + Guid.NewGuid().ToString() + ".pdf";
var printAdapter = myWebView.CreatePrintDocumentAdapter(fileName);
PrintJob printJob = printManager.Print("MyPrintJob", printAdapter, new PrintAttributes.Builder().Build());
};
}
}
public class MyWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return false;
}
}
这篇关于Xamarin Android以pdf格式保存webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!