How to create WinForms PDF document in Azure function?
The Syncfusion Essential® PDF is a feature-rich and high performance .NET PDF Library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can create PDF document using .NET in Azure functions.
Steps to create PDF in Azure functions programmatically:
- Create a new Azure function project.
- Select framework Azure Functions v1 (.NET Framework) and HTTP trigger as follows.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your Azure functions project from NuGet.org.
- Include the following namespaces in Function1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Grid;
- Add the following code snippet in Function1 class to create PDF document in Azure functions.
C#
//Create a new PDF document. PdfDocument document = new PdfDocument(); //Add a page to the document. PdfPage page = document.Pages.Add(); //Create PDF graphics for the page. PdfGraphics graphics = page.Graphics; //Set the standard font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Draw the text. graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0)); MemoryStream ms = new MemoryStream(); //Save the PDF document document.Save(ms); ms.Position = 0; HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ByteArrayContent(ms.ToArray()); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "PDFDocument.pdf" }; response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"); return response;
- Right-click the project and select Publish. Then, create a new profile in the Publish Window.
- Create App service using Azure subscription.
- After creating the profile, click the publish button.
- Now, go to Azure portal and select the Functions Apps. After running the service, click Get function URL > Copy. Paste the same in the new browser tab. You will get the output PDF document as follows.
A Complete working sample can be downloaded from PDFCreation_AzureFunction.zip.
Take a moment to peruse the documentation, where you will find other options like drawing right-to-left text and multi-column text, consuming TrueType fonts, Standards fonts, and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to generate Hello world PDF document.
See Also:
Create a PDF file in ASP.NET Core
Create a PDF file in ASP.NET MVC
Create a PDF file in Windows Forms
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion