private async void ShowDocumentConverterPage(OcrOutputFormat outputFormat)
        {
            bool shouldBurnAnnotations = false;

            if (_shareAfterConversion && (IsPdfFormat(SourceDocumentFormat) || IsPdfFormat(outputFormat)) && HasAnnotations)
            {
                // If we have annotation objects drawn and user would like to share his document then show message box asking him/her
                // if they would like to burn the annotations.
                shouldBurnAnnotations = await DisplayAlert("Burn Annotations", "Would you like to burn the drawn annotations into the shared document?", "Yes", "No");
            }

            _annotationsMode = shouldBurnAnnotations ? DocumentConverterAnnotationsMode.Overlay : DocumentConverterAnnotationsMode.External;
            DocumentConverterPage documentConverterPage = new DocumentConverterPage(
                _documentViewer,
                _applicationDirectory,
                _documentsDirectory,
                new List <string> {
                _documentViewer.Document.DocumentId
            },
                _documentConverterHelper,
                outputFormat,
                _annotationsMode);

            documentConverterPage.PageClosing += DocumentConverterPage_PageClosing;
            await PopupNavigation.Instance.PushAsync(documentConverterPage);
        }
        public DocumentConverterPage(DocumentViewer documentViewer, string appDirectory, string documentsDirectory, List <string> documentsIds, DocumentConverterHelper documentConverterHelper, OcrOutputFormat outputFormat, DocumentConverterAnnotationsMode annotationsMode)
        {
            InitializeComponent();

            _documentViewer          = documentViewer;
            _applicationDirectory    = appDirectory;
            _documentsDirectory      = documentsDirectory;
            _documentConverterHelper = documentConverterHelper;
            DocumentsIds             = documentsIds;
            _annotationsMode         = annotationsMode;

            Disappearing += DocumentConverterPage_Disappearing;

            if (Device.RuntimePlatform == Device.iOS)
            {
                HasSystemPadding = false;
            }

            Task.Factory.StartNew(() => ConvertDocuments(documentsIds, outputFormat));
        }
        private async void DocumentConverterPage_PageClosing(object sender, DocumentConvertEventArgs e)
        {
            string oldDocumentId         = _documentViewer.Document.DocumentId;
            DocumentConvertResult result = e.Results[oldDocumentId];

            if (result == null)
            {
                return;
            }

            try
            {
                if (result.Error != null)
                {
                    await Device.InvokeOnMainThreadAsync(async() => { await DisplayAlert("Error", $"Error converting document: {result.Error.Message}", "OK"); });
                }
                else
                {
                    if (result.Status != DocumentConverterJobStatus.Aborted)
                    {
                        await Device.InvokeOnMainThreadAsync(async() =>
                        {
                            try
                            {
                                Exception error = null;
                                LEADDocument convertedDocument = null;
                                // if we have no documents yet or if we are sharing document and burning annotations into the document then this
                                // document is a temp and we shouldn't update the document viewer with it.
                                if (_documents == null || (_shareAfterConversion && _annotationsMode == DocumentConverterAnnotationsMode.Overlay))
                                {
                                    List <Exception> errors = null;
                                    convertedDocument       = LEADDocumentHelper.CreateDocument(result.OutputDocumentFiles, null, null, out errors);
                                    if (errors != null && errors.Count > 0)
                                    {
                                        error = errors[0];
                                    }
                                }
                                else
                                {
                                    convertedDocument = LEADDocumentHelper.ReplaceDocument(oldDocumentId, result.OutputDocumentFiles, _documents, _documentIndexInList, out error);
                                }

                                if (convertedDocument != null)
                                {
                                    if (!(_shareAfterConversion && _annotationsMode == DocumentConverterAnnotationsMode.Overlay))
                                    {
                                        SetDocument(null);
                                        _documentAlreadyConverted = true;
                                        _annObjectsModified       = false;
                                        // Change the SourceDocumentFormat since we converted the document.
                                        SourceDocumentFormat = e.OutputFormat;

                                        SetDocument(convertedDocument);
                                        Document = _documentViewer.Document;
                                    }

                                    if (_shareAfterConversion)
                                    {
                                        _tempFileToDeleteAfterShare = await LEADDocumentHelper.ShareDocument(convertedDocument, null, !string.IsNullOrWhiteSpace(OutputDocumentNameLabel.Text) ? OutputDocumentNameLabel.Text : PageTitle.Text);
                                    }
                                }
                                else if (error != null)
                                {
                                    await Device.InvokeOnMainThreadAsync(async() => { await DisplayAlert("Error", $"Error converting document: {error.Message}", "OK"); });
                                }
                            }
                            catch (Exception ex)
                            {
                                await Device.InvokeOnMainThreadAsync(async() => { await DisplayAlert("Error", $"Error converting document: {ex.Message}", "Ok"); });
                            }
                        });
                    }
                }
            }
            finally
            {
                _annotationsMode = DocumentConverterAnnotationsMode.External;
            }
        }