Working with Images

Essential PDF supports both raster and vector images.

Images are supported through the PdfImage class, which is an abstract base class that provides the common functionality for PdfBitmap and PdfMetafile classes.

Inserting an image in a new document

The following raster images are supported in Essential PDF.

  • BMP
  • JPEG
  • JPEG with Exif standard
  • GIF
  • PNG
  • TIFF
  • ICO and ICON

You can load image streams, files on disk, and use System.Drawing.Bitmap objects to draw the images through the DrawImage method of the PdfGraphics class.

The following code snippet shows how to add a file from disk to the PDF document.

//Create a new PDF document.

PdfDocument doc = new PdfDocument();

//Add a page to the document.

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the disk.

PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document.

doc.Save("Output.pdf");

//Close the document.

doc.Close(true);
'Create a new PDF document.

Dim doc As New PdfDocument()

'Add a page to the document.

Dim page As PdfPage = doc.Pages.Add()

'Create PDF graphics for the page

Dim graphics As PdfGraphics = page.Graphics

'Load the image from the disk.

Dim image As New PdfBitmap("Autumn Leaves.jpg")

'Draw the image

graphics.DrawImage(image, 0, 0)

'Save the document.

doc.Save("Output.pdf")

'Close the document.

doc.Close(True)
//Create a new PDF document.

PdfDocument doc = new PdfDocument();

//Add a page to the document.

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image as stream from the disk.

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the document.

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create a new PDF document.

PdfDocument doc = new PdfDocument();

//Add a page to the document.

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the disk.

FileStream imageStream = new FileStream("Autumn Leaves.jpg", FileMode.Open, FileAccess.Read);

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Creating the stream object

MemoryStream stream = new MemoryStream();

//Save the document as stream

doc.Save(stream);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

doc.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = "Output.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Create a new PDF document.

PdfDocument doc = new PdfDocument();

//Add a page to the document.

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image as stream

Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the document.

doc.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

Inserting an image in an existing document

You can also add images into an existing PDF document using the below code snippet.

//Load a PDF document.

PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the disk

PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document.

doc.Save("Output.pdf");

//Close the document.

doc.Close(true);
'Load a PDF document.

Dim doc As New PdfLoadedDocument("input.pdf")

'Get first page from document

Dim page As PdfLoadedPage = TryCast(doc.Pages(0), PdfLoadedPage)

'Create PDF graphics for the page

Dim graphics As PdfGraphics = page.Graphics

'Load the image from the disk

Dim image As New PdfBitmap("Autumn Leaves.jpg")

'Draw the image

graphics.DrawImage(image, 0, 0)

'Save the document.

doc.Save("Output.pdf")

'Close the document.

doc.Close(True)
//Create the file open picker

var picker = new FileOpenPicker();

picker.FileTypeFilter.Add(".pdf");

//Browse and chose the file

StorageFile file = await picker.PickSingleFileAsync();

//Creates an empty PDF loaded document instance

PdfLoadedDocument doc = new PdfLoadedDocument();

//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class

await doc.OpenAsync(file);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image as stream

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the document.

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Load the PDF document

FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);

PdfLoadedDocument doc = new PdfLoadedDocument(docStream);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the disk

FileStream imageStream = new FileStream("Autumn Leaves.jpg", FileMode.Open, FileAccess.Read);

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Creating the stream object

MemoryStream stream = new MemoryStream();

//Save the document as stream

doc.Save(stream);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

doc.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = "Output.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Load the file as stream

Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");

PdfLoadedDocument doc = new PdfLoadedDocument(docStream);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image as stream

Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the document.

doc.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

To add image from stream, use the below code snippet.

//Load a PDF document.

PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image

Stream imageStream = File.OpenRead("Autumn Leaves.jpg");

//Load the image from the stream

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document.

doc.Save("Output.pdf");

//Close the document.

doc.Close(true);
'Load a PDF document.

Dim doc As New PdfLoadedDocument("input.pdf")

'Get first page from document

Dim page As PdfLoadedPage = TryCast(doc.Pages(0), PdfLoadedPage)

'Create PDF graphics for the page

