0
votes

I am looking for solution generating pdf file from string using Xamarin ( Xamarin Forms or Xamarin iOS).

I have found libraries like iTextSharp, SelectPDF or Syncfusion and other but all of them must be paid. I need entirely free solution.

PDFsharp library almost works:

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
const string filename = "HelloWorld.pdf";
document.Save(filename);

But at 'XFont" there is an error: "Reference to type 'FontFamily' claims it is defined in 'System.Drawing', but it could not be found".

Apple provides solution: link but this is objective-c that I am not familiar with and there are a lot of errors when I paste this code in Visual Studio.

Have you got solution written in C#?

2

2 Answers

0
votes

You can use this library: PDFSharp.Xamarin.Forms.

I just tested and it works well on iOS.

You can reference the project inside Core and Library folder to your project.

He use dependence service to save the pdf file:

var pdf = PDFManager.GeneratePDFFromView(mainGrid);

DependencyService.Get<IPdfSave>().Save(pdf, "SinglePage.pdf");

Remember to delete the bin and obj folder and rebuild project if you can't run it after download.

0
votes

You can also use SkiaSharp, a great drawing library that can generate PDF files.