Copied RSS Feed

PDF

Adding a Custom Stamp to a PDF Using Xamarin PDF Viewer

What is a custom stamp?

A custom stamp is a user-defined virtual rubber stamp that allows users to add their own customized, personal signatures or seals to a PDF document. For example, this can be used for marking up an invoice in the received, approved, or expired stages of a process. It also can be used to add an approval (or other) stage with name, date, and time information.

Overview

The custom stamp annotation feature in PDF Viewer allows users to include any form of Xamarin.Forms widget, such as Button, Entry, Label, or Image, anywhere in a PDF document. You can perform operations such as add, move, resize, and delete on custom stamp annotations. Also, you can save and load the existing custom stamp annotations associated with a PDF document in the PDF Viewer control.

In this blog, we are going to see two typical examples of custom stamps used in the real world:

Adding an image as a custom stamp

PDF Viewer provides an option to add any user-defined or customized image as a custom stamp to the PDF document. The AddStamp method in PDF Viewer is used to achieve this. The following code example demonstrates how to add an image as a custom stamp.

//Initialize PdfViewer
PdfViewer pdfViewer = new PdfViewer();
 
//Load document
pdfViewer.LoadDocument(typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("PDFViewerSample.Assets.Invoice.pdf"));
 
//Set image source 
Image customImage = new Image();
customImage.Source = "Expired.png";
 
//Add image as custom stamp
pdfViewer.AddStamp(customImage, 1, new Point(250, 400));

This code example will add the image as a custom stamp in the location 250X400 in the first page of the PDF document.

Adding a custom dynamic stamp

Dynamic stamps obtain information from your computer and allow you to indicate name, date, and time information on the stamp. Please find the following a reference image for a dynamic stamp.

PDF Viewer lets you add a custom dynamic stamp to a PDF document. The AddStamp method in PDF Viewer is used to add this stamp. The following code example demonstrates how to create and add a custom dynamic stamp to a PDF page.

<Grid> 
  <Image  x:Name="approved" Source="Approved.png" Aspect="Fill" />
  <Label  x:Name="dateAndTime" VerticalOptions="End" />
</Grid>
//Add the Date and Time value to the custom stamp/image
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
    Device.BeginInvokeOnMainThread(() => dateAndTime.Text = DateTime.Now.ToString());
        return true;
    });
});
//Convert the Grid with dynamic date/time text as image
DependencyService.Get<CustomImage>().ConvertToImage(tappedstamp as Grid);
Stream stream= DependencyService.Get<CustomImage>().GetStream();
stream.Position = 0;
 
//Set image source
Image customImage = new Image();
customImage.Source = ImageSource.FromStream(() => stream);
customImage.HeightRequest = 90;
customImage.WidthRequest = 300;                   
 
//Add image as custom stamp
pdfViewer.AddStamp(customImage, pdfViewer.PageNumber, new Point(250, 400));

Sample project

You can find a sample project that explains the previous scenarios on GitHub. The following screenshot illustrates the execution of a sample project.

In this sample, we have two custom stamp sections: Standard and Dynamic. The Standard section contains the image of the stamps, whereas the Dynamic section contains the image of the stamp along with the text field displaying the current date and time.

To learn more about adding custom stamps and using their features, refer to our documentation page, Working with Custom Stamp annotations.

Conclusion

We hope you now have a better understanding of adding custom stamps to a PDF using the Xamarin PDF Viewer control. By using these custom stamp APIs and events, you can add your own style of custom stamps to your application.

If you are already a Syncfusion user, please download the latest version of Essential Studio® from the License & Downloads page and try these new features. Our NuGet packages are available on NuGet.org. If you aren’t a customer yet, you can try our 30-day free trial to check out these features. Also, try our other samples from this GitHub location.

If you wish to send us feedback or submit any questions, please feel free to post them in the comments section below, or contact us through our support forumDirect-Trac, or feedback portal. We are happy to assist you!

If you liked this post, we hope you will also enjoy the following:

Meet the Author

Ramkumar Ravy

Ramkumar Ravy is a Product Manager at Syncfusion. He is passionate about building cross-platform mobile and Windows applications. He also loves working on Artificial Intelligence and Machine Learning and has been active since 2014.