Articles in this section
Category / Section

How to add an image as content in PDF

2 mins read

Images can be added as content in the pages of a PDF document using Syncfusion Xamarin PDF Viewer. In the PageTapped event of PDF Viewer, you will get the location on the page, where the tap action is performed from the e.Position of PageTappedEventArgs. The image can be added as content to that specific position of a page using PdfLoadedDocument API.

To add images in the PDF document please refer to the following code snippet.

 

private void PdfViewerControl_Tapped(object sender, Syncfusion.SfPdfViewer.XForms.TouchInteractionEventArgs e)
{
if (isImageAdditionEnabled)
{
 
//Gets the point
Point tappedPoint = e.Position;
 
//Pixel to point conversion
float x = pdfUnitConverter.ConvertFromPixels((float)tappedPoint.X, PdfGraphicsUnit.Point);
 
float y = pdfUnitConverter.ConvertFromPixels((float)tappedPoint.Y, PdfGraphicsUnit.Point);
 
//Get the image stream
Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("ImageContent.Assets.Image.png");
 
//Save the document to preserve all the current changes made in the document.
Stream documentStream = pdfViewerControl.SaveDocument();
 
//Create PdfLoadedDocument instance from the saved stream.
PdfLoadedDocument doc = new PdfLoadedDocument(documentStream);
 
//Get tapped page from document
PdfLoadedPage page = doc.Pages[e.PageNumber-1] as PdfLoadedPage;
 
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
 
//Load the image from the disk
PdfBitmap image = new PdfBitmap(imageStream);
 
//Draw the image and save the stream
graphics.DrawImage(image, x, y);
MemoryStream savedStream = new MemoryStream();
doc.Save(savedStream);
 
//Load the modified document
pdfViewerControl.LoadDocument(savedStream);
 
//Disable the image addition on further tap
isImageAdditionEnabled = false;
}
}

 

Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ImageContent1777460431

 

Steps to execute and test the above scenario:

 

1. Run the attached sample.

2. Click the “Image” button above the PdfViewer which enables the flag to add the image on the page.

3. Tap on the desired location to add the image.

4. After adding the image, the document will be saved and reloaded.

5. The image will now be added as the content of the page and so it cannot be edited or removed.

 

See Also

 

To learn more about including the images as annotation and custom stamps in PdfViewer, kindly refer the following links

Working with custom stamp annotations

Adding a custom stamp to PdfViewer

 

 

 

 

 

 

 

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