Exemple #1
0
        public override void OnLayoutFinished(PrintDocumentInfo info, bool changed)
        {
            //using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments))
            using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads))
            {
                if (!_dir.Exists())
                {
                    _dir.Mkdir();
                }

                // var path = _dir.Path + "/" + FileName + ".pdf";
                var path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads, FileName + ".pdf");
                var file = new Java.IO.File(path);
                int iter = 0;
                while (file.Exists())
                {
                    file.Dispose();
                    iter++;
                    //path = _dir.Path + "/" + FileName + "_" + iter.ToString("D3") + ".pdf";
                    path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads, FileName + "_" + iter.ToString("D3") + ".pdf");
                    file = new Java.IO.File(path);
                }
                file.CreateNewFile();

                var fileDescriptor = ParcelFileDescriptor.Open(file, ParcelFileMode.ReadWrite);
                file.Dispose();

                var writeResultCallback = new PdfWriteResultCallback(TaskCompletionSource, path);

                Adapter.OnWrite(new Android.Print.PageRange[] { PageRange.AllPages }, fileDescriptor, new CancellationSignal(), writeResultCallback);
            }
            base.OnLayoutFinished(info, changed);
        }
Exemple #2
0
        public override void OnLayoutFinished(PrintDocumentInfo info, bool changed)
        {
            //using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments))
            //using (var _dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads))
            //using (var _dir = Forms9Patch.Droid.Settings.Context.FilesDir)
            using (var _dir = Forms9Patch.Droid.Settings.Context.CacheDir)
            {
                if (!_dir.Exists())
                {
                    _dir.Mkdir();
                }

                // var path = _dir.Path + "/" + FileName + ".pdf";
                //var path = System.IO.Path.Combine(_dir.AbsolutePath, Android.OS.Environment.DirectoryDownloads, FileName + ".pdf");
                //var file = new Java.IO.File(path);

                /*
                 * var file = new Java.IO.File(_dir, FileName + ".pdf");
                 * int iter = 0;
                 * while (file.Exists())
                 * {
                 *  file.Dispose();
                 *  iter++;
                 *  //path = _dir.Path + "/" + FileName + "_" + iter.ToString("D3") + ".pdf";
                 *  //path = System.IO.Path.Combine(_dir.AbsolutePath, Android.OS.Environment.DirectoryDownloads, FileName + "_" + iter.ToString("D4") + ".pdf");
                 *  //file = new Java.IO.File(path);
                 *  file = new Java.IO.File(_dir, FileName + "_" + iter.ToString("D3") + ".pdf");
                 * }
                 *
                 *
                 * //file.CreateNewFile();
                 */
                var file = Java.IO.File.CreateTempFile(FileName + ".", ".pdf", _dir);

                var fileDescriptor = ParcelFileDescriptor.Open(file, ParcelFileMode.ReadWrite);

                var writeResultCallback = new PdfWriteResultCallback(TaskCompletionSource, file.AbsolutePath);

                Adapter.OnWrite(new Android.Print.PageRange[] { PageRange.AllPages }, fileDescriptor, new CancellationSignal(), writeResultCallback);

                file.Dispose();

                //Android.Media.MediaScannerConnection.ScanFile(Forms9Patch.Droid.Settings.Context, new string[] { file.AbsolutePath }, new string[] { "application/pdf" }, null);
                //Android.Media.MediaScannerConnection.ScanFile(file.AbsolutePath, "application/pdf");
            }
            base.OnLayoutFinished(info, changed);
        }