Dim graphics As PdfGraphics = page.Graphics

'Load the image

Dim imageStream As Stream = File.OpenRead("Autumn Leaves.jpg")

'Load the image from the stream

Dim image As New PdfBitmap(imageStream)

'Draw the image

graphics.DrawImage(image, 0, 0)

'Save the document.

doc.Save("Output.pdf")

'Close the document.

doc.Close(True)
//Create the file open picker

var picker = new FileOpenPicker();

picker.FileTypeFilter.Add(".pdf");

//Browse and chose the file

StorageFile file = await picker.PickSingleFileAsync();

//Creates an empty PDF loaded document instance

PdfLoadedDocument doc = new PdfLoadedDocument();

//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class

await doc.OpenAsync(file);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image as stream

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the document.

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Load the PDF document

FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);

PdfLoadedDocument doc = new PdfLoadedDocument(docStream);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the disk

FileStream imageStream = new FileStream("Autumn Leaves.jpg", FileMode.Open, FileAccess.Read);

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Creating the stream object

MemoryStream stream = new MemoryStream();

//Save the document as stream

doc.Save(stream);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

doc.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = "Output.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Load the file as stream

Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");

PdfLoadedDocument doc = new PdfLoadedDocument(docStream);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image as stream

Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the document.

doc.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

Inserting a vector image

Essential PDF supports adding Metafile vector image. During the insertion, Metafile graphics will be transformed to native PDF graphics that supports text selection and searching. The following types of Metafiles are supported in Essential PDF.

  • EMF only
  • EMF plus
  • EMF plus dual
  • WMF

PdfMetafile class is used to load EMF images. Additionally the PdfMetafileLayoutFormat class allows you to prevent text and image split across pages in the PDF document.

The following code illustrate this,

// Create a PDF Document.

PdfDocument doc = new PdfDocument();

//Add pages to the document

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Create the layout format

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();

//Split text and image between pages

format.SplitImages = true;

format.SplitTextLines = true;

//Create a Metafile instance

PdfMetafile metaChart = new PdfMetafile("MetaChart.emf");

//Draw the Metafile in the page

metaChart.Draw(page, PointF.Empty, format);

//Save the document

doc.Save("Output.pdf");

//Close the document

doc.Close(true);
'Create a PDF Document.

Dim doc As New PdfDocument()

'Add pages to the document

Dim page As PdfPage = doc.Pages.Add()

'Create PDF graphics for the page

Dim graphics As PdfGraphics = page.Graphics

'Create the layout format

Dim format As New PdfMetafileLayoutFormat()

'Split text and image between pages

format.SplitImages = True

format.SplitTextLines = True

'Create a Metafile instance

Dim metaChart As New PdfMetafile("MetaChart.emf")

'Draw the Metafile in the page

metaChart.Draw(page, PointF.Empty, format)

'Save the document

doc.Save("Output.pdf")

'Close the document

doc.Close(True)
//PDF supports inserting a vector image only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports inserting a vector image only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports inserting a vector image only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

Working with image masking

Essential PDF supports image masking through the PdfImageMask class.

The following code illustrate shows how to add a mask to TIFF image.

//Create a PDF document

PdfDocument doc = new PdfDocument();

//Add pages to the document

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the TIFF image

PdfBitmap image = new PdfBitmap("image.tif");

//Create masking image

PdfImageMask mask = new PdfImageMask(new PdfBitmap("mask.bmp"));

image.Mask = mask;

//Draw the image

graphics.DrawImage(image, 0, 0);

//Saves the document

doc.Save("Output.pdf");

//Close the document

doc.Close(true);
'Create a PDF document

Dim doc As New PdfDocument()

'Add pages to the document

Dim page As PdfPage = doc.Pages.Add()

'Create PDF graphics for the page

Dim graphics As PdfGraphics = page.Graphics

'Load the TIFF image

Dim image As New PdfBitmap("image.tif")

'Create masking image

Dim mask As New PdfImageMask(New PdfBitmap("mask.bmp"))

image.Mask = mask

'Draw the image

graphics.DrawImage(image, 0, 0)

'Saves the document

doc.Save("Output.pdf")

