I was going through the following link:
https://www.syncfusion.com/kb/10697/how-to-convert-xaml-to-pdf-in-xamarin-using-c
This code works fine as long as what I need from the xaml fits in the screen without scrolling. But i want my all element in a ScrollView to be save in my pdf. I tried out the code in the Xamarin forums link but was not able to get it to work https://forums.xamarin.com/discussion/146465/how-to-convert-the-all-content-in-scroll-view-to-bitmap-in-xamarin-forum
this is my code
private async void button_Clicked(object sender, EventArgs e)
{
MemoryStream stream = new MemoryStream();
//Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
//Add page to the PDF document.
PdfPage page = document.Pages.Add();
//Create graphics instance.
PdfGraphics graphics = page.Graphics;
Stream imageStream = null;
if (Device.RuntimePlatform == Device.Android )
{
byte[] data;
data = await Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().CaptureAsync();
imageStream = new MemoryStream(data);
}
else
{
imageStream = new MemoryStream(DependencyService.Get<ISave>().CaptureAsync().Result);
}
//Load the image
PdfBitmap image = new PdfBitmap(imageStream);
//Draw the image
graphics.DrawImage(image, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
//Save the document into memory stream
document.Save(stream);
}
stream.Position = 0;
if (Device.RuntimePlatform == Device.Android )
{
Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XAMLtoPDF.pdf", "application/pdf", stream);
}
else
{
Xamarin.Forms.DependencyService.Get<ISave>().Save("XAMLtoPDF.pdf", "application/pdf", stream);
}
}
and to save
public void Save(string fileName, String contentType, MemoryStream s)
{
string root = null;
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//root = "/interner Speicher/Download/";
Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, fileName);
if (file.Exists()) file.Delete();
try
{
FileOutputStream outs = new FileOutputStream(file);
outs.Write(s.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception e)
{
}
if (file.Exists())
{
Android.Net.Uri path = Android.Net.Uri.FromFile(file);
string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(path, mimeType);
Android.App.Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}
}
and to converted the current view to bitmap in Xamarin Forms
public System.Threading.Tasks.Task<byte[]> CaptureAsync()
{
// scrollversion
var activity = Forms.Context as Activity;
ViewGroup content = activity.FindViewById<ViewGroup>(Android.Resource.Id.Content);
ViewGroup frameLayout = (ViewGroup)content.GetChildAt(0);
ViewGroup relativeLayout = (ViewGroup)frameLayout.GetChildAt(0);
ViewGroup platFormRenderer = (ViewGroup)relativeLayout.GetChildAt(0);
ViewGroup myNavigationPageRenderer = (ViewGroup)platFormRenderer.GetChildAt(0);
ViewGroup pageRenderer = (ViewGroup)myNavigationPageRenderer.GetChildAt(0);
ViewGroup scrollViewRenderer = (ViewGroup)pageRenderer.GetChildAt(0);// --> scrollViewRenderer
//Adjust this to find your own scrollViewRenderer
Android.Widget.ScrollView z = scrollViewRenderer as Android.Widget.ScrollView;
z.SetBackgroundColor(Android.Graphics.Color.White);
int totalHeight = z.GetChildAt(0).Height;
int totalWidth = z.GetChildAt(0).Width;
Bitmap bitmap = getBitmapFromView(z, (int)totalHeight, (int)totalWidth);
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);
bitmapData = stream.ToArray();
}
return Task.FromResult(bitmapData);
}
public Bitmap getBitmapFromView(Android.Views.View view, int totalHeight, int totalWidth)
{
Bitmap returnedBitmap = Bitmap.CreateBitmap(totalWidth, totalHeight, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.Background;
if (bgDrawable != null)
bgDrawable.Draw(canvas);
else
canvas.DrawColor(Android.Graphics.Color.White);
canvas.Save();
view.Draw(canvas);
canvas.Restore();
return returnedBitmap;
}
In the code above in Part z.SetBackgroundColor(Android.Graphics.Color.White); i am getteing System.NullReferenceException: 'Object reference not set to an instance of an object.' my z value is null Am I missing something? Thank you in advance!