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
//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

FileStream imageStream = new FileStream("image.tif", FileMode.Open, FileAccess.Read);

PdfTiffImage image = new PdfTiffImage(imageStream);

//Create masking image

FileStream maskStream = new FileStream("mask.bmp", FileMode.Open, FileAccess.Read);

PdfImageMask mask = new PdfImageMask(new PdfTiffImage(maskStream));

image.Mask = mask;

//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);
//PDF supports image masking only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms

NOTE

  1. Essential PDF supports image masking with Syncfusion.Pdf.Imaging.Portable assembly reference in ASP.NET Core.

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 using SetTransparency and RotateTransform methods of PdfGraphics respectively. 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);
}

Converting multi page TIFF to PDF

Multi frame TIFF image can be converted to PDF document. This can be done by accessing each frame of the multi frame TIFF image and rendering it in each page of the PDF document.

The code snippet to illustrate the same is given below.

//Create a PDF document

PdfDocument pdfDocument = new PdfDocument();

//Set page margins

pdfDocument.PageSettings.Margins.All = 0;

//Load multi frame TIFF image

PdfBitmap tiffImage = new PdfBitmap("image.tiff");

//Get the frame count

int frameCount = tiffImage.FrameCount;

//Access each frame and draw into the page

for (int i = 0; i < frameCount; i++)

{

PdfPage page = pdfDocument.Pages.Add();

PdfGraphics graphics = page.Graphics;

tiffImage.ActiveFrame = i;

graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);

}

//Save and close the document

pdfDocument.Save("Sample.pdf");

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

Dim pdfDocument As New PdfDocument()

'Set page margins

pdfDocument.PageSettings.Margins.All = 0

'Load multi frame TIFF image

Dim tiffImage As New PdfBitmap("image.tiff")

'Get the frame count

Dim frameCount As Integer = tiffImage.FrameCount

'Access each frame and draw into the page

For i As Integer = 0 To frameCount - 1

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

Dim graphics As PdfGraphics = page.Graphics

tiffImage.ActiveFrame = i

graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height)

Next

'Save and close the document

pdfDocument.Save("Sample.pdf")

pdfDocument.Close(True)
//Create a PDF document

PdfDocument pdfDocument = new PdfDocument();

//Set page margins

pdfDocument.PageSettings.Margins.All = 0;

//Load multi frame TIFF image

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.image.tiff");

PdfBitmap tiffImage = new PdfBitmap(imageStream);

//Get the frame count

int frameCount = tiffImage.FrameCount;

//Access each frame and draw into the page

for (int i = 0; i < frameCount; i++)

{

    PdfPage page = pdfDocument.Pages.Add();

    PdfGraphics graphics = page.Graphics;

    tiffImage.ActiveFrame = i;

    graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);

}

MemoryStream memoryStream = new MemoryStream();

//Save the document

await pdfDocument.SaveAsync(memoryStream);

//Close the documents

pdfDocument.Close(true);

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

Save(memoryStream, "Sample.pdf");
//Create a new PDF document

PdfDocument doc = new PdfDocument();

//Set page margins

doc.PageSettings.Margins.All = 0;

//Load the multi frame TIFF image from the disk

FileStream imageStream = new FileStream("image.tiff", FileMode.Open, FileAccess.Read);

PdfTiffImage tiffImage = new PdfTiffImage(imageStream);

//Get the frame count

int frameCount = tiffImage.FrameCount;

//Access each frame and draw into the page

for (int i = 0; i < frameCount; i++)

{
    PdfPage page = doc.Pages.Add();

    PdfGraphics graphics = page.Graphics;

    tiffImage.ActiveFrame = i;

    graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);

}

//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);
//Essential PDF supports converting multi page TIFF to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.

NOTE

  1. Essential PDF supports converting TIFF to PDF with Syncfusion.Pdf.Imaging.Portable assembly reference in ASP.NET Core.