'Close the document

doc.Close(True)
//PDF supports image masking only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports image masking only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports image masking only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

Replacing Images in an existing PDF document

Essential PDF allows you to replace images in an existing document. The ReplaceImage method of the page collection allows you to replace an image.

//Load the PDF document

PdfLoadedDocument doc = new PdfLoadedDocument(@"image.pdf");

//Create an image instance

PdfBitmap image = new PdfBitmap(@"Autumn Leaves.jpg");

//Replace the first image in the page.

doc.Pages[0].ReplaceImage(0, image);

//Save the document

doc.Save("Output.pdf");

//Close the document

doc.Close(true);
'Load the PDF document

Dim doc As New PdfLoadedDocument("image.pdf")

'Create an image instance

Dim image As New PdfBitmap("Autumn Leaves.jpg")

'Replace the first image in the page.

doc.Pages(0).ReplaceImage(0, image)

'Save the document

doc.Save("Output.pdf")

'Close the document

doc.Close(True)
//PDF supports replacing image in an existing PDF document only in Windows Forms, WPF,ASP.NET and ASP.NET MVC platforms.
//PDF supports replacing image in an existing PDF document only in Windows Forms, WPF,ASP.NET and ASP.NET MVC platforms.
//PDF supports replacing image in an existing PDF document only in Windows Forms, WPF,ASP.NET and ASP.NET MVC platforms.

Image Pagination

You can allow a large image to paginate across multiple pages in the PDF document. This can be done through the PdfLayoutFormat class as shown below.

//Create Document

PdfDocument doc = new PdfDocument();

//Add new page

PdfPage page = doc.Pages.Add();

//Load a bitmap

PdfBitmap image = new PdfBitmap("input.jpg");

//Set layout property to make the element break across the pages.

PdfLayoutFormat format = new PdfLayoutFormat();

format.Break = PdfLayoutBreakType.FitPage;

format.Layout = PdfLayoutType.Paginate;

//Draw image

image.Draw(page,20,400, format);

//Save the PDF

doc.Save("output.pdf");

doc.Close(true);
'Create Document

Dim doc As New PdfDocument()

'Add new page

Dim page As PdfPage = doc.Pages.Add()

'Load a bitmap

Dim image As New PdfBitmap("input.jpg")

'Set layout property to make the element break across the pages.

Dim format As New PdfLayoutFormat()

format.Break = PdfLayoutBreakType.FitPage

format.Layout = PdfLayoutType.Paginate

'Draw image

image.Draw(page, 20, 400, format)

'Save the PDF

doc.Save("output.pdf")

doc.Close(True)
//Create Document

PdfDocument doc = new PdfDocument();

//Add new page

PdfPage page = doc.Pages.Add();

//Load the image as stream

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Data.Assets.Autumn Leaves.jpg");

//Load a bitmap

PdfBitmap image = new PdfBitmap(imageStream);

//Set layout property to make the element break across the pages.

PdfLayoutFormat format = new PdfLayoutFormat();

format.Break = PdfLayoutBreakType.FitPage;

format.Layout = PdfLayoutType.Paginate;

//Draw image

image.Draw(page, 20, 400, format);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the document.

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create Document

PdfDocument doc = new PdfDocument();

//Add new page

PdfPage page = doc.Pages.Add();

//Load a bitmap

FileStream imageStream = new FileStream("Autumn Leaves.jpg", FileMode.Open, FileAccess.Read);

PdfBitmap image = new PdfBitmap(imageStream);

//Set layout property to make the element break across the pages.

PdfLayoutFormat format = new PdfLayoutFormat();

format.Break = PdfLayoutBreakType.FitPage;

format.Layout = PdfLayoutType.Paginate;

//Draw image

image.Draw(page, 20, 400, format);

//Creating the stream object

MemoryStream stream = new MemoryStream();

//Save the document as stream

doc.Save(stream);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

doc.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = "Output.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Create Document

PdfDocument doc = new PdfDocument();

//Add new page

PdfPage page = doc.Pages.Add();

//Load a bitmap

Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Autumn Leaves.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//Set layout property to make the element break across the pages.

