samples prebuilt linebreakmode examples change apps xamarin xamarin.forms

prebuilt - Cómo cargar PDF en formularios Xamarin



xamarin forms samples (3)

Recientemente hice esto en mi propio proyecto usando un renderizador personalizado. Primero implemente una vista vacía de formas de Xamarin como (he incluido un atributo de FilePath enlazable):

public class PdfViewer : View { public static readonly BindableProperty FilePathProperty = BindableProperty.Create<DocumentViewer, string>(p => p.FilePath, null); public string FilePath { get { return (string)this.GetValue(FilePathProperty); } set { this.SetValue(FilePathProperty, value); } } }

A continuación, cree un procesador de iOS que se registrará para este control. Este renderizador puede, como lo hace dentro de un proyecto de iOS, usar el Controlador de Vista Previa de Vista Rápida para delegar en el visor de PDF integrado de iOS:

[assembly: ExportRenderer(typeof(PdfViewer), typeof(DocumentViewRenderer))] public class DocumentViewRenderer : ViewRenderer<PdfViewer, UIView> { private QLPreviewController controller; protected override void OnElementChanged(ElementChangedEventArgs<DocumentViewer> e) { base.OnElementChanged(e); this.controller = new QLPreviewController(); this.controller.DataSource = new DocumentQLPreviewControllerDataSource(e.NewElement.FilePath); SetNativeControl(this.controller.View); } private class DocumentQLPreviewControllerDataSource : QLPreviewControllerDataSource { private string fileName; public DocumentQLPreviewControllerDataSource(string fileName) { this.fileName = fileName; } public override int PreviewItemCount(QLPreviewController controller) { return 1; } public override QLPreviewItem GetPreviewItem(QLPreviewController controller, int index) { var documents = NSBundle.MainBundle.BundlePath; var library = Path.Combine(documents, this.fileName); NSUrl url = NSUrl.FromFilename(library); return new QlItem(string.Empty, url); } private class QlItem : QLPreviewItem { public QlItem(string title, NSUrl uri) { this.ItemTitle = title; this.ItemUrl = uri; } public override string ItemTitle { get; private set; } public override NSUrl ItemUrl { get; private set; } } } }

No he compilado y ejecuto esto como lo he extraído de mi proyecto más grande, pero en general esto debería funcionar.

Tengo una aplicación Xamarin Forms en la que quiero abrir un PDF almacenado localmente. No es necesario que los cargue en la aplicación, estoy de acuerdo con el visor de documentos predeterminado del dispositivo para archivos PDF. ¿Cómo puedo hacer esto?

Intenté enviar un WebView al PDF, pero eso no funcionó, acabo de recibir una página en blanco.


Tenía que hacer algo y resolverlo usando un DependencyService. Puedes usarlo para abrir el pdf dependiendo de cada plataforma

Te muestro un ejemplo de cómo resolverlo en Android:

IPdfCreator.cs :

public interface IPdfCreator { void ShowPdfFile(); }

MainPage.cs:

private void Button_OnClicked(object sender, EventArgs e) { DependencyService.Get<IPdfCreator>().ShowPdfFile(); }

PdfCreatorAndroid.cs

[assembly: Dependency(typeof(PdfCreatorAndroid))] namespace Example.Droid.DependecyServices { public class PdfCreatorAndroid : IPdfCreator { public void ShowPdfFile() { var fileLocation = "/sdcard/Template.pdf"; var file = new File(fileLocation); if (!file.Exists()) return; var intent = DisplayPdf(file); Forms.Context.StartActivity(intent); } public Intent DisplayPdf(File file) { var intent = new Intent(Intent.ActionView); var filepath = Uri.FromFile(file); intent.SetDataAndType(filepath, "application/pdf"); intent.SetFlags(ActivityFlags.ClearTop); return intent; } } }

Resultado: http://i.stack.imgur.com/vrwzt.png


Aquí hay un buen proyecto que usa la Biblioteca MuPDF en xamarin. Lo he probado y funciona correctamente.

Con MuPDF puede alejar , acercar e incluso escribir alguna nota en archivos PDF .