Articles in this section
Category / Section

How to insert GIF image to PDF document in ASP.NET Core platform

5 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can extract the images from a PDF document in ASP.NET Core.

Steps to insert the GIF image to a PDF document in ASP.NET Core programmatically:

  1. Create a new C# ASP.NET Core web application project.

Create a new .NET Core application project

  1. Select the web application pattern (Model-View-Controller) for the project.

Select web application pattern

  1. Install the Syncfusion.Imaging.Pdf.Net.Core NuGet package as a reference to your .NET Standard application from Nuget.org.

NuGet package reference

  1. A default controller with the name HomeController.cs gets added to the creation of the ASP.NET MVC project. Include the following namespaces in the HomeController.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;

 

  1. A default action method named Index will be present in the HomeController.cs. Right-click the Index method and select ‘Go To View,’ you will be directed to its associated view page index.cshtml. 
  2. Add a new button in the Index.cshtml as follows.

CSHTML

@{Html.BeginForm("ImageToPDF", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Create Document" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}

 

  1. Add the new method ImageToPDF in HomeController.cs and include the following code sample to insert the GIF image to a PDF document in the ASP.NET Core platform.

C#

//Create a PDF document 
PdfDocument pdfDocument = new PdfDocument();
 
//Add a page 
PdfPage page = pdfDocument.Pages.Add();
PdfGraphics graphics = page.Graphics;
 
//Getting page size to fit the image within the page
SizeF pageSize = page.GetClientSize();
 
//Load the image file 
string path = _hostingEnvironment.WebRootPath + "/Data/Ani.gif";
Stream imageStream = new FileStream(path, FileMode.Open);
 
//Get the image stream and draw frame by frame
PdfTiffImage tiffImage = new PdfTiffImage(imageStream);
 
int frameCount = tiffImage.FrameCount;
for (int i = 0; i < frameCount; i++)
{
    //Selecting frame in TIFF
    tiffImage.ActiveFrame = i;
    //Draw TIFF frame
    page.Graphics.DrawImage(tiffImage, 0, 0, pageSize.Width, pageSize.Height);
}
 
//Saving the PDF to the MemoryStream
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
 
//Set the position as '0'.
stream.Position = 0;
 
//Download the PDF document in the browser
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
 
fileStreamResult.FileDownloadName = "Sample.pdf";
 
return fileStreamResult;

 

A complete working sample to insert the GIF image to a PDF document in the ASP.NET Core platform can be download from GIFToPDFSample.zip.

 

Take a moment to peruse the documentation. You will find options such as inserting an image in a new PDF document and an existing PDF document, inserting vector image, image masking, replacing images, image pagination, apply transparency, and rotation to image and TIFF to PDF.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

Image to PDF in Windows Forms

Add image as background in PDF document

 

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