Save PDF file to Azure Blob Storage

24 Jul 20262 minutes to read

To save a PDF file to Azure Blob Storage, you can follow the steps below.

Step 1: Create a simple console application.

Project configuration window

Step 2: Install the Syncfusion.Pdf.Net.Core and Microsoft.Azure.Storage.Blob NuGet packages as a reference to your project from the NuGet.org.
NuGet package installation



NuGet package installation

Step 4: Include the following namespaces in the Program.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;

Step 5: Add the below code example to create a simple PDF and save in Azure Blob Storage.

// Create a new PDF document.
using (PdfDocument doc = new PdfDocument())
{
    // Add a page to the document.
    PdfPage page = doc.Pages.Add();
    // Get the graphics object for drawing on the page.
    PdfGraphics graphics = page.Graphics;
    // Create a font to use for drawing text.
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
    // Draw the text.
    graphics.DrawString("Hello, World!", font, PdfBrushes.Black, new PointF(10, 10));
    // Create a memory stream to save the PDF document.
    MemoryStream stream = new MemoryStream();
    doc.Save(stream);
    // Reset the stream position so the upload can read it from the beginning.
    stream.Position = 0;

    // Parse the connection string for the Azure Storage Account.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    // Create a client for accessing Blob storage.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    // Get a reference to the container and create it if it does not exist.
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    container.CreateIfNotExists();
    // Get a reference to the block blob.
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
    // Upload the PDF directly from the memory stream to the block blob.
    blockBlob.UploadFromStream(stream);
}

You can download a complete working sample from GitHub.