How to insert GIF image to PDF document in ASP.NET Core platform
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:
- Create a new C# ASP.NET Core web application project.
- Select the web application pattern (Model-View-Controller) for the project.
- Install the Syncfusion.Imaging.Pdf.Net.Core NuGet package as a reference to your .NET Standard application from Nuget.org.
- 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;
- 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.
- 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(); }
- 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.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
See Also:
Add image as background in PDF document
Note:
A new version of Essential Studio® for ASP.NET is available. Versions prior to the release of Essential Studio® 2014, Volume 2 will now be referred to as classic versions. The new ASP.NET suite is powered by Essential Studio for JavaScript, providing client-side rendering of HTML5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.
The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio® for ASP.NET. Although Syncfusion® will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.
You can also contact us through our support forums or feedback portal. We are always happy to assist you!