我有local html file
,我想将html保存到本地路径中已加载的Webview
。但是第一次总是保存空数据。
try
{
htmltopdfs = html;
var dir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/SVSTATIONERY/");
var file = new Java.IO.File(dir + "/" + fileName + ".pdf");
if (!dir.Exists())
dir.Mkdirs();
//else
// dir.Delete();
int x = 0;
while (file.Exists())
{
x++;
file = new Java.IO.File(dir + "/" + fileName + "( " + x + " )" + ".pdf");
}
//if (webpage == null)
var webpage = new Android.Webkit.WebView(MainActivity.context);
int width = 2102;
int height = 2970;
webpage.Layout(0, 0, width, height);
webpage.LoadDataWithBaseURL("", htmltopdfs, "text/html", "UTF-8", null);
webpage.SetWebViewClient(new WebViewCallBack(file.ToString()));
return file.ToString();
}
catch (Java.Lang.Exception e)
{
return e.Message;
}
Onpagefinished event
class WebViewCallBack : WebViewClient
{
string fileNameWithPath = null;
public WebViewCallBack(string path)
{
this.fileNameWithPath = path;
}
public override void OnPageFinished(Android.Webkit.WebView myWebview, string url)
{
PdfDocument document = new Android.Graphics.Pdf.PdfDocument();
PdfDocument.Page page = document.StartPage(new PdfDocument.PageInfo.Builder(2102, 3500, 1).Create());
myWebview.Draw(page.Canvas);
document.FinishPage(page);
Stream filestream = new MemoryStream();
FileOutputStream fos = new Java.IO.FileOutputStream(fileNameWithPath, false);
document.WriteTo(filestream);
fos.Write(((MemoryStream)filestream).ToArray(), 0, (int)filestream.Length);
fos.Close();
}
}
创建后,但第一次创建空白页到本地路径,然后多次创建后便可以正常工作。
这是我的有关将webview(加载本地html)打印为PDF的演示。
这里正在运行GIF()。
我在xamarin表单中的布局类似于以下代码。
<StackLayout>
<Button Text="save" Clicked="Button_Clicked"/>
<Entry x:Name="MyEntry" Text="5"></Entry>
<Label x:Name="MyLabel"></Label>
<!-- Place new controls here -->
<WebView x:Name="MywebView" HeightRequest="1000" Navigated="MywebView_Navigated"
WidthRequest="1000"/>
</StackLayout>
在Android成就中,我使用MessagingCenter和用于Webview的自定义渲染器来实现。
[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyWebviewRender))]
命名空间MyWebView.Droid{类MyWebviewRender:WebViewRenderer{上下文上下文;public MyWebviewRender(Context context):base(context){this.context =上下文;}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
MessagingCenter.Subscribe<string>(this, "pdf", (key) => {
var printManager = (PrintManager)context.GetSystemService(Context.PrintService);
var printAdapter = new MyPrintDocumentAdapter(context, Control);
var printJob = printManager.Print("MyPrintJob", printAdapter, null);
});
}
}
MyPrintDocumentAdapter.cs
public class MyPrintDocumentAdapter : PrintDocumentAdapter
{
View view;
Context context;
PrintedPdfDocument document;
float scale;
public MyPrintDocumentAdapter(Context context, View view)
{
this.view = view;
this.context = context;
}
public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
{
document = new PrintedPdfDocument(context, newAttributes);
CalculateScale(newAttributes);
//set the printed PDF attributes
var printInfo = new PrintDocumentInfo
.Builder("MyPrint.pdf")
.SetContentType(PrintContentType.Document)
.SetPageCount(10)
.Build();
callback.OnLayoutFinished(printInfo, true);
}
public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
{
PrintedPdfDocument.Page page = document.StartPage(0);
page.Canvas.Scale(scale, scale);
view.Draw(page.Canvas);
document.FinishPage(page);
WritePrintedPdfDoc(destination);
document.Close();
document.Dispose();
callback.OnWriteFinished(pages);
}
void CalculateScale(PrintAttributes newAttributes)
{
int dpi = Math.Max(newAttributes.GetResolution().HorizontalDpi, newAttributes.GetResolution().VerticalDpi);
int leftMargin = (int)(dpi * (float)newAttributes.MinMargins.LeftMils / 1000);
int rightMargin = (int)(dpi * (float)newAttributes.MinMargins.RightMils / 1000);
int topMargin = (int)(dpi * (float)newAttributes.MinMargins.TopMils / 1000);
int bottomMargin = (int)(dpi * (float)newAttributes.MinMargins.BottomMils / 1000);
int w = (int)(dpi * (float)newAttributes.GetMediaSize().WidthMils / 1000) - leftMargin - rightMargin;
int h = (int)(dpi * (float)newAttributes.GetMediaSize().HeightMils / 1000) - topMargin - bottomMargin;
scale = Math.Min((float)document.PageContentRect.Width() / w, (float)document.PageContentRect.Height() / h);
}
void WritePrintedPdfDoc(ParcelFileDescriptor destination)
{
var i=destination.FileDescriptor;
var javaStream = new Java.IO.FileOutputStream(destination.FileDescriptor);
var osi = new OutputStreamInvoker(javaStream);
using (var mem = new MemoryStream())
{
document.WriteTo(mem);
var bytes = mem.ToArray();
osi.Write(bytes, 0, bytes.Length);
}
}
}