Home > database >  Cannot save pdf file on my phone or bluestacks Xamarin.Forms
Cannot save pdf file on my phone or bluestacks Xamarin.Forms

Time:02-24

i'm trying to create a pdf using PdfSharpCore for Xamarin Forms but I've run into some issues.

 private void CreatePdf(object sender)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPage pdfPage = pdf.AddPage();
            XGraphics graph = XGraphics.FromPdfPage(pdfPage);
            XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
            graph.DrawString("This is my first PDF document", font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string localFilename = "test.pdf";
            string localPath = Path.Combine(documentsPath, localFilename);
            MemoryStream stream = new MemoryStream();
            pdf.Save(stream, false);
            byte[] bytes = stream.ToArray();
            File.WriteAllBytes(localPath, bytes);
        }

This is my function that generate the pdf and save it but when I press the button that invoke it, nothing happens. I've already add the the permissions on the AndroidManifest.xml file like this:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I've tried using a library called Xamarin.Forms.SaveOpenPDFPackage. With this library you can save and open your brand new pdf by doing this:

await CrossXamarinFormsSaveOpenPDFPackage.Current.SaveAndView("myFile1.pdf", "application/pdf", stream, PDFOpenContext.InApp);

It half works: it opens my new pdf but it doesn't save it.

Do you have any tip?

CodePudding user response:

You could try to use Syncfusion.Xamarin.PDF.

Install from NuGet package first.

Creating a PDF document with table:

  //Create a new PDF document.
  PdfDocument doc = new PdfDocument();
  //Add a page.
  PdfPage page = doc.Pages.Add();
  //Create a PdfGrid.
  PdfGrid pdfGrid = new PdfGrid();
  //Add values to list
  List<object> data = new List<object>();
  Object row1 = new { ID = "E01", Name = "Clay" };
  Object row2 = new { ID = "E02", Name = "Thomas" };
  Object row3 = new { ID = "E03", Name = "Andrew" };
  Object row4 = new { ID = "E04", Name = "Paul" };
  Object row5 = new { ID = "E05", Name = "Gray" };
  data.Add(row1);
  data.Add(row2);
  data.Add(row3);
  data.Add(row4);
  data.Add(row5);
  //Add list to IEnumerable
  IEnumerable<object> dataTable = data;
  //Assign data source.
  pdfGrid.DataSource = dataTable;
  //Draw grid to the page of PDF document.
  pdfGrid.Draw(page, new PointF(10, 10));
  //Save the PDF document to stream.
  MemoryStream stream = new MemoryStream();
  doc.Save(stream);
  //Close the document.
  doc.Close(true);

  //Save the stream as a file in the device and invoke it for viewing
  Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf",             "application/pdf", stream);

For more details about the ISave, you could get the whole code from Syncfusion official document.

  • Related