Xamarin.Android pdf generator

I recently worked on Xamarin.Android . I need to use a pdf generator to send a report by email.

I came across the next block. I really don't know what to add to FileStream fs = new FileStream (???????); . In addition, I would like to open or see this pdf on the screen.

 using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using System.IO; using XamiTextSharpLGPL; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp; namespace PDFAapp { [Activity (Label = "PDFAapp", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { int count = 1; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); FileStream fs = new FileStream (???????); Document document = new Document (PageSize.A4, 25, 25, 30, 30); PdfWriter writer = PdfWriter.GetInstance (document, fs); document.Add(new Paragraph("Hello World")); document.Close(); writer.Close(); fs.Close(); } } } 

EDIT 1:

Having the following code to open the generated Pdf, but it shows that the pdf format is not valid.

 Java.IO.File file = new Java.IO.File(filePath); Intent intent = new Intent(Intent.ActionView); intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf"); StartActivity(intent); 
+7
android c # pdf filestream xamarin
source share
2 answers

First, make sure that in the Manifest file you enable WriteExternalStorage

To read or write files to external storage, your application must have READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission. For example:

  <manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> 
Constructor

FileStream (one of many) takes a string for the path and FileMode , so there will be an approximate solution.

  var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var path = Path.Combine(directory, "myTestFile.pdf"); var fs = new FileStream(path, FileMode.Create); 

This creates a folder called "pdf" for external storage, and then creates a pdf file. Additional code can be included to check if the file exists before it is created.

Edit: full example just tested on my device:

  protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var path = Path.Combine(directory, "myTestFile.pdf"); if (File.Exists(path)) { File.Delete(path); } var fs = new FileStream(path, FileMode.Create); Document document = new Document(PageSize.A4, 25, 25, 30, 30); PdfWriter writer = PdfWriter.GetInstance(document, fs); document.Open(); document.Add(new Paragraph("Hello World")); document.Close(); writer.Close(); fs.Close(); Java.IO.File file = new Java.IO.File(path); Intent intent = new Intent(Intent.ActionView); intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf"); StartActivity(intent); } 
+6
source share

If you are doing cross-platform development with your player to develop a PDF file. see below the PDF creation code in PCL using iTEXT sharp. iTEXT PDF

here I have a PCL storage library for IO access, which is very useful for PCL. You can find more information here PCL Storage

  • Add the PCL storage library to the PCL project, as well as the corresponding platforms, using nuget.

PCLStorage Installation Package

  1. Add iTEXT PDF to PCL Project

    Install-Package iTextSharp

  2. use the code below in the PCL project

     public class PdfHelper { public async Task PCLGenaratePdf(string path) { IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(path); IFolder folder = await rootFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists); IFile file = await folder.CreateFileAsync("file.pdf", CreationCollisionOption.ReplaceExisting); using (var fs = await file.OpenAsync(FileAccess.ReadAndWrite)) { var document = new Document(PageSize.A4, 25, 25, 30, 30); PdfWriter writer = PdfWriter.GetInstance(document, fs); document.Open(); document.Add(new Paragraph("heloo everyone")); document.Close(); writer.Close(); } } public async Task<string> PCLReadFile(string path) { IFile file = await FileSystem.Current.GetFileFromPathAsync(path); return file.Path; } } 
  3. use the code below in an android project.

Generate pdf

  private async void Genarate(string path) { var creator = new PdfHelper(); await creator.PCLGenaratePdf(path); } 

Reading PDF Using Intent

  private async void ReadPDF(object sender, EventArgs e) { String sdCardPath = Environment.ExternalStorageDirectory.AbsolutePath; String fullpath = Path.Combine(sdCardPath, "folder/" + "file.pdf"); var creator = new PdfHelper(); string filePath = await creator.PCLReadFile(fullpath); Uri pdfPath = Uri.FromFile(new File(filePath)); Intent intent = new Intent(Intent.ActionView); intent.SetDataAndType(pdfPath, "application/pdf"); intent.SetFlags(ActivityFlags.NewTask); Application.Context.StartActivity(intent); } 
  1. write ios code to create and get a PDF file.

Note : sometimes an error may occur in system.drawings and system.security after compiling a PCL project. The solution uses the iTEXT text source code, removes all the dependencies system.drawings and system.security.

Happy coding.

+1
source share

All Articles