Articles in this section
Category / Section

How to convert TIFF to ASP.NET Core PDF in Azure Functions 3.0?

6 mins read

The Syncfusion Essential® PDF is a .NET Core PDF library used to create, read, and edit PDF documents. Using this library, you can convert TIFF to PDF in Azure Functions 3.0.

Steps to convert TIFF to PDF in Azure Functions 3.0

  1. Create an Azure Functions projects.

Create Azure functions

  1. Select the framework to Azure Functions V3 (.NET Core) and select HTTP triggers as follows.

select http trigger.

  1. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to the project.

Install Nuget packages.

  1. Set Copy to Output Directory property to Copy if newer to input the TIFF file.

copy always to image file.

  1. Include the following namespace in Functions1.cs file to convert the TIFF to PDF using C#

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;

 

  1. Add the following code sample in Function1 class to convert TIFF to PDF in Azure Functions V3.
    string name = req.Query["name"];
    //Create a new PDF document
    PdfDocument document = new PdfDocument();
     
    //Load the multi frame TIFF image from the disk
    string TiffPath = Path.Combine(executionContext.FunctionAppDirectory, "Data", "image.tiff");
    FileStream imageStream = new FileStream(TiffPath, 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++)
    {
           //Add a section to the PDF document
           PdfSection section = document.Sections.Add();
     
           //Set page margins
           section.PageSettings.Margins.All = 0;
           tiffImage.ActiveFrame = i;
     
           //Create a PDF unit converter instance
           PdfUnitConvertor converter = new PdfUnitConvertor();
     
           //Convert to point
           Syncfusion.Drawing.SizeF size = converter.ConvertFromPixels(tiffImage.PhysicalDimension, PdfGraphicsUnit.Point);
     
           //Set page orientation
           section.PageSettings.Orientation = (size.Width > size.Height) ? PdfPageOrientation.Landscape : PdfPageOrientation.Portrait;
     
           //Set page size
           section.PageSettings.Size = size;
     
           //Add a page to the section
           PdfPage page = section.Pages.Add();
     
           //Draw TIFF image into the PDF page
           page.Graphics.DrawImage(tiffImage, Syncfusion.Drawing.PointF.Empty, size);
    }
     
    //Creating the stream object
    MemoryStream stream = new MemoryStream();
     
    //Save the document as stream
    document.Save(stream);
     
    //If the position is not set to '0' then the PDF will be empty
    stream.Position = 0;
     
    //Close the document
    document.Close(true);
     
    //Defining the ContentType for pdf file
    string contentType = "application/pdf";
     
    //Define the file name
    string fileName = "Output.pdf";
     
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(stream.ToArray());
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
           FileName = filename
    };
    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
     
    return response;
    

 

  1. Now, you can check the conversion in the local machine.
  2. Right-click the project and select publish.

Right click the project and Click publish

  1. Create a new function in the publishing window and create App service using the Azure subscription and select a hosting plan.

Create a new function

  1. Configure the Hosting plan.

Configure Hosting plan

  1. When creating a publish target, please unselect the Run from package file option. So, you can avoid the conversion failure due to permission restrictions.

Unselect the run from package file

  1. Once the profile is created, click the publish button.

Click the publish button

  1. Now, go to the Azure portal and select the Functions Apps. After running the service, click the Get function URL -> Copy. Paste the same in the browser. In this example, you need to pass the webpage URL in the query string, which needs to convert TIFF to PDF.

(https://tifftopdfsample.azurewebsites.net/api/Function1?code=***********************)

Output documents

A complete working sample can be downloaded from TIFFtoPDFSample.zip.

Take a moment to peruse the documentation. You will find other options like performing OCR on the image, region of the document, and large PDF documents with code examples.

Refer to here to explore a rich set of Syncfusion Essential® PDF features.

 

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 the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied