How to crop PDF page in Xamarin Forms ?
The Syncfusion Essential PDF is a Xamarin PDF library used to create, read, and edit PDF documents. Using this library, you can crop the PDF page in Xamarin Forms.
Steps to crop PDF page in Xamarin Forms
- Create a new C# Xamarin.Forms application project.
- Select a project template and required platforms to deploy the application. In this application, the portable assemblies to be shared across multiple platforms, the .NET Standard code sharing strategy has been selected. For more details about code sharing, refer to here. Note:
If the .NET Standard is not available in the code-sharing strategy, the Portable Class Library can be selected.
- Install the Syncfusion.Xamarin.Pdf NuGet package as a reference to your .NET Framework application from NuGet.org.
- Add a new Forms XAML page in the 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 MainPage.
- In-App class of portable project (App.cs), replace the existing constructor of App class with the following code sample that invokes the MainPage.
public App() { InitializeComponent(); MainPage = new MainPage(); }
- In the MainPage.xaml, add a new button as follows.
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:CropPdfPage" x:Class="CropPdfPage.MainPage"> <StackLayout VerticalOptions="Center"> <Button Text="Generate Document" Clicked="OnButtonClicked" HorizontalOptions="Center"/> </StackLayout> </ContentPage>
- Include the following namespaces in the MainPage.cs file.
using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing;
- Include the following code sample in the click event of the button in MainPage.xaml.cs file to crop the PDF page in Xamarin Forms.
//Load the existing PDF document Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("CropPdfPage.Assets.Barcode.pdf"); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Load the page PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; //Create the template from the page PdfTemplate template = loadedPage.CreateTemplate(); //Create a new PDF document PdfDocument document = new PdfDocument(); //Calculate height from inches float inches = 6f; float height = inches * 72; //Set the document margin and height document.PageSettings.Height = height; //Add the page to new PDF document PdfPage page = document.Pages.Add(); //Create the graphics PdfGraphics graphics = page.Graphics; //Draw the template graphics.DrawPdfTemplate(template, new PointF(0, 0)); //Save the new document MemoryStream stream = new MemoryStream(); document.Save(stream); //Close the documents loadedDocument.Close(true); document.Close(true); //Save the stream as a file in the device and invoke it for viewing Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Sample.pdf", "application/pdf", stream);
- Download the helper files from this link and add them to the mentioned project. These helper files allow you to 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 the iOS device |
PreviewControllerDS.cs | Helper class for viewing the PDF file in the iOS device | |
Android project | SaveAndroid.cs | Save implementation for the Android device |
WinPhone project | SaveWinPhone.cs | Save implementation for the Windows Phone device |
UWP project | SaveWindows.cs | Save implementation for the UWP device |
Windows(8.1) project | SaveWindows81.cs | Save implementation for the WinRT device |
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 view the generated PDF document.
- Create a new XML file with the name of provider_paths.xml under the Android project Resources folder and add the following code in it.
Ex: Resources/xml/provider_paths.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 code to the AndroidManifest.xml file located under Properties/AndroidManifest.xml.
<?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.CropPdfPage"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" /> <application android:label="CropPdfPage.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>
- Compile and execute the application. This creates a crop PDF page.
Download the complete working sample from CropPDFPage.zip.
Take a moment to peruse the documentation for working with pages. You can find options like rotating a PDF page and splitting a PDF file to individual pages with code examples.
Refer to here to explore a rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.
See Also: