We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create a PDF file in Xamarin.iOS?

Platform: Xamarin.iOS |
Control: PDF |
Published Date: August 16, 2018 |
Last Revised Date: April 26, 2019

PDF (Portable Document Format) is the file format used to display the document with same formatting, independent of application software, hardware, and operating system. Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a PDF document in Xamarin.iOS.

Steps to create PDF programmatically:

  1. Create a new C# Xamarin iOS application project. Create new iOS Xamarin application
  2. Select a project template, minimum iOS version, and device support for the application. Select Single View App
  3. Install the Syncfusion.Xamarin.Pdf NuGet package as reference to your .NET Framework applications from NuGet.org. Install Nuget packages
  4. In the project, add new UIViewController class. Add new UIViewController class in project
  1. In AppDelegate.cs, add the following code on FinishedLaunching() to load the UIViewController1 at top of the window.
    //Load the UIViewController for UI Window
    Window.RootViewController = new UIViewController1();
    

 

  1. In UIViewController1.cs, add the following code in the ViewDidLoad() method to add the button in the UIView.
    var btn = UIButton.FromType(UIButtonType.System);
    btn.Frame = new CoreGraphics.CGRect(20, 200, 280, 44);
    btn.SetTitle("Generate PDF Document", UIControlState.Normal);
    btn.TouchUpInside += OnButtonClicked;
    View.AddSubview(btn);
    
  1. Include the following namespace in the UIViewController1.cs file.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    using SPointF = Syncfusion.Drawing.PointF;
    
  1. In the click event method (OnButtonClicked), add the following code to create a PDF file and save it in a stream.
    // Create a new PDF document
    PdfDocument document = new PdfDocument();
     
    //Add a page to the document
    PdfPage page = document.Pages.Add();
     
    //Create PDF graphics for a page
    PdfGraphics graphics = page.Graphics;
     
    //Set the standard font
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
     
    //Draw the text
    graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new SPointF(0, 0));
     
    //Save the document to the stream
    MemoryStream stream = new MemoryStream();
    document.Save(stream);
     
    //Close the document
    document.Close(true);
     
    //Save the stream as a file in the device and invoke it for viewing
    SaveIOS saveIOS = new SaveIOS();
    saveIOS.SaveAndView("Output.pdf", "application/pdf", stream);
    
  1. Add the SaveIOS class to the project, where the stream will be saved to a physical file and the file can be opened for viewing. Refer to the following code.
    class SaveIOS
    {
      //Method to save document as a file and view the saved document
      public async Task SaveAndView(string filename, string contentType, MemoryStream stream)
      {
        //Get the root path in iOS device
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string filePath = Path.Combine(path, filename);
     
        //Create a file and write the stream into it
        FileStream fileStream = File.Open(filePath, FileMode.Create);
        stream.Position = 0;
        stream.CopyTo(fileStream);
        fileStream.Flush();
        fileStream.Close();
     
        //Invoke the saved document for viewing
        UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
        while (currentController.PresentedViewController != null)
            currentController = currentController.PresentedViewController;
        UIView currentView = currentController.View;
     
        QLPreviewController qlPreview = new QLPreviewController();
        QLPreviewItem item = new QLPreviewItemBundle(filename, filePath);
        qlPreview.DataSource = new PreviewControllerDS(item);
     
        currentController.PresentViewController(qlPreview, true, null);
      }
    }
    

 

Note:

To preview the PDF document in QuickLook, add the PreviewControllerDS, QLPreviewItemFileSystem, and QLPreviewItemBundle classes in the project. The code for these helper classes can be found from this link (iOS -> PreviewControllerDS.cs).

  1. Compile and execute the application. Now, this application creates a simple PDF document.

A complete working sample can be downloaded from Create-PDF-file.zip

Take a moment to peruse the documentation, where you can find other options like drawing right-to-left text and multi-column text,  consuming TrueType fonts, Standard fonts and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile