Articles in this section
Category / Section

How to capture images and create PDF file in .NET MAUI?

7 mins read

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can capture and create PDF document in .NET MAUI application using C#

Steps to capture and create PDF in .NET MAUI programmatically

  1. Create a new .NET MAUI Application project.configure_project.png

  2. Install the Syncfusion.Pdf.NET and Syncfusion.Pdf.Imaging.NET NuGet package as a reference to your .NET Core application from NuGet.org.

    Screenshot (1498).png
    Screenshot (1497).png

  3. Add a new button to the MainPage.xaml as shown below.

C#

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="CaptureCreatePDF.MainPage">

   <StackLayout>
   <Button
               x:Name="CameraBtn"
               Text="Capture the image"
               Clicked="OnCameraClicked"
               HorizontalOptions="Center" />

   </StackLayout>
</ContentPage>
  1. Include the following namespaces in the MainPage.xaml.cs file.
using SkiaSharp;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics; 
  1. Add a new action method OnCameraClicked in MainPage.xaml.cs and include the below code snippet to capture and create PDF in .NET MAUI.

C#

private void OnCameraClicked(object sender, EventArgs e)
{
   // Call the method CaptureCreatePDF to take a photo and create a PDF document
   CaptureCreatePDF();
}

// Capture a photo and Create a PDF document
public async void CaptureCreatePDF()
{
   // Check if capturing photo is supported
   if (MediaPicker.Default.IsCaptureSupported)
   {
       // Capture a photo
       FileResult photo = await MediaPicker.Default.CapturePhotoAsync();

       // Check if the photo is not null
       if (photo != null)
       {
           // Save the file into local storage
           string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);

           // Reduce the size of the image
           using (Stream sourceStream = await photo.OpenReadAsync())
           {
               using (SKBitmap sourceBitmap = SKBitmap.Decode(sourceStream))
               {
                   int height = Math.Min(794, sourceBitmap.Height);
                   int width = Math.Min(794, sourceBitmap.Width);

                   using (SKBitmap scaledBitmap = sourceBitmap.Resize(new SKImageInfo(width, height), SKFilterQuality.Medium))
                   {
                       using (SKImage scaledImage = SKImage.FromBitmap(scaledBitmap))
                       {
                           using (SKData data = scaledImage.Encode())
                           {
                               // Write the scaled image to the local file
                               File.WriteAllBytes(localFilePath, data.ToArray());
                           }
                       }
                   }
               }
           }

           // Create an ImageModel instance and add it to the collection
           ImageModel model = new ImageModel() { ImagePath = localFilePath, Title = "sample", Description = "Cool" };
           viewModel.Items.Add(model);

           // Check if there are items in the view model
           if (viewModel.Items.Count > 0)
           {

               // Create a new PDF document
               PdfDocument document = new PdfDocument();

               // Add a page to the document
               PdfPage page = document.Pages.Add();

               // Create PDF graphics for the page
               PdfGraphics graphics = page.Graphics;

               // Iterate through each item in the view model
               foreach (var item in viewModel.Items)
               {
                   // Open the image file
                   using (FileStream imageStream = new FileStream(item.ImagePath, FileMode.Open))
                   {
                       // Create a PDF bitmap from the image
                       PdfBitmap image = new PdfBitmap(imageStream);

                       // Draw the image on the PDF page
                       graphics.DrawImage(image, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
                   }
               }

               // Create a memory stream to save the PDF document
               using (MemoryStream ms = new MemoryStream())
               {
                   // Save the PDF document to the memory stream
                   document.Save(ms);

                   // Close the PDF document
                   document.Close(true);

                   // Reset the memory stream position
                   ms.Position = 0;

                   // Create a SaveService instance
                   SaveService service = new SaveService();

                   // Save and view the PDF document
                   service.SaveAndView("Output.pdf", "application/pdf", ms);
               }

           }
           else return;
       }
   }
}

A complete working sample can be downloaded from Capture_image_and_create_PDF.zip

By executing the program, you will get the PDF document as follows.Screenshot (1499).png

Take a moment to peruse the documentation for working with images , where you will find other options like inserting, replacing image in PDF document, image pagination, and image masking.

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

Conclusion

I hope you enjoyed learning about how to capture images and create PDF file in .NET MAUI.

You can refer to our .NET MAUI PDF feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI PDF Documentation to understand how to present and manipulate data.

For current customers, you can check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our .NET MAUI PDF and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums, Direct-Trac or feedback portal, or the 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  to leave a comment
Access denied
Access denied