PdfLayoutFormat format = new PdfLayoutFormat();

format.Break = PdfLayoutBreakType.FitPage;

format.Layout = PdfLayoutType.Paginate;

//Draw image

image.Draw(page, 20, 400, format);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the document.

doc.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

Applying transparency and rotation to the image

You can add transparency and rotation to the image. This is explained in the below code snippet.

//Create Document

PdfDocument doc = new PdfDocument();

//Add a new page

PdfPage page = doc.Pages.Add();

//Load a bitmap

PdfBitmap image = new PdfBitmap("input.jpg");

//save the current graphics state

PdfGraphicsState state = page.Graphics.Save();

//Translate the coordinate system to the  required position

page.Graphics.TranslateTransform(20, 100);

//Apply transparency

page.Graphics.SetTransparency(0.5f);

//Rotate the coordinate system

page.Graphics.RotateTransform(-45);

// Draw image

image.Draw(page, 0, 0);

//Restore the graphics state

page.Graphics.Restore(state);

//Save the PDF

doc.Save("output.pdf");

doc.Close(true);
'Create Document

Dim doc As New PdfDocument()

'Add a new page

Dim page As PdfPage = doc.Pages.Add()

'Load a bitmap

Dim image As New PdfBitmap("input.jpg")

'save the current graphics state

Dim state As PdfGraphicsState = page.Graphics.Save()

'Translate the coordinate system to the  required position

page.Graphics.TranslateTransform(20, 100)

'Apply transparency

page.Graphics.SetTransparency(0.5F)

'Rotate the coordinate system

page.Graphics.RotateTransform(-45)

' Draw image

image.Draw(page, 0, 0)

'Restore the graphics state

page.Graphics.Restore(state)

'Save the PDF

doc.Save("output.pdf")

doc.Close(True)
//Create Document

PdfDocument doc = new PdfDocument();

//Add new page

PdfPage page = doc.Pages.Add();

//Load the image as stream

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Data.Assets.input.jpg");

//Load a bitmap.

PdfBitmap image = new PdfBitmap(imageStream);

//save the current graphics state

PdfGraphicsState state = page.Graphics.Save();

//Translate the coordinate system to the  required position

page.Graphics.TranslateTransform(20, 100);

//Apply transparency

page.Graphics.SetTransparency(0.5f);

//Rotate the coordinate system

page.Graphics.RotateTransform(-45);

// Draw image

image.Draw(page, 0, 0);

//Restore the graphics state

page.Graphics.Restore(state);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the document.

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create Document

PdfDocument doc = new PdfDocument();

//Add a new page

PdfPage page = doc.Pages.Add();

//Load a image as stream

FileStream imageStream = new FileStream("input.jpg", FileMode.Open, FileAccess.Read);

//Load a bitmap.

PdfBitmap image = new PdfBitmap(imageStream);

//save the current graphics state

PdfGraphicsState state = page.Graphics.Save();

//Translate the coordinate system to the  required position

page.Graphics.TranslateTransform(20, 100);

//Apply transparency

page.Graphics.SetTransparency(0.5f);

//Rotate the coordinate system

page.Graphics.RotateTransform(-45);

// Draw image

image.Draw(page, 0, 0);

//Restore the graphics state

page.Graphics.Restore(state);

//Creating the stream object

MemoryStream stream = new MemoryStream();

//Save the document as stream

doc.Save(stream);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

doc.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = "Output.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Create Document

PdfDocument doc = new PdfDocument();

//Add a new page

PdfPage page = doc.Pages.Add();

//Load a bitmap

Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.jpg");

PdfBitmap image = new PdfBitmap(imageStream);

//save the current graphics state

PdfGraphicsState state = page.Graphics.Save();

//Translate the coordinate system to the  required position

page.Graphics.TranslateTransform(20, 100);

//Apply transparency

page.Graphics.SetTransparency(0.5f);

//Rotate the coordinate system

page.Graphics.RotateTransform(-45);

// Draw image

image.Draw(page, 0, 0);

//Restore the graphics state

page.Graphics.Restore(state);

//Save the document as stream.

MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the document.

doc.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}