Articles in this section
Category / Section

How to insert image into PDF using Xamarin PDF library?

3 mins read

Syncfusion Essential PDF is a Xamarin PDF library used to create, read, and edit PDF documents. Using this library, you can insert an image into a PDF document in Xamarin.

Steps to insert an image into a PDF document programmatically:

  1. Create a new C# Xamarin.Forms application project. Create new Xamarin.Forms application
  2. Select a project template and required platforms to deploy the application. In this application, to share the portable assemblies across multiple platforms, the .NET Standard code sharing strategy has been selected. For more details about code sharing, refer here.
    Note:

    If .NET Standard is not available in the code sharing strategy, the Portable Class Library (PCL) can be selected.

 

Select Blank App template

  1. Install the Syncfusion.Xamarin.PDF NuGet package as a reference to your Portable/NetStandard project from NuGet.org. Install required Nuget packages
  2. Add new Forms XAML page in portable project if there is no XAML page is defined in the App class. Otherwise, proceed to the next step.
  • To add the new XAML page, right-click the project and select Add > New Item and add a Forms XAML page from the list. Name it as MainXamlPage.
  • In App class of portable project (App.cs), replace the existing constructor of App class with the following code snippet, which invokes the MainXamlPage.
    public App()
    {
        //The root page of your application
        MainPage = new MainXamlPage();
    }
    

 

  1. In the MainXamlPage.xaml, add new button as follows.
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="GettingStarted. MainXamlPage">
    <StackLayout VerticalOptions="Center">
     
    <Button Text="Insert Image" Clicked="OnButtonClicked"  HorizontalOptions="Center" VerticalOptions="Center"/>
     
    </StackLayout>
    </ContentPage>
    

 

  1. Include the following namespaces in the MainXamlPage.xaml.cs file.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    using Syncfusion.Pdf.Parsing;
    using Syncfusion.Drawing;
    

 

  1. Add a new folder and name it as Assets and include the existing PDF file, image into the folder, and set Build Action to Embedded resource
  2. Build Action
  3. Include the following code snippet in the click event of the button in MainXamlPage.xaml.cs to insert an image into the PDF file and save it in a stream.
  4. //Get the input PDF document as stream
    Stream documentStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("InsertImage.Assets.input.pdf");
     
    //Load the existing PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream);
     
    //Get the existing PDF page
    PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
                
    //Get the images as stream
    Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("InsertImage.Assets.image.jpg");
     
    //Create a new PdfBitmap instance
    PdfBitmap image = new PdfBitmap(imageStream);
     
    //Draw the image
    loadedPage.Graphics.DrawImage(image, new PointF(40, 100));
     
    MemoryStream stream = new MemoryStream();
     
    //Save the document
    loadedDocument.Save(stream);
     
    //Close the document
    loadedDocument.Close(true);
     
    stream.Position = 0;
     
    //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);
    

 

  1. Download the helper files from this link and add them into the mentioned project. These helper files save the stream as a physical file and open the file for viewing.

Project

File name

Summary

Portable project

ISave.cs

Represent the base interface for save operation.

iOS Project

SaveIOS.cs

Save implementation for iOS device.

PreviewControllerDS.cs

Helper class for viewing the PDF file in iOS device.

Android project

SaveAndroid.cs

Save implementation for Android device.

WinPhone project

SaveWinPhone.cs

Save implementation for Windows Phone device.

UWP project

SaveWindows.cs

Save implementation for UWP device.

Windows (8.1) project

SaveWindows81.cs

Save implementation for WinRT device.

 

Note:

Introduced a new runtime permission model for the Android SDK version 23 and above. So, include the following code for enabling the android file provider to save and viewing the generated PDF document.

 

  • Create a new XML file with the name of provider_path.xml under the Android project Resources folder and add the following code in it. Eg: Resources/xml/provider_path.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-path name="external_files" path="."/>
    </paths>
    

 

  • Add the following highlighted code to the AndroidManifest.xml file.
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.InsertImage">
     <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
     <application android:label="InsertImage.Android">
        <provider android:name="android.support.v4.content.FileProvider"
              android:authorities="${applicationId}.provider"
              android:exported="false"
              android:grantUriPermissions="true">
          <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                     android:resource="@xml/provider_paths" />
        </provider>
      </application>
    </manifest>
    

 

  1. Compile and execute the application.

By executing the program, you will get the PDF document as follows. Xamarin PDF document

Download the complete work sample from Insert Images.zip

Note:

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


Conclusion

I hope you enjoyed learning about how to insert image into PDF using Xamarin PDF library.

You can refer to our Xamarin PDF Library's feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our Document processing libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied