How to Convert Image to PDF in ASP.NET Core | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (199)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (892)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (497)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (379)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (319)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Image to PDF

How to Convert Image to PDF in ASP.NET Core

Converting images to PDFs is critical in a wide variety of contexts in our day-to-day lives. In order to archive legal, medical, industrial, and academic documents, scanned images are converted into PDF documents. Images are converted to PDF documents and digitally signed to ensure their originality and fidelity. This type of conversion is also used for processing optical character recognition.

The Syncfusion PDF library has extended support to convert images to PDFs for the ASP.NET Core platform. In this blog, I am going to walk you through this feature, covering the following details:

Experience a leap in PDF technology with Syncfusion's PDF Library, shaping the future of digital document processing.

Converting PNG and JPEG images to PDF

To convert PNG and JPEG images to PDF, create and set up an ASP.NET Core project and refer the project to the Syncfusion PDF assemblies on NuGet.

Add the following code in Index.cshtml to select multiple images.

<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="ConvertToPDF">
    <div class="form-group">
        <div class="col-md-10">
            <p><h4>Select one or more images:</h4></p>
            <input type="file" name="files" multiple accept="image/*">
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-10">
            <br />
            <input type="submit" value="Convert to PDF">
        </div>
    </div>
</form>

This markup renders in a browser as shown in the following figure.Selecting Images for Conversion

Selecting Images for Conversion

Add a new action method ConvertToPDF in HomeContoller.cs as provided in the following code snippet.

[HttpPost]
public IActionResult ConvertToPDF(IList files)
{
    //Creating the new PDF document
    PdfDocument document = new PdfDocument();

    foreach (var formFile in files)
    {
        if (formFile.Length > 0)
        {
            MemoryStream file = new MemoryStream();
            formFile.CopyTo(file);
            //Loading the image
            PdfImage image = PdfImage.FromStream(file);
            //Adding new page
            PdfPage page = page = document.Pages.Add();

            //Drawing image to the PDF page
            page.Graphics.DrawImage(image, new PointF(0, 0));
            file.Dispose();
        }
    }
    //Saving the PDF to the MemoryStream
    MemoryStream stream = new MemoryStream();

    document.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 = "ImageToPDF.pdf";

    return fileStreamResult;
}

By executing this example, you will get the PDF document shown in the following image.Image Converted to PDF

Image Converted to PDF

Explore the wide array of rich features in Syncfusion's PDF Library through step-by-step instructions and best practices.

Converting multi-frame TIFF images to PDF

TIFF images usually have multiple frames in a single file. To draw each frame on a page of a PDF document, you need to iterate each frame individually.

To handle TIFF images, add a reference to Syncfusion.Pdf.Imaging.Net.Core NuGet package in your .NET Core project.

The following code sample draws a multi-frame TIFF image to a PDF document.

if (formFile.ContentType == "image/tiff")
{
    //Get the image stream and draw frame by frame
    PdfTiffImage tiffImage = new PdfTiffImage(file);

    int frameCount = tiffImage.FrameCount;
    for (int i = 0; i < frameCount; i++)
    {
        //Add pages to the document
        var page = document.Pages.Add();
        //Getting page size to fit the image within the page
        SizeF pageSize = page.GetClientSize();
        //Selecting frame in TIFF
        tiffImage.ActiveFrame = i;
        //Draw TIFF frame
        page.Graphics.DrawImage(tiffImage, 0, 0, pageSize.Width, pageSize.Height);
   }
}

By executing this example, you will get the PDF document shown in the following image.Multi-frame TIFF Image Converted to PDF

Multi-frame TIFF Image Converted to PDF

Changing the PDF page size to match the image size

The default size of a page in a PDF document is A4 (210 × 297 mm). You can set a custom size for a PDF document using the PageSettings property of PdfDocument. Here we are going to set the page size to be the same as the image size.

//Creating the new PDF document
PdfDocument document = new PdfDocument();

//Setting page margin to 0
document.PageSettings.Margins.All = 0;

foreach (var formFile in files)
{
    if (formFile.Length > 0)
    {
        MemoryStream file = new MemoryStream();
        formFile.CopyTo(file);

        //Loading the image
        PdfImage image = PdfImage.FromStream(file);
        //Setting same page size as image
        PdfSection section = document.Sections.Add();
        section.PageSettings.Width = image.PhysicalDimension.Width;
        section.PageSettings.Height = image.PhysicalDimension.Height;
        PdfPage page = section.Pages.Add();

        //Drawing image to the PDF page
        page.Graphics.DrawImage(image, new PointF(0, 0),new SizeF(page.Size.Width,page.Size.Height));

        file.Dispose();
    }
}
//Saving the PDF to the MemoryStream
MemoryStream stream = new MemoryStream();

document.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 = "ImageToPDF.pdf";

return fileStreamResult;

By executing this code, you will get the PDF document shown in the following image.PDF Document Size with Same Size as Original Image

PDF Document Size with Same Size as Original Image

Embark on a virtual tour of Syncfusion's PDF Library through interactive demos.

Applying transparency and rotation to images

You can add transparency and rotation to images using the SetTransparency and RotateTransform methods of PdfGraphics.

This is illustrated in the following code example.

//Loading the image
PdfImage image = PdfImage.FromStream(file);
//Adding new page
PdfPage page = page = document.Pages.Add();

//Apply transparency
page.Graphics.SetTransparency(0.5f);

//Rotate the coordinate system
page.Graphics.RotateTransform(-45);
//Drawing image to the PDF page
page.Graphics.DrawImage(image, new PointF(0, 0));

Conclusion

As you can see, the Syncfusion .NET Core PDF library provides an easy way to convert JPEG, PNG, and multi-frame TIFF images to PDF documents. With this, you can provide image-to-PDF conversion as an ASP.NET Core web service or web application.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

Please check out the sample in this GitHub repository for a hands-on experience of the image-to-PDF conversion feature. Also, take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.

If you have any questions or require clarifications about these features, please let us know in the comments section below. You can also contact us through our support forum, Direct-Trac, or Feedback Portal. We are happy to assist you!

Tags:

Share this post:

Comments (2)

Hi Praveen,

Our requirement is only to convert jpg and png images into pdf and then upload.

We are able to achieve this feature by referring this article.

Could you please let us no how to by this package only and its costing?

Hi Yogesh,

Thank you for your interest in the Syncfusion product.

The PDF library is bundled with our file format package, the file format package has the following library

1. PDF
2. Excel
3. Word
4. PowerPoint.

You can check the pricing details of the file format package in the below link.
https://www.syncfusion.com/sales/products/fileformats

For more sales related information, you can contact sales@syncfusion.com

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed